What is the yield keyword used for in C#?

后端 未结 17 1573
盖世英雄少女心
盖世英雄少女心 2020-11-22 05:26

In the How Can I Expose Only a Fragment of IList<> question one of the answers had the following code snippet:

IEnumerable FilteredList()
{
         


        
                      
相关标签:
17条回答
  • 2020-11-22 05:38

    Iteration. It creates a state machine "under the covers" that remembers where you were on each additional cycle of the function and picks up from there.

    0 讨论(0)
  • 2020-11-22 05:42

    This link has a simple example

    Even simpler examples are here

    public static IEnumerable<int> testYieldb()
    {
        for(int i=0;i<3;i++) yield return 4;
    }
    

    Notice that yield return won't return from the method. You can even put a WriteLine after the yield return

    The above produces an IEnumerable of 4 ints 4,4,4,4

    Here with a WriteLine. Will add 4 to the list, print abc, then add 4 to the list, then complete the method and so really return from the method(once the method has completed, as would happen with a procedure without a return). But this would have a value, an IEnumerable list of ints, that it returns on completion.

    public static IEnumerable<int> testYieldb()
    {
        yield return 4;
        console.WriteLine("abc");
        yield return 4;
    }
    

    Notice also that when you use yield, what you are returning is not of the same type as the function. It's of the type of an element within the IEnumerable list.

    You use yield with the method's return type as IEnumerable. If the method's return type is int or List<int> and you use yield, then it won't compile. You can use IEnumerable method return type without yield but it seems maybe you can't use yield without IEnumerable method return type.

    And to get it to execute you have to call it in a special way.

    static void Main(string[] args)
    {
        testA();
        Console.Write("try again. the above won't execute any of the function!\n");
    
        foreach (var x in testA()) { }
    
    
        Console.ReadLine();
    }
    
    
    
    // static List<int> testA()
    static IEnumerable<int> testA()
    {
        Console.WriteLine("asdfa");
        yield return 1;
        Console.WriteLine("asdf");
    }
    
    0 讨论(0)
  • 2020-11-22 05:45

    Intuitively, the keyword returns a value from the function without leaving it, i.e. in your code example it returns the current item value and then resumes the loop. More formally, it is used by the compiler to generate code for an iterator. Iterators are functions that return IEnumerable objects. The MSDN has several articles about them.

    0 讨论(0)
  • 2020-11-22 05:48

    It is a very simple and easy way to create an enumerable for your object. The compiler creates a class that wraps your method and that implements, in this case, IEnumerable<object>. Without the yield keyword, you'd have to create an object that implements IEnumerable<object>.

    0 讨论(0)
  • 2020-11-22 05:48

    One major point about Yield keyword is Lazy Execution. Now what I mean by Lazy Execution is to execute when needed. A better way to put it is by giving an example

    Example: Not using Yield i.e. No Lazy Execution.

    public static IEnumerable<int> CreateCollectionWithList()
    {
        var list =  new List<int>();
        list.Add(10);
        list.Add(0);
        list.Add(1);
        list.Add(2);
        list.Add(20);
    
        return list;
    }
    

    Example: using Yield i.e. Lazy Execution.

    public static IEnumerable<int> CreateCollectionWithYield()
    {
        yield return 10;
        for (int i = 0; i < 3; i++) 
        {
            yield return i;
        }
    
        yield return 20;
    }
    

    Now when I call both methods.

    var listItems = CreateCollectionWithList();
    var yieldedItems = CreateCollectionWithYield();
    

    you will notice listItems will have a 5 items inside it (hover your mouse on listItems while debugging). Whereas yieldItems will just have a reference to the method and not the items. That means it has not executed the process of getting items inside the method. A very efficient a way of getting data only when needed. Actual implementation of yield can seen in ORM like Entity Framework and NHibernate etc.

    0 讨论(0)
  • 2020-11-22 05:51

    Here is a simple way to understand the concept: The basic idea is, if you want a collection that you can use "foreach" on, but gathering the items into the collection is expensive for some reason (like querying them out of a database), AND you will often not need the entire collection, then you create a function that builds the collection one item at a time and yields it back to the consumer (who can then terminate the collection effort early).

    Think of it this way: You go to the meat counter and want to buy a pound of sliced ham. The butcher takes a 10-pound ham to the back, puts it on the slicer machine, slices the whole thing, then brings the pile of slices back to you and measures out a pound of it. (OLD way). With yield, the butcher brings the slicer machine to the counter, and starts slicing and "yielding" each slice onto the scale until it measures 1-pound, then wraps it for you and you're done. The Old Way may be better for the butcher (lets him organize his machinery the way he likes), but the New Way is clearly more efficient in most cases for the consumer.

    0 讨论(0)
提交回复
热议问题