1 min read

Initialize a dictionary with a collection initializer in C# Part 3 – Loop through the dictionary

With our dictionary of students created we can now loop through it to retrieve both Keys and values.

We will make use of the Enumerable.Range so make sure to add the Linq to your class.

 using System.Linq;

Now write the foreach loop.

foreach (var index in Enumerable.Range(100,2))
     Console.WriteLine($"Student {index} is {students[index].FirstName} {students[index].LastName}");

Now let’s run the app and see the result!

There you go! Wow! Great work today!

Leave a Reply