Thursday, 10 October 2013

What is use of Yield keyword in C#

Yield may be a keyword that is capable in get list of things from a loop. the function implementing yield keyword returns associate numerable object.Yield retains the state of the tactic throughout the multiple calls. meaning yield returns one component to the business perform and pause the execution of this perform. Next time it'll resume the execution from that time solely and come consequent component. it's a reliable and additional economical thanks to get an inventory of values from a perform. this is often the keyword provided in C#.net 2.0 onward.Yield makes the developer life additional easier. The compiler once compiles the code containing the yield keyword, creates a decent quantity  of IL(Intermediate Language) code. primarily we have a tendency to don't have to be compelled to worry regarding these codes, that's the headache of the compiler.


private void MainProcess()
{
    string[] nameList = GetNames();
    foreach(string name in nameList)
    {
        //do the processing here.
    }
}

private string[] GetNames()
{
    List<string> names = new List<string>();

    for (int i = 0; i < 10; i++)
    {
        names.Add("Name:" + i);
    }

    return names.ToArray();
}

The following code shows the way Yield implements the same thing.

private void MainProcess()
{
    foreach(string name in GetNames())
    {
        //do the processing here.
    }
}

private IEnumerable GetNames()
{
    for (int i = 0; i < 10; i++)
    {
        yield return "Name:" + i;
    }
}



Here, the implementation just in case of Yield is straightforward and undemanding. The GetNames() methodology iterates through the for loop and returns a price every time it's referred to as. The yield come is completely different from the come keyword. wherever the come keyword stops the execution of {the methodology|the tactic|the strategy} and returns the management to the business method, yield come pauses the execution of {the methodology|the tactic|the strategy} and returns the management to the business method. consequent time once we decision the GetNames() methodology the execution resumes from the purpose wherever it left last time.

The yield keyword will take any of the subsequent 2 forms:
- yield come ;
- yield break;

Where yield come  returns the values to the business methodology and paused the execution, yield break ends the execution and for good returns the management to the business perform.

There area unit few restrictions whereas implementing the yield keyword. These are:
- The yield statement will solely seem within associate iterator block.
- Unsafe blocks don't seem to be allowed.
- Parameters to the tactic, operator, or accessor can not be referee or out.
- A yield statement cannot seem in associate anonymous methodology.
- Keep the enumeration code as easy as you'll as a result of debugging it subsequently is sort of not possible.

Initially Yield could feels like bit confusing however once you begin implementing it you'll notice it terribly fascinating and powerful.

0 comments:

Post a Comment