Yielding in C#

Here is a simply program I wrote to see how yielding works in C#.

Additional, these MSDN documentation and SO question might be helpful.

Classes (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/x9afc042.aspx

List(Of T) Class
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Yield(C# Reference)
http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx
Interesting Use Of The C# Yield Keyword In Nerd Dinner Tutorial
http://stackoverflow.com/questions/1971053/interesting-use-of-the-c-sharp-yield-keyword-in-nerd-dinner-tutorial

using System;
using System.Collections.Generic;

public class YieldTest{
    List<string> names;

    public YieldTest() {
        this.names = new List<string>();
        this.names.Add("Bit Diddler");
        this.names.Add("Dan Moore");
        this.names.Add("Joe Somebody");
    }

    public int getCount() { return this.names.Count; } // Current list count

    public string get( Int32 index ) {
    //  List getter
        if (index >= 0 && index < this.names.Count)
            return this.names[index];
        else
            return null;
    }

    public System.Collections.IEnumerable ForEachTest() {
    //  For Each Example
        System.Console.WriteLine("{0} ", "----------------------------------------------");
        System.Console.WriteLine("{0} ", "@YieldTest.ForEachTest()");
        System.Console.WriteLine("{0} ", "----------------------------------------------");

        foreach (string name in this.names){ yield return(name); }
        yield break;
    }

    public System.Collections.IEnumerable ForTest() {
    //  For Loop Example
        System.Console.WriteLine("{0} ", "----------------------------------------------");
        System.Console.WriteLine("{0} ", "@YieldTest.ForTest()");
        System.Console.WriteLine("{0} ", "----------------------------------------------");

        for (Int32 i = 0; i < this.names.Count; i++) { yield return (this.names[i]); }
        yield break;
    } 
} //!YieldTester


class Run{
 // Executes YieldTest code
    static void Main() {    
        YieldTest tester = new YieldTest();

    //  Basic For Loop Iteration Test
        System.Console.WriteLine("{0} ", "----------------------------------------------");
        System.Console.WriteLine("{0} ", "@Main - Basic For Loop");
        System.Console.WriteLine("{0} ", "----------------------------------------------");
        for ( Int32 i = 0; i < tester.getCount(); i++ )
            System.Console.WriteLine("{0} ", tester.get(i) );

    //  Foreach - For Loop Yield Test
        foreach ( string str in tester.ForTest() ) { System.Console.WriteLine("{0} ", str); }

    //  Foreach - Foreach Loop Yield Test
        foreach ( string str in tester.ForEachTest() ) { System.Console.WriteLine("{0} ", str); }
        
    } //  !Main()
} //  !Run
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s