Advertisement
Constructor | Description |
---|---|
Dictionary<T>() | This constructor creates an empty instance of the Dictionary<TKey, TValue> class by using the default initial capacity, hash code provider, and comparer. The capacity of a Dictionary<TKey, TValue> is the number of elements it can hold, as the new elements are added to the Dictionary<TKey, TValue>, its capacity is automatically increased. |
Dictionary<T>(int capacity) | This constructor initializes created a Dictionary<TKey, TValue> that is empty and has the specified initial capacity. |
Dictionary<T>(IDictionary<TKey, TValue>) | This constructor creates a Dictionary<T> initialized with the elements of an IDictionary<TKey, TValue> collection. |
Properties | Description |
---|---|
Capacity | This property is used to get or set the total number of elements a Dictionary<TKey, TValue> can hold. |
Count | This property gives total number of elements in a Dictionary<TKey, TValue>. |
IsFixedSize | This property determines whether a Dictionary<TKey, TValue> has a fixed size or not. |
IsReadOnly | This property determines whether a Dictionary<TKey, TValue> is read-only or not. |
IsSynchronzied | This property determines whether a Dictionary<TKey, TValue> is thread-safe or not. |
Item[Object key] | This property gets or sets a value associated with the specified key in theDictionary<TKey, TValue>. To use this property, you should not type Item, but you only need to specify the specific key within a pair of square brackets [ ] |
Keys | This property gets an ICollection<TKey> containing the keys of aDictionary<TKey, TValue>. |
Values | This property gets an ICollection<TValue containing the values of aDictionary<TKey, TValue>. |
Methods | Description |
---|---|
int Add(TKey key, TValue value) | This method adds the specified key-value pair in Dictionary<TKey, TValue>. |
bool Contains(TKey key) | This method checks if Dictionary<TKey, TValue> contains a specific key. |
bool ContainsKey(TKey key) | This method checks if Dictionary<TKey, TValue> contains a specific key. |
bool ContainsValue(TKey key) | This method checks if Dictionary<TKey, TValue> contains a specific value. |
void Clear() | This method removes all of the elements from the Dictionary<TKey, TValue>. |
int GetHash(TKey key) | This method returns the hash code for the specified key. |
Dictionary<TKey, TValue>.Enumerator GetEnumerator() | This method returns to enumerate the entire Dictionary<TKey, TValue>. |
void Remove(TKey key) | This method removes the element with the specified key in the Dictionary<TKey, TValue>. |
//C# Example of Dictionary<TKey, TValue>
using System;
using System.Collections.Generic;
class DictionaryDemo
{
public static void Main(String[] ar)
{
//Creating a Dictionary<TKey, TValue>
//Where, Keys are of type String
//And, Values are of type int
Dictionary<String, int> dict = new Dictionary<String, int>();
//Adding key-value pairs by the Add() method
dict.Add("Max", 1000);
dict.Add("John", 4000);
dict.Add("Tom", 2000);
dict.Add("Ana", 6000);
dict.Add("Rick", 5000);
//Getting a collection of the keys using property Keys
ICollection<String> c = dict.Keys;
// Use the keys to obtain the values.
foreach(String str in c)
Console.WriteLine(str + ": " + dict[str]);
//Using the property Item to retrieve the value of key "Tom"
//Remember the property Item is not used with its name
Console.WriteLine("Value at the key, Tom is "+ dict["Tom"]);
//Using the property Item to retrieve the value of key "Ana"
Console.WriteLine("Value at the key, Ana is "+ dict["Ana"]);
}
}
Max: 1000
John: 4000
Tom: 2000
Ana: 6000
Rick: 5000
Value at the key, Tom is 2000
Value at the key, Ana is 6000
Advertisement
//C# Checking if Dictionary<TKey, TValue> contains a specific key or a specific value
using System;
using System.Collections.Generic;
class DictionaryDemo
{
public static void Main(String[] ar)
{
//Creating a Dictionary<TKey, TValue>
//Where, Keys are of type String
//And, Values are of type int
Dictionary<String, int> dict = new Dictionary<String, int>();
//Adding key-value pairs by theAdd() method
dict.Add("Max", 1000);
dict.Add("John", 4000);
dict.Add("Tom", 2000);
dict.Add("Ana", 6000);
dict.Add("Rick", 5000);
//Getting a collection of the keys using property Keys
ICollection<String> c = dict.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The contents of Dictionary<TKey, TValue>:");
foreach(String str in c)
Console.WriteLine(str + ": " + dict[str]);
//Calling the method ContainsKey() to see if Dictionary<TKey, TValue> contains key "Ana"
Console.WriteLine("Does our Dictionary<TKey, TValue> contains the key, Tom: "+ dict.ContainsKey("Ana"));
//Calling the method ContainsKey() to see if Dictionary<TKey, TValue> contains key "Jack"
Console.WriteLine("Does our Dictionary<TKey, TValue> contains the key, Jack: "+ dict.ContainsKey("Jack"));
//Calling the method ContainsValue() to see if Dictionary<TKey, TValue> contains value 4000
Console.WriteLine("Does our Dictionary<TKey, TValue> contains the value, 4000: "+ dict.ContainsValue(4000));
//Calling the method ContainsValue() to see if Dictionary<TKey, TValue> contains value 10000
Console.WriteLine("Does our Dictionary<TKey, TValue> contains the value, 10000: "+ dict.ContainsValue(10000));
}
}
The contents of Dictionary<TKey, TValue>:
Max: 1000
John: 4000
Tom: 2000
Ana: 6000
Rick: 5000
Does our Dictionary<TKey, TValue> contains the key, Tom: True
Does our Dictionary<TKey, TValue> contains the key, Jack: False
Does our Dictionary<TKey, TValue> contains the value, 4000: True
Does our Dictionary<TKey, TValue> contains the value, 10000: False
//C# Removing elements from a Dictionary<TKey, TValue>
using System;
using System.Collections.Generic;
class DictionaryDemo
{
public static void Main(String[] ar)
{
//Creating a Dictionary<TKey, TValue>
//Where, Keys are of type char
//And, Values are of type String
Dictionary<char, String> hm = new Dictionary<char, String>();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
hm.Add('B', "Banana");
hm.Add('M', "Mango");
hm.Add('G', "Grapes");
hm.Add('A', "Apple");
hm.Add('O',"Orange");
//Getting a collection of the keys using property Keys
ICollection<char> c = hm.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The contents of Dictionary<TKey, TValue>:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + hm[ch]);
//Calling the method Remove() to remove a key 'A' and its associated value
hm.Remove('A');
//Calling the method Remove() to remove a key 'M' and its associated value
hm.Remove('M');
//Printing all elements of Dictionary<TKey, TValue>
Console.WriteLine("The updated contents of Dictionary<TKey, TValue> after removing keys 'A' and 'M' and their values:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + hm[ch]);
}
}
The contents of Dictionary<TKey, TValue>:
A: Apple
G: Grapes
B: Banana
O: Orange
M: Mango
The updated contents of Dictionary<TKey, TValue> after removing keys 'A' and 'M' and their values:
G: Grapes
B: Banana
O: Orange
//C# Key cannot be null in a Dictionary<TKey, TValue>
using System;
using System.Collections.Generic;
class DictionaryDemo
{
public static void Main(String[] ar)
{
//Creating aDictionary<TKey, TValue> of type, Dictionary<String,String>
//Where keys are of type String and values are of type String
Dictionary<String, String> dict = new Dictionary<String, String>();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
dict.Add("B", "Banana");
dict.Add("Ma", "Mango");
dict.Add("Gr", "Grapes");
dict.Add("Ap", "Apple");
dict.Add("Or", "Orange");
dict.Add(null, "no value"); //Trying to store a null as a key
}
}
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at DictionaryDemo.Main(String[] ar)
//C# A value can be null in a Dictionary<TKey, TValue>
using System;
using System.Collections.Generic;
class DictionaryDemo
{
public static void Main(String[] ar)
{
//Creating a Dictionary<TKey, TValue>
//Where, keys are of char type and values of String type
Dictionary<char, String> dict = new Dictionary<char, String>();
//Adding key-value pairs by theAdd() method
//Where, each key is a character and each value is a String.
dict.Add('B', "Banana");
dict.Add('M', "Mango");
dict.Add('N', null); //Trying to store a null as a key
dict.Add('G', "Grapes");
dict.Add('A', "Apple");
dict.Add('O', "Orange");
//Getting a collection of the keys using property Keys
ICollection<char> c = dict.Keys;
//Printing the key-value pairs by using the collection of keys to obtain their values.
Console.WriteLine("The contents of Dictionary<TKey, TValue>:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + dict[ch]);
}
}
The contents of Dictionary:
B: Banana
M: Mango
N:
G: Grapes
A: Apple
O: Orange
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement