Home Common Problem What is the core principle of yield?

What is the core principle of yield?

Jul 01, 2020 pm 03:34 PM
yield

The core principle of yield is that it is used in the iterator block to provide values ​​to the enumerator object or to signal the end of iteration. Its statements can only appear in the iterator block, which can be used as a method, operator or The body of the accessor.

What is the core principle of yield?

The core principle of yield is:

is used in the iterator block to add the enumerator object Provide a value or signal the end of the iteration.

Its form is one of the following:

  • Copy codeyield return <expression>;yield break;

  • Remarks Calculates expressions and returns them in the form of enumerator object values;

  • expression must be implicitly convertible to iteration The yield type of the device.

The yield statement can only appear within an iterator block, which can be used as the body of a method, operator, or accessor.

The body of such a method, operator, or accessor is governed by the following constraints:

  • Unsafe blocks are not allowed.

  • Parameters to methods, operators, or accessors cannot be ref or out.

  • The yield statement cannot appear in an anonymous method.

When used with expression, a yield return statement cannot appear in a catch block or in a try block containing one or more catch clauses. Example In the following example, the yield statement is used in an iterator block (here the method Power(int number, int power)). When the Power method is called, it returns an enumerable object containing numbers raised to powers.

Note that the return type of the Power method is IEnumerable (an iterator interface type).

 yield-example.csusing 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);        }    }}
Copy after login

The above is the detailed content of What is the core principle of yield?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)