This article is reprinted from the WeChat public account "Full Stack Coder Portrait", the author is Xiaomajia. Please contact the Full Stack Coder Portrait public account for reprinting this article. The two interface words IEnumerator and IEnumerable are similar and have related meanings, and it is difficult to distinguish them. After many years in the industry, I have never systematically sorted out this pair of Li Kui and Li Gui. Recently, I have been arguing with the article "Actually, LRU is just so-so" by the God of Why. Solution 1 uses an array to implement LRU. The handwritten algorithm involves this pair of interfaces. I will take this opportunity to cover this pair of difficult enemies. IEnumerator The IEnumerator and IEnumerable interfaces have similar names and are often used together, but they have different purposes. The IEnumerator interface provides a way to iterate over a collection within a class. IEnumerator requires you to implement three methods:
IEnumerable The IEnumerable interface provides support for foreach iteration. IEnumerable requires you to implement the GetEnumerator method.
Which interface should I use? Based on the above words alone, it is difficult to distinguish the usage scenarios of the two interfaces. The IEnumerator interface defines how to iterate over collection-type objects in a class. The IEnumerable interface allows enumeration using a foreach loop. Therefore, the GetEnumerator method of the IEnumerable interface returns an IEnumerator interface. To implement IEnumerable, you must also implement IEnumerator. From the English root: The IEnumerator interface represents an enumerator, which defines the enumeration method and is a noun. The IEnumerable interface represents that the object has the property of being enumerable and is an adjective. In short, if you want to provide support for foreach, then make the object enumerable first, and then talk about the enumeration method, that is, implement these two interfaces. Best Practices
Why do this? If the contents of the collection change, the reset method will be called, and then the current enumerator will be invalid, and you will receive an IndexOutOfRangeException (other situations may also cause this exception). So a Try...Catch block is executed to catch this exception and throw an InvalidOperationException, indicating that modifying the collection content is not allowed while iterating. "This is also the reason why we often get an InvalidOperationException when trying to modify the iterated object in foreach. The following uses the car list as an example to implement the IEnumerator IEnumerable interface
When foreach cars, you can clearly see
|
>>: Review of 5G industry-specific networks in 2020: The beginning of a new era
Charter Communications Inc, which provides intern...
If you need a high-end server, you can also take ...
On December 12, the Ministry of Industry and Info...
In computer networks, there are multiple layers t...
[51CTO Suzhou Report] On June 9, 2017, the 2017 H...
[[391876]] Recently, market research firm Gartner...
As the digital transformation of various industri...
[[442675]] This article is reprinted from the WeC...
After the rapid development in 2020, 2021 is a cr...
[[411408]] From July 13 to 15, the 2021 China Int...
Last month, Hostons added a Jacksonville data cen...
AlphaVPS's Black Friday special packages incl...
In the past, people settled near the banks of riv...
Here is another VPS host in an unpopular area. In...
CMIVPS recently launched a new VPS host in the US...