Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Dictionary in C#, VB.NET with Examples

Aug 24, 2016
Introduction:

Here I will explain what is dictionary in c#, vb.net with example or dictionary object in c#, vb.net with example or how to create and use dictionary object in c#, vb.net with example. Dictionary object in c# or vb.net is a collection which is used to represent data as a key and value pairs.

Description:


Dictionary Object

Dictionary is a collection object which is used to store data in key and value pairs. In dictionary object Key value must be always unique and it won’t accept any null or duplicate values because we use keys to identify values stored in dictionary object but values can be duplicated or we can set null value also.

The Dictionary collection is a part of generic collections so to use Dictionary object in our applications we need to add following namespace in our applications.

C# Code


using System.Collections.Generic;

VB.NET Code


Imports System.Collections.Generic

We will see the syntax of using Dictionary object in our applications.

Syntax of Dictionary Object

Following is the syntax of using Dictionary object in our applications.


Dictionary<TKey,TValue>

The Dictionary class will accept two parameters one is for key and another for value.

TKey – It defines what type of keys which we are going to store.

TValue – It defines what type of values which we are going to store.

Following is the declaration of Dictionary object


Dictionary<int,string>

The dictionary object can be initialized with IDictionary<TKey,TVal> interface or Dictionary<TKey,TVal> class like as shown below

C# Code


IDictionary<int, string> objdict = new Dictionary<int, string>();

//or

Dictionary<int, string> objdict = new Dictionary<int, string>();

VB.NET Code


Dim objdict As IDictionary(Of Integer, String) = New Dictionary(Of Integer, String)()
'or

Dim objdict As New Dictionary(Of Integer, String)()

It’s always recommended to use IDictionary<TKey,TVal> interface instead of Dictionary<TKey,TVal> class.

We will see how to add elements to Dictionary object and how to access Dictionary object elements with example.

Example of Dictionary Object

Following is the example of using Dictionary object in our applications.

C# Code


using System;
using System.Collections.Generic;

namespace Linqtutorials
{
class Program
{
static void Main(string[] args)
{
// Adding Elements to Dictionary Object
IDictionary<int, string> objdic = new Dictionary<int, string>();
objdic.Add(1, "Suresh Dasari");
objdic.Add(2, "Rohini Alavala");
objdic.Add(3, "Praveen Alavala");
objdic.Add(4, "Sateesh Chandra");

// Read Data from Dictionary Object
Console.WriteLine("Number of Users: {0}", objdic.Count);
Console.WriteLine("User Details");
foreach (KeyValuePair<int, string> user in objdic)
{
Console.WriteLine("Key={0}, Value={1}", user.Key, user.Value);
}
Console.ReadLine();
}
}
}

VB.NET Code


Module Module1
Sub Main()
' Adding Elements to Dictionary Object
Dim objdic As IDictionary(Of Integer, String) = New Dictionary(Of Integer, String)()
objdic.Add(1, "Suresh Dasari")
objdic.Add(2, "Rohini Alavala")
objdic.Add(3, "Praveen Alavala")
objdic.Add(4, "Sateesh Chandra")

' Read Data from Dictionary Object
Console.WriteLine("Number of Users: {0}", objdic.Count)
Console.WriteLine("User Details")
For Each user As KeyValuePair(Of Integer, String) In objdic
Console.WriteLine("Key={0}, Value={1}", user.Key, user.Value)
Next
Console.ReadLine()
End Sub
End Module

If you observe above example we are adding elements to Dictionary object “objdic” and getting elements from Dictionary object “objdic” using “KeyValuePair<TKey, TVal>”. Now we will run and see the output that would be like as shown below.

Output of Dictionary Object Example

Following is the result of Dictionary object example.

Dictionary object in c#, vb.net with example
In Dictionary object we have different properties and methods available to perform like getting count or elements or check elements in Dictionary object or get particular value from dictionary object with key value, etc.

Dictionary Object Properties

Following are the properties available in dictionary object.

Property
Description
Comparer
It is used to determine equality of keys
Count
It is used to get number of elements exists in Dictionary<TKey,TVal>
Item
It’s used to gets or sets the value associated with the specified key.
Keys
It’s used to get collection containing keys in the Dictionary<TKey,TValue>.
Values
It’s used to get a collection containing the values in the Dictionary<TKey,TValue>.
Dictionary Object Methods

Following are the methods available in dictionary object.

Method
Description
Add(TKey,TVal)
It is used to add specified key and value to the dictionary
Remove(TKey)
Removes the value from Dictionary<TKey, TValue> based on specified key
TryGetValue(TKey, TValue)
It is used to get the value associated with the specified key.
Clear
It removes all keys and values from the Dictionary<TKey, TValue>.
ContainsKey(TKey)
It determines whether the Dictionary<TKey, TValue> contains specified key or not.
I hope it helps you to understand dictionary object in c# or vb.net.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email
© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.