Dictionary in C#-1
Dictionary in C#-1
C# Corner Login
Dictionary In C#
Mahesh Chand Aug 25, 2023 1.2m 20 52
A C# dictionary is a generic collection of key-value pairs. The keys must be unique, but the values can be
duplicated. Dictionaries are implemented as hash tables so their keys can quickly access them.
To create a dictionary in C#, you use the Dictionary<TKey, TValue> class, where TKey is the type of the
keys, and TValue is the type of the values. For example, the following code creates a dictionary that can
store strings as keys and integers as values:
You can add key-value pairs to a dictionary using the Add() method. For example, the following code adds
two key-value pairs to the dictionary:
1 dictionary.Add("one", 1);
2 dictionary.Add("two", 2);
You can retrieve a value from a dictionary by its key using the Get() method. For example, the following
code retrieves the value associated with the key "one":
You can also iterate over the keys or values in a dictionary using the Keys() or Values() methods. For
example, the following code iterates over the keys in the dictionary and prints them to the console:
They can be slow for iterating over all the key-value pairs.
They can be memory-intensive if they store a large number of key-value pairs.
Here are some examples of when you might use a dictionary in C#:
C# Dictionary class is a generic collection of keys and values pair of data. The Dictionary class is defined in
the System.Collections.A generic namespace is a class that can store any data type in keys and values.
Each key must be unique in the collection.
Before you use the Dictionary class in your code, you must import the System.Collections.Generic
namespace using the following line.
1 using System.Collections.Generic;
The following Dictionary class is a generic class and can store any data type. This class is defined in the
code snippet and creates a dictionary where both keys and values are string types.
The following code snippet creates a dictionary where the key type is a string, and the value type is a short
integer.
We can also limit the size of a dictionary. The following code snippet creates a dictionary where the key
type is a string, the value type is float, and the total number of items it can hold is 3.
1 PriceList.Add("Tea", 3.25f);
2 PriceList.Add("Juice", 2.76f);
3 PriceList.Add("Milk", 1.15f);
The following code snippet creates a new dictionary, reads all its items, and displays them on the console.
15 }
16 }
C# Dictionary Properties
The Dictionary class has three properties – Count, Keys, and Values.
The following code snippet sets and gets the value of an item.
C# Dictionary Methods
The Dictionary class is a generic collection and provides all common methods to add, remove, find, and
replace items in the collection.
The following code snippet creates a Dictionary and adds an item using the Add method.
Alternatively, we can use the Item property. A new item is added if the key is not in the collection. If the
same key already exists in the collection, the item value is updated to the new value.
The following code snippet adds an item and updates the existing item in the collection.
1 AuthorList["Neel Beniwal"] = 9;
2 AuthorList["Mahesh Chand"] = 20;
The Clear method removes all items from the collection. The following code snippet removes all items by
calling the Clear method.
1 if (!AuthorList.ContainsKey("Mahesh Chand"))
2 {
3 AuthorList["Mahesh Chand"] = 20;
4 }
1 if (!AuthorList.ContainsValue(9))
2 {
3 Console.WriteLine("Item found");
4 }
Summary
In this tutorial, we learned about the basic use of a dictionary using C# language. We also learned how to
create a dictionary using the C# and .NET Dictionary class. After that, we learned how to use various
methods and properties of the class.
C# Dictionary Dictionary in C#
FileInfo in C#
Download Now!
SIMILAR ARTICLES