Dictionary in C#
Namespace :- System.Collections.Generic
Assembly :- System.Collections.dll
public class Dictionary<TKey,TValue> :
System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IDictionary<TKey,TValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.Generic.IReadOnlyDictionary<TKey,TValue>, System.Collections.IDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
In C#, Dictionary is a generic Collection which is define under System.Collections.Generic namespace. It is used to store data in the form of key , value pairs (Dictionary<TKey,TValue>).
-: DICTIONARY CONSTRUCTORS :-
There are 7 constructors in c# Dictionary.
1. Dictionary<TKey, TValue>()
2. Dictionary<TKey,TValue>(IDictionary<TKey,TValue>)
3. Dictionary<TKey,TValue>(IDictionary<TKey,TValue>, IEqualityComparer<TKey>)
4. Dictionary<TKey,TValue>(IEqualityComparer<TKey>)
5. Dictionary<TKey,TValue>(Int32)
6. Dictionary<TKey,TValue>(Int32, IEqualityComparer<TKey>)
7. Dictionary<TKey,TValue>(SerializationInfo, StreamingContext)
-: CREATING A DICTIONARY AND ADDING ELEMENTS :-
using System.IO; using System; using System.Collections.Generic; class Program { static void Main() { Dictionary < string,string> dict = new Dictionary<string,string> { {"language","C#"}, {"topic","Dictionary"}, {"label","Beginner"} }; Console.WriteLine("We are learning "+dict["language"]+" "+dict["topic"] +" and level : "+dict["label"]); } }
using System.IO; using System; using System.Collections.Generic; class Program { static void Main() { Dictionary < string,string> dict=new Dictionary<string,string>(); dict["language"]="C#"; dict["topic"]="Dictionary"; dict["label"]="Beginner"; Console.WriteLine("We are learning "+dict["language"]+" "+dict["topic"] +" and level : "+dict["label"]); } }
using System.IO; using System; using System.Collections.Generic; class Program { static void Main() { Dictionary < string,string> dict=new Dictionary<string,string>(); dict.Add("language","C#"); dict.Add("topic","Dictionary"); dict.Add("label","Beginner"); Console.WriteLine("We are learning "+dict["language"]+" "+dict["topic"] +" and level : "+dict["label"]); } }
IMPORTENT POINTS
Dictionary store data in the form of key , value pairs.
The value of dictionary can be null but the key can not be null.
Keys in dictionary must be unique duplicate keys are not allowed in dictionary if you try to use duplicate key , compiler will throw exception.
The size of Dictionary is Dynamic , means according to need size of dictionary increase
You cann't store dynamic type element in dictionary . only store same type element in Dictionary
Dictionary is not Thread Safe. For thread-safe you can see ConcurrentDictionary<TKey,TValue> class or ImmutableDictionary<TKey,TValue> class