- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the best way to iterate over a Dictionary in C#?
In a Dictionary collection, store any data type. Dictionary is a collection of keys and values in C#. Dictionary<TKey, TValue> is included in the System.Collection.Generics namespace.
Let us now see the best way to iterate over a Dictionary in C# −
Firstly, let us create a dictionary −
var d = new Dictionary<string, int>(5);
Now add the key and value −
// add key and value d.Add("car", 25); d.Add("bus", 28); d.Add("motorbike", 17);
Use orderby to order by values −
var val = from ele in d orderby ele.Value ascending select ele;
We have set ascending above to sort the dictionary in ascending order. You can also use descending.
Display the values in ascending order −
foreach (KeyValuePair<string, int> ele in val) { Console.WriteLine("{0} = {1}", ele.Key, ele.Value); }
- Related Articles
- How to iterate over a C# dictionary?
- Iterate over a dictionary in Python
- What is the best way to remove an item from a Python dictionary?
- How to iterate over a C# list?
- How to iterate over a C# tuple?
- What is the best way to initialize a JavaScript number?
- What is the best way to log a Python exception?
- What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?
- What is the best way to check capacity in Java?
- What is the best way to concatenate strings in JavaScript?
- What is the best way to stop Brain Drain?
- What is the best way to earn money online?
- What is the best way to convert a number to a string in JavaScript?
- What is the best way to initialize a JavaScript Date to midnight?
- What is the best way to show data in a table in Tkinter?

Advertisements