Giter VIP home page Giter VIP logo

yieldappcsharp's Introduction

YieldAppCSharp

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.

Example of using yield

public class Generators
{
    public static IEnumerable<int> GetPrimeNumbers()
    {
        int counter = 1;

        while (true)
        {
            if (IsPrimeNumber(counter))
            {
                yield return counter;
            }
            
            counter++;
        }
    }

    private static bool IsPrimeNumber(int value)
    {
        bool output = true;

        for (int i = 2; i < value / 2; i++)
        {
            if (value % i == 0)
            {
                output = false;
                break;
            }
        }
        
        return output;
    }
}

Requirements for the use of yield return

  • Do not put yield in an unsafe block.
  • Do not use ref or out keywords with the parameters of the method, operator or accessor (getter / setter).
  • Yield return can only be placed in the try block if it is followed by a finally block.
  • Yield break can be put in the try block and catch, but not placed in the finally block.
  • Do not use yield within the anonymous method.

Conclusion

One advantage of using yield is to help keep the code clean and concise. On top of that, because the yield is called within a method that uses IEnumerable there is no need to create or get a list of elements to browse. This applies in cases such as searching and browsing a number of elements required that will be reduce the dependency on the location of the element to find.

yieldappcsharp's People

Contributors

vgmda avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.