C# Map Example
A map is also known as a dictionary or associative array in other programming languages. C# has no built-in Map type. In C#, a map is typically implemented as a dictionary (or more precisely, an instance of the Dictionary
The concept of a C# map or dictionary is to provide a way to map a value to a key inside a collection. In a dictionary or map, every element in the collection is stored as a key-value pair, where each key maps to a corresponding value. The primary advantage of using a map or dictionary is that it provides an efficient way to retrieve values based on their keys. By using a key to look up a value, you can quickly access the associated data without having to search through the entire collection.
C# Map/Dictionary
Following is an example of how to create and use a map/dictionary in C#:
In the above example, create a map called myMap using the Dictionary class. Then add three key-value pairs to the map, with "red", "green", and "blue" as keys, and 100, 200, and 300 as values, respectively.
Access C# map/dictionary values
You can access a value from the map using its key, like this:
You can update a value in the map by assigning a new value to its key:
You can remove a key-value pair from the map using the Remove method:
How to iterate Map/dictionary in C#
Finally, you can iterate over the keys and values in the map using a foreach loop:
- Advantages of C#
- C# vs. Java: What Are Its Main Differences
- Advantages of C# over Python
- First C# Program | Hello World
- Difference between Console.Write and Console.WriteLine in C#
- How do I create a MessageBox in C#?
- C# Comments
- How to reverse a string in C#
- Palindrome in C# with Examples
- Fibonacci Series Program in C# with Examples
- C# Program to Print Number Triangle
- Get Integer Input from User in C# Console Application
- C# StringBuilder | Learn To Use C# StringBuilder Class
- C# HashMap (Dictionary)
- Ternary Operator (? :) in C# with Examples
- How To Sort Datatable in C#
- Struct Vs Class in C# | Differencees and Advantages
- Async And Await In C#
- IEnumerable in C# | Examples
- ref Vs out in C#
- How to remove item from list in C#?
- How to Add Items to a C# List
- C# StreamWriter Examples
- StreamReader in C# |Examples
- Static Method In C# | Examples
- How to convert an Enum to a String in C#
- How to convert a string to an enum in C#
- How to filter a list in C#?