Dictionary Object
Dictionary is a collection object which is used to store data in key and value pairs. In dictionary objectKey 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.
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.
|
No comments:
Post a Comment