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
VMISS is a foreign VPS service provider establish...
Building equipment suppliers are prioritizing gre...
Continuous delivery does not mean that every chan...
Unlike in the past, affected by the epidemic, thi...
DatabaseMart is a foreign hosting company founded...
[51CTO.com original article] "It's time ...
In daily work, message communication is a very co...
From mainframes and client servers to cloud compu...
At the end of 2017, the country began to promote ...
[51CTO.com original article] On the afternoon of ...
Wikihost, also known as Micro-base, was founded i...
WiFi has been expanding its deployment and applic...
Edge computing, edge networking, and edge data ma...
Wi-Fi dramatically impacts everything from the se...
On October 28, Huawei Intelligent Manufacturing N...