here yield is doing simple thing like if name is bob then it will return the name to its calling environment....what special about it.we can write the same thing in another way like
IList<string> FindBobs(IEnumerable<string> names)
{
var bobs = new List<string>();
foreach(var currName in names)
{
if(currName == "Bob")
bobs.Add(currName);
}
return bobs;
}
so please guide me what is the difference of two above approach....and what is the importance of yield. it would be better if some one make me understand the usage of yield with few easy sample code. thanks
The way it was described to me is yield is like rasing an enumeration event. The following code for example will flatten out a control collection hierarchy and allow you to work with it like a flat array:
public static IEnumerable<Control> All(this ControlCollection controls)
{
foreach (Control control in controls)
{
//this part is important
foreach (Control grandChild in control.Controls.All())
yield return grandChild;
yield return control;
}
}
This will allow you to create a loop that will display all controls in a page:
foreach(Control c in Page.Controls.All())
{
Debug.Print(c.Id);
}
They are a pretty useful construct, but they are not always necessary. They are used more when you do not control how the enumeration will be initiated, but you want to provide an implementation of an enumeration.
Anthony Terra
Marked as answer by mou_inn on Apr 17, 2012 07:34 AM
mou_inn
Participant
778 Points
952 Posts
what is the actual use of yield keyword
Apr 15, 2012 07:55 PM|LINK
i have seen many time this keyword yield but do not understand it's use. what it does and when to use it. here is sample code for yield.
IEnumerable<string> FindBobs(IEnumerable<string> names)
{
foreach(var currName in names)
{
if(currName == "Bob")
yield return currName;
}
}
here yield is doing simple thing like if name is bob then it will return the name to its calling environment....what special about it.we can write the same thing in another way like
IList<string> FindBobs(IEnumerable<string> names)
{
var bobs = new List<string>();
foreach(var currName in names)
{
if(currName == "Bob")
bobs.Add(currName);
}
return bobs;
}
so please guide me what is the difference of two above approach....and what is the importance of yield. it would be better if some one make me understand the usage of yield with few easy sample code. thanks
aterra
Participant
910 Points
162 Posts
Re: what is the actual use of yield keyword
Apr 15, 2012 09:38 PM|LINK
The way it was described to me is yield is like rasing an enumeration event. The following code for example will flatten out a control collection hierarchy and allow you to work with it like a flat array:
public static IEnumerable<Control> All(this ControlCollection controls) { foreach (Control control in controls) { //this part is important foreach (Control grandChild in control.Controls.All()) yield return grandChild; yield return control; } }This will allow you to create a loop that will display all controls in a page:
foreach(Control c in Page.Controls.All()) { Debug.Print(c.Id); }They are a pretty useful construct, but they are not always necessary. They are used more when you do not control how the enumeration will be initiated, but you want to provide an implementation of an enumeration.
dr.Xis
Member
79 Points
83 Posts
Re: what is the actual use of yield keyword
Apr 15, 2012 10:54 PM|LINK
check out this it may help to understand
http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx