Wednesday, January 22, 2020

Tricky C# interview question

I had been asked to trace the following code for the output

  class Program
    {
        public delegate void Iterator();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            List<Iterator> interators = new List<Iterator>();
            for (int i = 0; i < 15; i++)
            {
                interators.Add(delegate { Console.WriteLine(i); });
            }

            foreach (var interator in interators)
            {
                interator();
            }
            Console.Read();
        }
    }

Do you like to try it?
 


No comments:

Post a Comment