Friday, November 1, 2019

using IEnumerable over List in C#

What's the advantages to use IEnumerable over List? For example I have a Employee class and a collection of employee objects, if I want to iterate each employee, I can either use:



List employees = new List() { new Employee(...),...}
foreach (Employee emp in employees)
{
Console.WriteLine(emp.Name);
}



or I can use Ienumerable as :



IEnumerable employees = new List() { new Employee(...),...}


then use foreach loop.



Both of them achieve same goal, so why we sometimes prefer to use IEnumerable over List?

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...