Freigeben über


IEnumerable is still great for lazy fetching

This isn't really a quiz per se, just a reiteration of how IEnumerable can be great for getting a pseudo-collection of something that you know will still change in the future and you want to include those updates.

This includes some generic type inferencing, and params usage, and Console.WindowWidth, but overall isn't really interesting in and of itself.

using System;

using System.Collections.Generic;

namespace IEnumerableChecking

{

    class Program

    {

        static void Main(string[] args)

        {

            List<string> strings1 = new List<string>(new string[] { "a1", "b1", "c1" });

            List<string> strings2 = new List<string>(new string[] { "a2", "b2", "c2" });

            List<string> strings3 = new List<string>(new string[] { "a3", "b3", "c3" });

            List<string> strings4 = new List<string>(new string[] { "a4", "b4", "c4" });

            IEnumerable<string> combination = Combine(strings1, strings2, strings3, strings4);

            List<string> combinationList = new List<string>(combination);

          PrintEntries(combination);

            PrintEntries(combinationList);

            strings3.Clear();

            PrintEntries(combination);

            PrintEntries(combinationList);

        }

        static void PrintEntries<T>(IEnumerable<T> entries)

  {

            Console.WriteLine(new String('-', Console.WindowWidth - 1));

            foreach (T item in entries)

            {

                Console.WriteLine(item.ToString());

            }

        }

        static IEnumerable<T> Combine<T>(params IEnumerable<T>[] enumerables)

        {

            foreach (IEnumerable<T> enumerable in enumerables)

            {

                foreach (T item in enumerable)

                {

                    yield return item;

                }

          }

        }

    }

}