C # in the "yield" use

yield is C # syntactic sugar traversal to simplify implementation, we want to know if you want to support a particular type traversing it is necessary to implement system interfaces IEnumerable, the follow-up to achieve this interface is cumbersome to write a lot of code to support real traversal function. for example

Copy the code
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace
{
    class Program
    {
        static void Main(string[] args)
        {
            HelloCollection helloCollection = new HelloCollection();
            foreach (string s in helloCollection)
            {
                Console.WriteLine(s);
            }

            Console.ReadKey();
        }
    }

    //
public class HelloCollection : IEnumerable
    //
{
    //
    public IEnumerator GetEnumerator()
    //
    {
    //
        yield return "Hello";
    //
        yield return "World";
    //
    }
    //}


    public class HelloCollection : IEnumerable
    {
        public IEnumerator GetEnumerator()
        {
            Enumerator enumerator = new Enumerator(0);
            return enumerator;
        }

        public class Enumerator : IEnumerator, IDisposable
        {
            private int state;
            private object current;

            public Enumerator(int state)
            {
                this.state = state;
            }

            public bool MoveNext()
            {
                switch (state)
                {
                    case 0:
                        current = "Hello";
                        state = 1;
                        return true;
                    case 1:
                        current = "World";
                        state = 2;
                        return true;
                    case 2:
                        break;
                }
                return false;
            }

            public void Reset()
            {
                throw new NotSupportedException();
            }

            public object Current
            {
                get { return current; }
            }

            public void Dispose()
            {
            }
        }
    }
}
Copy the code

 

    Section above comments cited "yield return", which is functionally equivalent to all of the code below! We can see some if not applicable yield requires a lot of code to support traversal operation.

    yield return represents a return to the next iteration in the iteration of data, in addition to yield break, which is expressed out of iteration, in order to understand the difference between the two examples we see the following

Copy the code
class A : IEnumerable
{
    private int[] array = new int[10];

    public IEnumerator GetEnumerator()
    {
        for (int i = 0; i < 10; i++)
        {
            yield return array[i];
        }
    }
}
Copy the code

 

    If you just want to let users access data ARRAY before 8, you can make the following modifications this time will be used yield break, modify the function as follows

Copy the code
public IEnumerator GetEnumerator()
{
    for (int i = 0; i < 10; i++)
    {
        if (i < 8)
        {
            yield return array[i];
        }
        else
        {
            yield break;
        }
    }
}
Copy the code

 

    In this way, it will only return to the previous eight data.

Guess you like

Origin www.cnblogs.com/lsgsanxiao/p/10974288.html