C# Generic Dictionary Tutorial with Example

In this chapter you will learn

  1. What are Generic Dictionary Class?
  2. How to declare it?
  3. Programming Example

What are Generics Dictionary Class?

Dictionary is a collection of keys and Values. We bind values with keys and store them in dictionary and later that key is used for accessing dictionary value. Generic Dictionary is type safe and avoid boxing and unboxing and provides complete benefit of Dictionary. Generic Dictionary belongs to System.Collection.Generic namespace.

Generic Collection doesn’t allow inserting duplicate keys.

Important Methods of Generic Dictionary Class

Properties

PropertiesUsage
ComparerGets the IEqualityComparer that is used to determine equality of keys for the dictionary.
CountGets the number of key/value pairs contained in the Dictionary.
Item[TKey]Gets or sets the value associated with the specified key.
KeysGets a collection containing the keys in the Dictionary.
ValuesGets a collection containing the values in the Dictionary.

Methods

MethodsUsage
Add(TKey, TValue)Adds the specified key and value to the dictionary.
Clear()Removes all keys and values from the Dictionary.
ContainsKey(TKey)Determines whether the Dictionary contains the specified key.
ContainsValue(TValue)Determines whether the Dictionary contains a specific value.
Equals(Object)Determines whether the specified object is equal to the current object.(Inherited from Object.)
Finalize()Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)
GetEnumerator() Returns an enumerator that iterates through the Dictionary.
GetHashCode()Serves as the default hash function. (Inherited from Object.)
GetObjectData (SerializationInfo, StreamingContext)Implements the System.Runtime .Serialization.ISerializable interface and returns the data needed to serialize the Dictionary instance.
GetType()Gets the Type of the current instance.(Inherited from Object.)
MemberwiseClone()Creates a shallow copy of the current Object.(Inherited from Object.)
OnDeserialization(Object)Implements the System.Runtime .Serialization.ISerializable interface and raises the deserialization event when the deserialization is complete.
Remove(TKey)Removes the value with the specified key from the Dictionary.
ToString()Returns a string that represents the current object.(Inherited from Object.)
TryGetValue(TKey, TValue)Gets the value associated with the specified key.

Programming Example

Output

Star Wars
[sci-fi, Star Wars]
[Action, The One]
[Fantacy, Star Dust]
[Computer, Algorithm]
[Love, 50 First Dates]
Key found. Value is Star Wars
_

Summary

In this chapter you learned Generic Dictionary class with complete programming example. In the next chapter you will learn Generic SortedList.


One thought on “C# Generic Dictionary Tutorial with Example”

  1. Hi,
    Very useful site to learn c# as a beginner.Thank you.

    Is there any pdf to download this entire tutorial?

Leave a Reply

Your email address will not be published. Required fields are marked *