========================================================== ============================
Yield
VaR q = getnums (5 );
Public static ienumerable getnums (INT Nums)
{
Int result = 0;
For (INT I = 0; I <Nums; I ++)
{
Result = I;
Yield return result;
}
}
Result output numbers 0, 1, 2, 3, and 4
Msdn
YieldThe keyword indicates to the compiler that its method is an iterator block. The compiler generates a class to implement the behavior represented in the iterator block. In the iterator block,YieldKeyword andReturnKeyword used in combination to provide a value to the enumerated object. This is a return value, for example, inForeachThe value returned in each loop of the statement. YieldThe keyword can also beBreakCombined Use, indicates that the iteration ends. For more information about the iterator, seeIterator (C # programming guide). The following example shows two forms of yield statements.
Yield return <expression>; yield break;
========================================================== ================================
yield keyword signals to the compiler that the method in which it appears is an iterator block. "jquery1710451054870190338 =" 1 "> ref yield keyword signals to the compiler that the method in which it appears is an iterator block. "jquery1710451054870190338 =" 1 "> off yield keyword signals to the compiler that the method in which it appears is an iterator block. "jquery1710451054870190338 =" 1 "> key word
static void method ( ref int I)
{< br> // rest the mouse pointer over I to verify that it is an int.
// The following statement wocould cause a compiler error if I
// were boxed as an object.
I = I + 44;
}
Static VoidMain ()
{
IntVal = 1;
Method (RefVal );
Console. writeline (VAL );
// Output: 45
}
========================================================== ========================================================== ===
OUT OffKey word
static void method ( out int I)
{< br> I = 44;
}< br> static void main ()
{< br> int value;
method ( out value );
// value is now 44
}