C # Guan jianzi

Source: Internet
Author: User

========================================================== ============================

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
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.