In this article am going to explain you concept of
dictionaries in C#, from base to the level afterwards I’ll be discussing about
the concept of types of dictionaries and concept of hash table and it’s related
useful operation with their respective example.
Dictionaries
· - Dictionaries are used to associate a particular key
with a given value.
· - Dictionaries are collection classes.
· - Functionality or access to the dictionary in C# is
provided using-
using System.Collections;
using System.Collections;
· - A dictionary uses KEYS, for their functioning.
· - Dictionaries don’t have any sense of order.
· - Keys, works as lookups for table.
· - A dictionary contains various set of members in its
declaration.
Now am
going to discuss about hash table.
Hash
Table | Dictionaries
· - Hash Table is an example of dictionary.
· - Declared in the same fashion as you declare other
classes in C#.
· - Declared using this function
Hashtable HT = new Hashtable();
Hashtable HT = new Hashtable();
·
This will create a new hash table, in which you can
add data and perform other operations.
Operations
| Hash Table
The major operations of hash table are-
- Add
- Remove
- Copy
- Count
- Test
- Remove
- Copy
- Count
- Test
Add
Operation | Hash Table
Adding
Data to the Hash Table
There are two methods of adding data in your hash table in
C#, you can both use or any of them in your code. I’ll show both methods
through example.
Method:
1
In method 1 for adding data we do use-
HT.Add("CShub", "cshub.somee.com");
HT.Add("CShub", "cshub.somee.com");
HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
HT.Add("Picmaniac", "picmaniac.brinkster.net");
Method:
2
In method2 we do use-
HT["GoogleSays"] = "jaiswalabhishek.blogspot.in";
HT["GoogleSays"] = "jaiswalabhishek.blogspot.in";
HT["CShub"] = "cshub.somee.com";
Example
| Hash Table
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
Hashtable HT = new
Hashtable();
HT.Add("CShub", "cshub.somee.com");
HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
HT.Add("Picmaniac", "picmaniac.brinkster.net");
Console.WriteLine("Address
for website {0} is {1} ", "GoogleSays", HT["GoogleSays"]);
Console.ReadLine();
}
}
}
This is the second method for same functionality-
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
Hashtable HT = new
Hashtable();
// HT.Add("CShub", "cshub.somee.com");
// HT.Add("GoogleSays",
"jaiswalabhishek.blogspot.in");
// HT.Add("Picmaniac", "picmaniac.brinkster.net");
HT["GoogleSays"] = "jaiswalabhishek.blogspot.in";
HT["CShub"] = "cshub.somee.com";
Console.WriteLine("Address
for website {0} is {1} ", "GoogleSays", HT["GoogleSays"]);
Console.ReadLine();
}
}
}
Count
Operation | Hash Table
§ - As like
other languages in C# count is used for counting total number of data values
available in the hash table.
§ - There is no
sense of order in the hash table for this operation.
§ - Count operation
is performed by-
Console.WriteLine("Total websites of abhishek jaiswal are {0}", HT.Count);
Console.WriteLine("Total websites of abhishek jaiswal are {0}", HT.Count);
Example
| Count
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
Hashtable HT = new
Hashtable();
HT.Add("CShub", "cshub.somee.com");
HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
HT.Add("Picmaniac", "picmaniac.brinkster.net");
// Console.WriteLine("Address for website {0} is
{1} ", "GoogleSays",
HT["GoogleSays"]);
Console.WriteLine("Total
websites of abhishek jaiswal are {0}", HT.Count);
Console.ReadLine();
}
}
}
Remove
Operation | Hash Table
Remove operation is used for removing data
entries from your hash table. This is simply performed by using-
HT.Remove("GoogleSays");
HT.Remove("GoogleSays");
Example | Remove
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
Hashtable HT = new
Hashtable();
HT.Add("CShub", "cshub.somee.com");
HT.Add("GoogleSays", "jaiswalabhishek.blogspot.in");
HT.Add("Picmaniac", "picmaniac.brinkster.net");
HT.Remove("GoogleSays");
if (HT.ContainsKey("GoogleSays"))
{
Console.WriteLine("Address for website {0} is {1} ", "GoogleSays",
HT["GoogleSays"]);
// Console.WriteLine("Total websites of
abhishek jaiswal are {0}", HT.Count);
}
Console.ReadLine();
}
}
}