C#
C# Stream Tutorial: StreamReader, StreamWriter with Example
In C# file operations, normally streams are used to read and write to files. A stream is an...
A hash table is a special collection that is used to store key-value items. So instead of storing just one value like the stack, array list and queue, the hash table stores 2 values. These 2 values form an element of the hash table.
Below are some example of how values of a hash table might look like.
{ "001" , ".Net" }
{ "002" , ".C#" }
{ "003" , "ASP.Net" }Above we have 3 key value pairs. The keys of each element are 001, 002 and 003 respectively. The values of each key value pair are ".Net", "C#" and "ASP.Net" respectively.
Let's look at the operations available for the Hashtable collection in more detail.
The declaration of a Hashtable is shown below. A Hashtable is created with the help of the Hashtable Datatype. The "new" keyword is used to create an object of a Hashtable. The object is then assigned to the variable ht.
Hashtable ht = new Hashtable()
The Add method is used to add an element on to the queue. The general syntax of the statement is given below
HashTable.add("key","value")Remember that each element of the hash table comprises of 2 values, one is the key, and the other is the value.
Now, let's see this working at a code level. All of the below-mentioned code will be written to our Console application.
The code will be written to our Program.cs file. In the below program, we will write the code to see how we can use the above-mentioned methods.
For now in our example, we will just look at how we can create a hashtable , add elements to the hashtable and display them accordingly.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("001",".Net");
ht.Add("002","C#");
ht.Add("003","ASP.Net");
ICollection keys = ht.Keys;
foreach (String k in keys)
{
Console.WriteLine(ht[k]);
}
Console.ReadKey();
}
}
} Code Explanation:-
If the above code is entered properly and the program is run the following output will be displayed.
Output:
Let's look at some more methods available for hash tables.
This method is used to see if a key is present in the Hashtable. Below is the general syntax of this statement. The statement will return true if the key exists, else it will return the value false.
Hashtable.Containskey(key)
This method is used to see if a Value is present in the Hashtable. Below is the general syntax of this statement. The statement will return true if the Value exists, else it will return the value false.
Hashtable.ContainsValue(value)
Let's change the code in our Console application to showcase how we can use the "Containskey" and "ContainsValue" method.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("001",".Net");
ht.Add("002","C#");
ht.Add("003","ASP.Net");
Console.WriteLine(ht.ContainsKey("001"));
Console.WriteLine(ht.ContainsValue("C#"));
Console.ReadKey();
}
}
}Code Explanation:-
If the above code is entered properly and the program is run the following output will be displayed.
Output:
From the output, you can clearly see that both the key and value being searched are present in the hash table.
Summary
In C# file operations, normally streams are used to read and write to files. A stream is an...
What is an Arrays in C#? An array is used to store a collection or series of elements. These...
What is Queue in C#? The Queue is a special case collection which represents a first in first out...
So far we have seen how to work with C# to create console based applications. But in a real-life...
$20.20 $9.99 for today 4.5 (114 ratings) Key Highlights of C# Tutorial PDF 243+ pages eBook...
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It is used...