Jun 1, 2010

Yield keyword in C#

Yield Keyword in .net

Yield keyword is used to return a value in between the iteration of a loop.

Ex:-

public static IEnumerable<int> Square(int min, int max)

{

    for (int i = min; i <= max; i++)

    {

        yield return i*i;

    }

}

Now each time this method is called it will return the square of current value within a given range and it’s also maintains the state between calls.

foreach (int i in Square(1, 10))

{

    Response.Write(i.ToString() + " ");

}

output will be like 1,4,9,16,25,36,49,64,81,100.

 

 

2 comments:

  1. Generics in C#
    =================

    Generics are used to instead of writing a function or a class for a particular type, it can be written generally to use any type.

    Example 1 :
    ===========
    public class Col {
    T t;
    public T Val{get{return t;}set{t=value;}}
    }

    public class ColMain {
    public static void Main() {
    //create a string version of our generic class
    Col mystring = new Col();
    //set the value
    mystring.Val = "hello";

    //output that value
    System.Console.WriteLine(mystring.Val);
    //output the value's type
    System.Console.WriteLine(mystring.Val.GetType());

    //create another instance of our generic class, using a different type
    Col myint = new Col();
    //load the value
    myint.Val = 5;
    //output the value
    System.Console.WriteLine(myint.Val);
    //output the value's type
    System.Console.WriteLine(myint.Val.GetType());

    }
    }


    Example 2
    ==========

    User.cs


    namespace Rob {
    public class User {
    protected string name;
    protected int age;
    public string Name{get{return name;}set{name=value;}}
    public int Age{get{return age;}set{age=value;}}
    }
    }


    Main.cs

    public class M {
    public static void Main(string[] args) {
    System.Collections.Generic.List users = new System.Collections.Generic.List();
    for(int x=0;x<5;x++) {
    Rob.User user = new Rob.User();
    user.Name="Rob" + x;
    user.Age=x;
    users.Add(user);
    }

    foreach(Rob.User user in users) {
    System.Console.WriteLine(System.String.Format("{0}:{1}", user.Name, user.Age));
    }
    System.Console.WriteLine("press enter");
    System.Console.ReadLine();

    for(int x=0;x<users.Count;x++) {
    System.Console.WriteLine(System.String.Format("{0}:{1}", users[x].Name, users[x].Age));
    }
    }
    }

    ReplyDelete
  2. namespace Rob {
    public class User {
    protected string name;
    protected int age;
    public string Name{get{return name;}set{name=value;}}
    public int Age{get{return age;}set{age=value;}}
    }
    }


    System.Collections.Generic.List users
    = new System.Collections.Generic.List();


    for(int x=0;x<5;x++) {
    Rob.User user = new Rob.User();
    user.Name="Rob" + x;
    user.Age=x;
    users.Add(user);
    }


    foreach(Rob.User user in users) {
    System.Console.WriteLine(
    System.String.Format("{0}:{1}", user.Name, user.Age)
    );
    }

    ReplyDelete