Welcome to another edition of the C#.NET article series in the Digit magazine. We are back after a break of 2 months. Due to some unavoidable reasons I couldn’t provide an article to the September 2009 edition of the Digit magazine. I am sure that would have given you ample time to do more work on C#.NET and I am sure you would have gathered a good knowledge by now. During the August Edition, we discussed about the use of the Array class which is available in the C#.NET and we discussed it using some code segments as well. It also included some of the methods available under the Array class in C#.NET.
In this article we would like to concentrate on Iteraters and Collections, which are very important programming idioms and are heavily used. Here as usual we would discus some theoretical aspects would underline it using some code segments.
What are IEnumerable<T>, IEnumerator<T>, IEnumerable, and IEnumerator?
You’ve seen how you can use the C# foreach statement to conveniently iterate over a collection of objects, including a System.Array, ArrayList, List<T>, and so on. How does this work? The answer is that each collection that expects to work with foreach must implement the Enumerable<T> or IEnumerable interface that foreach uses to obtain an object that knows how to enumerate, or iterate over, the items in the collection. The iterator object obtained from IEnumerable<T> must implement the IEnumerator<T> or IEnumerator interface. Generic collection types typically implement IEnumerable<T>, and the enumerator object implements IEnumerator<T>. IEnumerable<T> derives from IEnumerable, and IEnumerator<T> derives from IEnumerator. This allows you to use generic collections in places where nongeneric collections are used. Strictly speaking, your collection types are not required to implement enumerators, and users can iterate through the collection using a ‘for’ loop if you provide an index operator by implementing IList<T>, for example. However, you won’t make many friends that way, and once I show you how easy it is to create enumerators using iterator blocks, you’ll see that it’s a piece of cake to implement IEnumerable<T> and IEnumerator<T>.
If things are a bit complicated right now, let’s clarify it through some examples.
Figure 1: Iterate through an array through ‘foreach’
In the rest of this section, I’ll quickly go over the salient points of creating enumerators from scratch, and I’ll quickly transition to how to create enumerators the new and improved way using iterator blocks. The IEnumerable<T> interface exists so that clients have a well-defined way to obtain an enumerator on the collection.
Figure 2: the IEnumerable<T> and IEnumerable interfaces
This above snippet shows IEnumerable interface, interface is a collection of methods which are not implemented. And are in an abstract state.
In the next example we are going to consider how to use the above mentioned classes. Here let’s build some employee objects using the employee class and try to iterate through them using ‘IEnumerable<T> and IEnumerable interfaces’.
In this code we have written an ‘Employee’ class which would display the basin information of an Employee. It has 2 fields ‘Employee Name’ and ‘Employee Number’
- Next we would describe a department class which would include the department the employees would belong to. Here we would write implement the ‘IEnumerable<T> interface,
Figure 4: Department class which the employees belong to
- A List is there which would add new class objects. ‘GetEnumerator’ method from the ‘IEnumerable<T> interface is there to iterate through the List to get the stored employee objects.
- ‘addEmployee’ method would add new employee objects to the list.
Figure 5: Adding the employee objects to the List and retrieve them
In the above snippet we are adding each ‘Employee’ to the list and retrieve them and display them. For that we are using a ‘foreach’ statement. Read more about these interfaces and methods to get a broader view.
In the next article we would concentrate more on generics as this a very important aspect in C# coding.