In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to achieve IEnumerator interface analysis. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
/ * Syntax: (C #) * / [ComVisibleAttribute (true)] [GuidAttribute ("496B0ABF-CDEE-11d3-88E8-00902754C43A")] public interface IEnumerator
Note:
/ * IEnumerator is the base interface for all non-generic enumerators. The foreach statement of the C # language (foreach in Visual Basic) hides the complexity of enumerators. Therefore, it is recommended that you foreach instead of directly manipulating enumerators. Enumerators can be used to read data in the collection, but cannot be used to modify the underlying set. Initially, the enumerator is positioned before the first element in the collection. The Reset method also returns the enumerator to this location. At this location, calling the Current property throws an exception. Therefore, before reading the value of Current, you must call the MoveNext method to advance the enumerator to the first element of the collection. Current returns the same object before calling MoveNext or Reset. MoveNext sets Current to the next element. If MoveNext crosses the end of the collection, the enumerator is placed after the last element in the collection, and MoveNext returns false. When the enumerator is in this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returns false, calling Current throws an exception. To set Current as the first element of the collection again, you can call Reset and then MoveNext. As long as the collection remains unchanged, the enumerator remains valid. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is invalidated and unrecoverable, and the next call to MoveNext or Reset raises an InvalidOperationException. If you modify the collection between MoveNext and Current, Current will return the element it is set to, even if the enumerator is no longer valid. Enumerators do not have exclusive access to collections; therefore, enumerations through collections are not essentially a thread-safe process. Even if a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To ensure thread safety during enumeration, you can lock the collection throughout the enumeration or catch exceptions that are thrown due to changes made by other threads. , /
Example:
Using System;using System.Collections;//person class public class Person {public Person (string fName, string lName) {this.firstName = fName; this.lastName = lName;} public string firstName; public string lastName;} / / People class, implement IEnumerablepublic class People: IEnumerable {private Person [] _ people; public People (Person [] pArray) {_ people = new Person [pArray.Length] For (int I = 0; I < pArray.Length; iTunes +) {_ people [I] = pArray [I];}} public IEnumerator GetEnumerator () {return new PeopleEnum (_ people);}} / / implement IEnumeratorpublic class PeopleEnum: IEnumerator {public Person [] _ people; / / Enumerators are positioned before the first element / / until the first MoveNext () call. Int position =-1; public PeopleEnum (Person [] list) {_ people = list;} public bool MoveNext () {position++; return (position < _ people.Length);} public void Reset () {position =-1 } public object Current {get {try {return _ people [position];} catch (IndexOutOfRangeException) {throw new InvalidOperationException () } / / appclass App {static void Main () {Person [] peopleArray = new Person [3] {new Person ("John", "Smith"), new Person ("Jim", "Johnson"), new Person ("Sue", "Rabon"),}; People peopleList = new People (peopleArray) Foreach (Person p in peopleList) Console.WriteLine (p.firstName + "" + p.lastName);}} / * This code produces output similar to the following: * * John Smith * Jim Johnson * Sue Rabon * * / the above is the analysis of how to implement the IEnumerator interface shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.