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
Recently, the operating data of the three major o...
Highways will be congested, and the Internet, the...
July is already halfway through, and the hot summ...
At the end of the TV series "Bright Sword&qu...
Basic concepts of lossless network First of all, ...
How the Tactile Internet will usher in a new era ...
5G is coming, and it will be possible to experien...
With the development of the times, people are pur...
Those who need a Korean server can pay attention ...
Quick definition: A computer network hub is a lay...
ColoCrossing Easter promotion has started, with 5...
Justhost.ru is a Russian hosting company founded ...
5G communication has become a technology that man...
1. Computer Network Architecture Layering Compute...
Ethernet is a technology used to connect various ...