Avatar ·

How to iterate over all elements of a Dictionary in C#?

Hello colleagues! I just started learning C# and ran into this problem. I have a Dictionary object that stores student names and their grades. I need to output all key-value pairs to the console. I understand that a dictionary is not an array or a list, so a regular for loop with an index won't work here. I tried several options: I attempted to use foreach, but I don't know how to properly access the elements. I tried something like foreach (var item in myDictionary), but inside the loop I don't understand how to get the key and value separately. Here is an example of my code: ```csharp Dictionary students = new Dictionary { { "Anna", 85 }, { "Boris", 92 }, { "Victor", 78 } }; // How do I now output all students and their grades?