Unfamiliar yield keywords

Source: Internet
Author: User

What is the function of the yield keyword? It is estimated that most people are confused like me before. I pay attention to him after preparing an interview question.

I checked Msdn's description about yield: In the iterator block, it is used to provide a value to the enumerated number object or send an iteration end signal.

It's still a fog. Let me take a look at it. yield appears in the loop body and returns the current operation result in each loop. yield is used with return or break to act as a flag of method output.

The difference between yield return and return:

 

For example:

 

Int n = 1;

Int m = 100;

While (n <100)

{

N + = n;

Yield return n; // result 2, 4, 8 ,......

}

 

Return indicates that the entire method is terminated and the current result is returned.

Yield return is an introduction to the current loop. It returns the current result and starts the next loop.

 

The Return Value of the method using the yield keyword is generally IEnumerable.

The execution process of the sample Msdn yield code.

In the following example, the yield statement is used in the iterator block (the method Power (int number, int power. When the Power method is called, it returns an enumerative object containing the number Power. Note that the return type of the Power method is IEnumerable (an iterator interface type ).

 

Using System;
Using System. Collections;
Public class List
{
Public static IEnumerable Power (int number, int exponent)
{
Int counter = 0;
Int result = 1; // <--------------- (2)
While (counter ++ <exponent) // <--------------- (3)
{
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) // <----------------- (1)
{
Console. Write ("{0}", I); // <--------------- (4)
}
}
}

 

Returned results: 2 4 8 16 32 64 128 256.

 

First, the program runs to (1), jumps to the Power method, runs to (2), enters (3) for the first time, returns the result to (4) output, (1) starts from (3) and returns result (4.

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.