Aug 15
An enumerator enables you to iterate over a collection in a foreach loop. You can use foreach to iterate over all C# collection classes, because all C# collection classes inherit from the IEnumerable interface (regular or generic). IEnumerable contains the GetEnumerator method, which returns an enumerator.
Occasionally you may find a need to create a custom enumerator, which used to be somewhat of a challenge until the yield keyword was introduced. Here is how Microsoft describes yield:
The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement.
So rather than creating your own enumerator class and managing the enumeration state — which is time consuming and tricky — you can simply write the enumeration logic in the GetEnumerator method, and the yield keyword will automagically wrap your code in a handy-dandy enumerator.
Read the rest of this entry »
Jan 27
To iterate over an IDictionary<x,y> interface, use the KeyValuePair<x,y> structure. Following is a simple example:
Read the rest of this entry »
Aug 14
How do you sort a C# array in descending or reverse order? A simple way is to sort the array in ascending order, then reverse it:
Read the rest of this entry »
Jun 10
This article provides C# code for an empty enumerator. This generic class can be used to simulate enumeration over an empty collection of any type of objects. Here is the code:
Read the rest of this entry »
May 28
It’s possible to provide multiple generic enumerators for a single class. The trick is that clients must specify which enumerator to use.
Read the rest of this entry »
May 06
It’s important to note that an enumerator does not have exclusive, thread-safe access to its collection. Even when a collection is synchronized, other threads can still modify the collection. Therefore, a collection’s contents can change while enumerating through it, which will cause the enumerator to throw an exception. So there are three key ways to safely enumerate a collection:
Read the rest of this entry »
Apr 29
Multi-threaded code is challenging to get right and even harder to debug once it’s gone wrong. This is especially true when attempting to collect data from multiple threads. To make this easier, many .NET collection classes include the SyncRoot property to maintain proper synchronization with other threads that might be simultaneously modifying the collection. But then Microsoft changed its mind in .NET 2.0 and decided to let the developer decide how to manage synchronization, and so none of the new generic collections have a SyncRoot property.
Read the rest of this entry »
Mar 18
It’s easy to reverse the contents of an array using C# generics:
Read the rest of this entry »
Mar 13
As discussed in a previous article, Generics provides the ability to create strongly-typed collections in C#. Unfortunately, C# currently does not support generics variance, which would allow inheritance of generic types.
Read the rest of this entry »
Feb 04
If you need a collection of objects accessed by a key, and the key is one of the object’s properties, then you should use the KeyedCollection class instead of Dictionary. For example, you would use a KeyedCollection to store Employee objects accessed by the employee’s ID property.
Read the rest of this entry »