Avatar ·

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

Hello everyone! I just started learning C# and ran into a problem when working with dictionaries (Dictionary). I have a simple dictionary that stores student names and their grades:\n\n```csharp\nDictionary students = new Dictionary\n{\n { "Anna", 85 },\n { "Ivan", 92 },\n { "Maria", 78 }\n};\n```\n\nI want to display all students and their grades on the screen. I tried using a regular `for` loop by indices, like for an array or list, but the compiler gives an error — it turns out that Dictionary doesn't have an indexer by numeric index.\n\nI tried searching online, but found a bunch of different options: `foreach`, `for` with `ElementAt()`, LINQ queries. I'm completely confused. Which method is correct and most efficient for simply iterating over all key-value pairs? And how to