Can you help me in understanding of yield
keyword in asp .NET(C#)
.
Yield return automatically creates an enumerator for you.
http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx
So you can do something like
//pseudo code:
while(get_next_record_from_database)
{
yield return your_next_record;
}
It allows you to quickly create an object collection (an Enumerator) that you can loop through and retrieve records. The yield return statement handles all the of the code needed to create an enumerator for you.
The big part of the yield return statement is that you don't have to load all the of the items in a collection before returning the collection to the calling method. It allows lazy loading of the collection, so you don't pay the access penalty all at once.
When to use Yield Return.