Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#
Article

List processing using Yield and Delegates

Rate me:
Please Sign up or sign in to vote.
3.55/5 (6 votes)
4 May 20053 min read 50.2K   395   17   12
Instead of iterating through each element in a collection, we can write iterators to do specific tasks.

Introduction

This article will describe how we can use iterators to process IEnumerable lists. This method is not used very often in imperative programming languages like C#, Java so it is interesting to see how it works. Since this code uses yield (iterators) as well as anonymous delegates, it will only run with C# 2.0.

A simple yield method (iterator)

C#
class PositiveNumbers : System.Collections.IEnumerable
{
  public System.Collections.IEnumerator GetEnumerator()
  {
    int a = 1;
    while (true)
    {
      yield return a;
      a = a + 1;
    }
  }
}
...
foreach (int num in new PositiveNumbers())
{
  .. Do something with each positive number ..
}

Instead of writing a class implementing the three IEnumerator methods, we can now in C# 2.0 write a single method using the yield terminology. This method can be accessed from outside using the IEnumerator methods, MoveNext, Reset, Current. (Note: yield does not support Reset).

So how does it work? The answer is quite simple actually. The first time the MoveNext method is called, our method runs from the beginning. The variable a is set to 1, we enter the while loop and it returns a which equals 1. The next time MoveNext is called, we continue with the statement after the yield statement, a=a+1. This increases a to 2, we loop and again run the yield return statement. This time however a is increased to two. The following code demonstrates this more clearly:

C#
public System.Collections.IEnumerator GetEnumerator()
{
  yield return "First element";
  yield return "Second element";
  yield return "Third element";
  yield return "Last element";
}

This yield method (iterator) represents a list that always contains four string elements.

Some syntactic sugar

A problem with the above approach is that we need one class for each iterator. Fortunately C# 2.0 provides a solution for this. If the yield statement is put into a method which is declared to return an IEnumerable, the compiler will do everything for you:

C#
class Iterators
{
  public static System.Collections.IEnumerable PositiveNumbers()
  {
    int a = 1;
    while (true)
    {
      yield return a;
      a = a + 1;
    }
  }
}

Not only does this mean that we can put several iterators in the same class, but also access each iterator by their class and method name instead of using the new statement:

C#
foreach (int num in IteratorsPositiveNumbers()){}

Finally, since our iterator only returns integers, we can optimize it by using the generic version of IEnumerable. It is a simple change in the method header that could both increase the execution speed as well as reduce the amount of casts needed.

C#
public static System.Collections.Generic.IEnumerable<INT> PositiveNumbers()

Cutting the infinite list short

The PositiveNumbers enumerator above runs forever. If we only want some of the PositiveNumbers, what should we do? The answer is to create another iterator. This iterator will have three arguments:

  • The index of the first element we want.
  • The index of the last element we want.
  • An IEnumerable instance containing the source list.
C#
public static System.Collections.IEnumerable SubList(int start, 
                               int end, IEnumerable enumerable)
{
  int count = 0;
  foreach (object o in enumerable)
  {
    count++;
    if (count < start)
      continue;
    else if (count <= end)
      yield return o;
    else
      yield break;
  }
}
...
//This gives us a list containing number 5 through 15
IEnumerable numbers =
  Iterators.SubList(5, 15, Iterators.PositiveNumbers()))

Our iterator simply skips elements in the list we take in the argument until we reach the 5th element. We then yield return until we get to the 16th element. There we call yield break. Calling yield break is the same as saying that our collection doesn't contain any more elements and it terminates the method.

Mapping

The final iterator that we will study in this article is named from a function originally found in the LISP language. What Map will do is to take a method and a list. It will return a list where the method has been applied on each element in that list:

C#
 public delegate object MapFunction(object o);

  /// <summary>
  /// Runs the function on each element in the enumerable list
  /// </summary>
  public static IEnumerable Map(MapFunction mapFunction, 
                                 IEnumerable enumerable)
  {
    foreach (object o in enumerable)
    {
      yield return mapFunction(o);
    }
  }
...
foreach (int num in
        I.Map(
        /* Multiply each element by 2 */
        delegate(object o){  return ((int)o) * 2; },
        I.SubList(5, 15, I.PositiveNumbers())))
    {
      Console.Write("{0} ", num); //Print number 10 12 14 .. 28 30
    }

The code was pretty simple to write. We simply apply the MapFunction to each object and then yield return the result.

Some final thoughts

While most of what has been written in this article can be implemented in other ways, it is still interesting to see how it works when approaching it from the side of functional programming. It is especially interesting to notice how we can work on a list containing all positive numbers while only generating elements on demand, making it possible to use infinite lists.

Credits

Many thanks to Kris Vandermotten for pointing to me the fact that IEnumerable functions may contain yield. This, while forcing me to rewrite most of the article, increased its readability significantly and reduced the amount of code needed.

History

  • 2005-04-29: Posted the original article.
  • 2005-04-29: Reduced a few lines of code and fixed a minor error.
  • 2005-05-04: Big change due to new knowledge about yield and its syntactic sugar.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDefinitely better after...yielding Pin
Mircea Puiu24-Jun-10 1:12
Mircea Puiu24-Jun-10 1:12 
GeneralA few comments... Pin
StealthyMark5-May-05 8:01
StealthyMark5-May-05 8:01 
GeneralRe: A few comments... Pin
Marcus Andrén5-May-05 12:25
Marcus Andrén5-May-05 12:25 
GeneralRe: A few comments... Pin
StealthyMark6-May-05 0:08
StealthyMark6-May-05 0:08 
GeneralRe: list index Pin
StealthyMark6-May-05 0:26
StealthyMark6-May-05 0:26 
GeneralRe: A few comments... Pin
Marcus Andrén5-May-05 14:31
Marcus Andrén5-May-05 14:31 
QuestionCan i do the Same on an Hashtable or a name value collection Pin
garapatiSridhar4-May-05 21:54
garapatiSridhar4-May-05 21:54 
AnswerRe: Can i do the Same on an Hashtable or a name value collection Pin
Marcus Andrén5-May-05 12:29
Marcus Andrén5-May-05 12:29 
GeneralSeveral iterators in a single class - the simple way Pin
Kris Vandermotten4-May-05 4:11
Kris Vandermotten4-May-05 4:11 
GeneralRe: Several iterators in a single class - the simple way Pin
Marcus Andrén4-May-05 5:50
Marcus Andrén4-May-05 5:50 
EDITED:

I had a whole reply written and posted that answered why I didn't do as you said and it was all correct, and then I went to look at the msdn link you provided. What you posted still isn't correct, but it did provide another way to use yield. Apparently, you can use yield with IEnumerable as well as IEnumerator. I will be editing my article to provide an updated version making use of that.

------

Here is my original reply:

The delegate wrapping is there specifically so that you can access the list using IEnumerable, not IEnumerator. IEnumerator doesn't represent a list, it only represents a single iteration over a list.

Using IEnumerable gives two important advantages. The first and simple one is that you can't use foreach on an IEnumerator. The second and most important reason is that if you access lists using IEnumerable they are immutable which is needed when you access the same list more than once.

This should demonstrate the problem

//Add object o to the list if and only if it doesn't already exist in the list
public IEnumerable AppendUnique(IEnumerable list,object o)
{
// This works ok
if(!Exists(list,o))
return Append(list,o);
}

public IEnumerator AppendUnique(IEnumerator list, object o)
{
if(!Exists(list,o))
{
//This won't work because the IEnumerator has already been used up when calling Exists.
return Append(list,o);
}
}

I do want to point out that using delegates isn't nescessary if you use one class for each IEnumerator you need. That is the basic way to do it and probably looks best if you only need a few of them.

About your second point regarding generics. I didn't consider boxing performance at all when writing the article. It just doesn't matter 99% of the time. You are correct that the cast syntax could be avoided in some cases where we don't use foreach, but since I was writing a more generic class, where IEnumerator could have different return arguments, I ..umm.. couldn't use generics. Smile | :)

Using a generic IEnumerable does present another compelling argument for sticking with a single IEnumerator per class.

Another interesting idea, using partial classes, is to put all the basic methods (Append, Rest, Map, etc.) in a partial class and write the application part in the same partial class in a different file. That way you wouldn't need to name the classname when calling them, much like I didn't do in the example I gave above.
GeneralRe: Several iterators in a single class - the simple way Pin
Kris Vandermotten4-May-05 6:30
Kris Vandermotten4-May-05 6:30 
GeneralRe: Several iterators in a single class - the simple way Pin
Marcus Andrén4-May-05 8:27
Marcus Andrén4-May-05 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.