PDA

Click to See Complete Forum and Search --> : Yeild ??


venerable bede
Nov 13th, 2006, 07:50 AM
Can anyone tell me what this yield keyword means in the .net framework 2.0 ?

I'm scared !

penagate
Nov 13th, 2006, 08:03 AM
Yield is used to simplify implementing implementing iterators.

Pre C# 2.0 you would need to implement both IEnumerable on your class and IEnumerator on a custom enumerator object. Since this is tedious and repetetive, in C# 2.0 you can make an "iterator block" which encapsulates the iteration process.

Using "yield" within a loop in your iterator block "pauses" the loop and returns a value. Movement within the loop itself is managed for you. In effect it's a sort of declarative form of IEnumerator.

There is an example on the MSDN page for "yield".
http://msdn2.microsoft.com/en-us/library/9k7k7cf0.aspx