Mohamed Sherif

Mohamed Sherif

  • NA
  • 15
  • 99.2k

IEnumerable Vs List

Jul 15 2013 8:41 AM
List<Animals> li = new List<Animals>();
            Animals n = new Animals();
                n.Cat = 1;
                n.Dog = 2;
                n.tiger = 3;
                li.Add(n);
            Animals n1 = new Animals();
                n1.Cat = 2;
                n1.Dog = 2;
                n1.tiger = 3;
                li.Add(n1);
            IEnumerable<Animals> test= li;
            
            foreach (var item in test)
            {
                Console.WriteLine(item.Cat);
            }
            foreach (var item in li)
            {
                Console.WriteLine(item.Cat);
            }
            Console.Read();

What is the difference between Ienumerable and list.List implements the interface Ienumerable.so it Includes all methods of IEnumerable.
Where should I use IEnumerable<> And List<> respectively.
Please Explain me the difference with an example.  

Answers (3)