"Yield keyword"
I want to know more about the yield keyword?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 9, 2012, 6:56 PM EST
The yield statement can only appear inside an iterator block, which might be used as a body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:
Unsafe blocks are not allowed.
Parameters to the method, operator, or accessor cannot be ref or out.
using System;
using System.Collections;
public class List
{
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);
}
}
}
Refer
http://msdn.microsoft.com/en-us/library/9k7k7cf0%28v=vs.80%29.aspx
Thanks
Mukesh KumarRank #615 · 3.9K reputationPosted Jan 9, 2012, 7:08 PM EST
Read these links :
AartiRank #2,383 · 594 reputationPosted Jan 9, 2012, 7:18 PM EST
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. For more information about iterators.
1.In a yield return statement, expression is evaluated and returned as a value to the enumerator object; expression has to be implicitly convertible to the yield type of the iterator.
2.In a yield break statement, control is unconditionally returned to the caller of the iterator, which is either the IEnumerator.MoveNext method (or its generic System.Collections.Generic.IEnumerable(Of T) counterpart) or the Dispose method of the enumerator object.
3.The yield statement can only appear inside an iterator block, which can be implemented as the body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:
Unsafe blocks are not allowed.
Parameters to the method, operator, or accessor cannot be ref or out.
A yield return statement cannot be located anywhere inside a try-catch block. It can be located in a try block if the try block is followed by a finally block.
A yield break statement may be located in a try block or a catch block but not a finally block.
4. A yield statement cannot appear in an anonymous method.
5. When used with expression, a yield return statement cannot appear in a catch block or in a try block that has one or more catch clauses.
Example:
In the following example, the yield statement is used inside an iterator block, which is the method Power(int number, int power). When the Power method is invoked, it returns an enumerable object that contains the powers of a number. Notice that the return type of the Power method is System.Collections.IEnumerable, an iterator interface type.
public class PowersOf2
{
public static System.Collections.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
*/
VulpesRank #55 · 29.2K reputationPosted Jan 9, 2012, 11:36 PM EST
This means that it's only a keyword in the context of an iterator, when it's always used in conjunction with 'return' and 'break'.
In all other contexts, it can be used as a normal identifier.