what is diff between return and yield

Posted by Xaxx345 under C# on 7/4/2012 | Points: 10 | Views : 3818 | Status : [Member] | Replies : 9
hi

some one explain me what is diff between return and yield statments in C#?




Responses

Posted by: Kamalakanta.Nayak09 on: 7/4/2012 [Member] Starter | Points: 25

Up
0
Down
Through return, function can return single value but by using yield return, used for bulk value return.

Another case where yield-return is preferable is if the IEnumerable represents an infinite set. Consider the list of Prime Numbers, or an infinite list of random numbers. You can never return the full IEnumerable at once, so you use yield-return to return the list incrementally.

Thanks,
K.K

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Xaxx345 on: 7/4/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks, But I would like to get an example with code snippets. I googled but need some more practical scenarios...

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kamalakanta.Nayak09 on: 7/4/2012 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;

class ExampleYieldReturn
{
static void Main(string[] args)
{
//with every call it goes to where it left till it encounters yield return
foreach(int n in GetNumbers())
{
Console.WriteLine(n);
}

Console.ReadLine();
}

public static IEnumerable<int> GetNumbers()
{
Console.WriteLine("Print number 52");
yield return 52; // will go back to the caller ie foreach

Console.WriteLine("Print number 63");
yield return 63;// again will go back to the caller ie foreach

Console.WriteLine("Print number 72");
yield return 72;// again will go back to the caller ie foreach

}

}

Thanks,
K.K

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Premalatha on: 7/4/2012 [Member] Starter | Points: 25

Up
0
Down
The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement. The yield keyword is also used with break to signal the end of iteration

An iterator is a method, get accessor, or operator that performs a custom iteration over an array or collection class by using the yield keyword.
Example:

public class List
{
//using System.Collections;
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}

static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
}
/*
Output:
2 4 8 16 32 64 128 256
*/

Premalatha
Software Engineer

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 7/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi
Premalatha

Nice Explanation


R D Patel

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 7/24/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
hi
check this url
http://www.dotnetperls.com/yield

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 7/31/2012 [Member] Silver | Points: 25

Up
0
Down
http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/4dbe17cb-9da3-4f57-8a10-01e04f8a239b

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 7/31/2012 [Member] Silver | Points: 25

Up
0
Down
Please refer this http://www.codeproject.com/Articles/38097/The-Mystery-Behind-Yield-Return

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 7/31/2012 [Member] Silver | Points: 25

Up
0
Down
Mark as answer if it helps...,That motivates...!!!

Xaxx345, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response