示例#1
0
        //What is the use of yeild keyword
        public static void YieldKeywordExplanation()
        {
            // 1. yield keyword helps to do custom/statefull iteration over a collection
            // 2. When we use yield keyword, the control moves between source and caller.

            //Custom Iteration example without using temp collection
            Questions questions = new Questions();

            Console.Write("Number greater than 3 are:");
            foreach (int number in questions.Filter())
            {
                Console.Write(" {0} ", number);
            }

            //Statefull iteration example
            //For e.g, we want to display the running total of a list
            Console.WriteLine();
            Console.Write("Running total are:");
            foreach (int runningTotal in questions.RunningTotal())
            {
                Console.Write(" {0} ", runningTotal);
            }

            // 3. yield keyword effectively creates a lazy enumeration over collection items that can be much more efficient.
            // For example, yield return is a .NET sugar to return an IEnumerable with the only needed items.

            // Code without yield:
            //class MyItem
            //{
            //public MyItem() { }
            //static public IEnumerable CreateListOfItems()
            //{
            //    return new List {
            //    new MyItem(),
            //    new MyItem(),
            //    new MyItem() };
            //}
            //}

            //Same code using yield:
            //class MyItem
            //{
            //public MyItem() { }
            //static public IEnumerable CreateListOfItems()
            //{
            //yield return new MyItem();
            //yield return new MyItem();
            //yield return new MyItem();
            //}
            //}

            // The advantage of using yield is that if the function consuming your data simply needs the first item of the collection,
            // the rest of the items won’t be created so it’s more efficient.

            // 4. The yield operator allows the creation of items as it is demanded.That’s a good reason to use it.

            // 5. The declaration of an iterator must meet the following requirements:
            // 5.A. The return type must be IEnumerable, IEnumerable<T>, IEnumerator, or IEnumerator<T>.
            // 5.B. The declaration can't have any in ref or out parameters.

            // 6. The yield type of an iterator that returns IEnumerable or IEnumerator is object.
            // 7. If the iterator returns IEnumerable<T> or IEnumerator<T>, there must be an implicit conversion from the type of the expression in the yield return statement to the generic type parameter.

            // 8. You can't include a yield return or yield break statement in:
            // 8.A. Lambda expressions and anonymous methods.
            // 8.B. Methods that contain unsafe blocks.

            // 9. A yield return statement can't be located in a try-catch block. A yield return statement can be located in the try block of a try-finally statement.
            // 10. A yield break statement can be located in a try block or a catch block but not a finally block.
            // 11. If the foreach body(outside of the iterator method) throws an exception, a finally block in the iterator method is executed.
            // 12. yield keyword can be used in a get accessor that is an iterator.

            var theGalaxies = new Galaxies();

            foreach (Galaxy theGalaxy in theGalaxies.NextGalaxy)
            {
                Console.WriteLine("Galaxy name: {0} and Mega Light Year is: {1}", theGalaxy.Name, theGalaxy.MegaLightYears);
            }

            // 13. You can use a yield break statement to end the iteration.
        }
示例#2
0
 static void Main(string[] args)
 {
     Questions.PrintOrgEmployeeHirechy();
     Console.ReadKey();
 }