Friday 1 November 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...