Avançar para o conteúdo principal

Mensagens

A mostrar mensagens de setembro, 2019

C# IEnumerator and IEnumerable interfaces

In this article we will learn how to use the IEnumerator interface to allow you to use a foreach loop in a data set or collection. Most collections (lists and others) already implement the interface, but in this case we will customize the way we browse through the list. When we use code like this: foreach(Class c in Collection) { ... } The compiler convertes this code in something like this: IEnumerator cc = Collection.GetEnumerator() while(cc.MoveNext()) { c=(Class)cc.Current; ... } Implementing the IEnumerable interface means that the class implements a version of the GetEnumerator () function that must return a class that implements the IEnumerator interface. Let's explore an example. We start with the client class: This class will allow you to store customer data, and there is a field to indicate whether the customer is still active or not. Next is a class that defines a client list and implements the IEnumerable interface that returns an object