Jul
25
2009
.NET // C# // Microsoft

Efficient iterations using the yield keyword

When working with collections there's always the question which would be the fastest way to do it. Well C# has come a long way since it was introduces in 2001. C# 2.0 added the yield retun statement. Microsoft's definition is: "Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration."

It allows for a very efficient way to modify and return collections. For example if you need to retrieve the distinct values of a string collection you could do this?

public IEnumerable<string> Distinct(IEnumerable<string> values)
        {
            Dictionary<string,string> distinctValues = new Dictionary<string,string>();
            foreach(string s in values)
            {
               if(!distinctValues.ContainsKey(s))
               {
                  distinctValues.Add(s,s);
                  yield return s;
               }
            }
        }

Now we can test this here.

string[] ss = new string[] { "one", "two", "three", "one", "two", "three" };
            foreach (string s in Distinct(ss))
                Console.WriteLine(s);

Now the reason this iterator method is much more efficient is that it executes the code to produce each element when that element is requested and not before. It also internally keeps track of the position it has reached. Thus it uses less storage space and composes better. This is particulary effective when there is the need to examine and modify multiple nested sequences.

Month List