Avançar para o conteúdo principal

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 of type ListOfClientsEnum, which will be used in the foreach loop.

In the ListOfClientsEnum class we define how the list elements will be enumerated.

Since we only want to go through the active clients we will implement the IEnumerator interface.


We define a list of clients and a variable to store the position of the current element.

The class constructor sets a reference to the client list and sets the current position to -1, that is, before the first element.

The IEnumerator interface is required to implement the MoveNext, Reset functions, and the Current property as follows.

The Current property returns the currently selected element according to the position property.
The MoveNext function, in our case, advances to the next element, if it does not exist returns false.
The Reset function sets the position back to -1.
In the main function we use the class like this:

Note that listOfClients is an object and yet is used in the foreach loop to traverse clients using our ListOfClientEnum class.

Link to the complete project.

Comentários

Mensagens populares deste blogue

Single Page App with C# WPF/XAML

 In this post we are going to create a single page app. The app will have multiple pages that get rendered in the main window. We will be using Visual Studio, C#, WPF and XAML. Let's start by creating a new project in Visual Studio of this type: Next, in the MainWindow, we define the interface structure. On the left side we place a menu and on the right side a DockPanel with a Frame in it. The Frame is the element that is used to render de pages content. Now let's add the new pages. In this example I will add two pages. Click in the Solution Explorer with the mouse right button, then choose Add and Page. The project looks like this. The app content goes on the recently create pages. Because this is just an example I will just change the background color and add a small text. Page1 Page2 Finally the code. Back to the MainWindow we need to create the click events on the menu items. So, in the MenuItem line add the click event and pick New Event Handler. If that option doesn't...

Saving pictures to an Access database

What is the best method for storing images in a server? That's a discussion that we will never see the end. In my opinion, has in everything in life, it depends. But in this post we will not contribute to that dilemma. The objective of this post is to show how to store the pictures in an Access database. We start by creating a new project in VS 2013. Next, we create a class that will manage the database. In this class we will need a reference to a DLL that is responsible for creating the database file. In the Reference Manager with search in the COM section.    Back in the class with add a namespace: using ADOX; Now let's add three properties to the class: the path to the database file, a connection string and an object to make the connection with the database. class BaseDados     {         string caminhoBD;         string strLigacao;         OleDbConnection ...

Upgrading Windows 10 Home to Pro

 So I have been thinking about upgrading my Windows 10 Home Edition to the Pro version, but I always get to the point where it seems that I had to reinstall the entire SO and quit. After some investigating I have done it this way: - following this post  on the microsoft site I use one of the default keys for Windows 10 Pro and went to Settings > Update & Security > Activation > Change the product key; - next, Windows will activate the Pro functionalities and asks to restart; - now you have the Pro version but it's not activated, so you have to buy a Windows Pro Key. I went to UR cdkeys  and bought a key for less then €20; - and with the new key went to Change the product key and activated; - and it's done. Disclaimer : I have nothing to do with UR cdkeys so you can use any site to buy you cd key and your experience may vary from mine.