Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

hash table in c#

Hashtable numberNames = new Hashtable();
numberNames.Add(1,"One"); //adding a key/value using the Add() method
numberNames.Add(2,"Two");
numberNames.Add(3,"Three");

//The following throws run-time exception: key already added.
//numberNames.Add(3, "Three"); 

foreach(DictionaryEntry de in numberNames)
    Console.WriteLine("Key: {0}, Value: {1}", de.Key, de.Value);
		
//creating a Hashtable using collection-initializer syntax
var cities = new Hashtable(){
	{"UK", "London, Manchester, Birmingham"},
	{"USA", "Chicago, New York, Washington"},
	{"India", "Mumbai, New Delhi, Pune"}
};
		
foreach(DictionaryEntry de in cities)
    Console.WriteLine("Key: {0}, Value: {1}", de.Key, de.Value);
Comment

hashtable in c#

/*
 * HashTable is a non generic collction which stores key,value pairs like Dictionary<TKey, TValue>
 * Under the System.Collection
 * Keys must be unique and cannot be null
 * Values can be null and suplicates
 * Values can be accessed by the keys
 * The key values are stored in the DixtionaryEntry objects
 * Kesy can be off different types
 */
//Lets create a HashTable
using System.Collections;
//Declaring a hashtable
Hashtable a1 = new Hashtable();
//Uisng var
var a3 = new Hashtable();
//Declaring initilising a hashtable
Hashtable a2 = new Hashtable() { { 1, "Subhasis" }, { "Name", "Biswal"}, { true, false} };
//Add the elements to the Hashtable
a2.Add(2, "Arun");
foreach (DictionaryEntry e in a2)
    Console.WriteLine($"The key and value pairs by foreach and dictionaryentry : {e.Key}, {e.Value}");
//using var : does not work here
/*foreach (var e in a2)
    Console.WriteLine($"The key and value by using var : {e.Key} {e.Value}");*/
/*The key and value pairs by foreach and dictionaryentry : Name, Biswal
The key and value pairs by foreach and dictionaryentry : 2, Arun
The key and value pairs by foreach and dictionaryentry : 1, Subhasis
The key and value pairs by foreach and dictionaryentry : True, False

*/
//Update the Hashtable: here these are the object so we have to use the unboxing methods to convert the object to data type
string name = (string)a2["Name"];
bool r = (bool)a2[true];

// Now update: you can update to any data types you want
a2["Name"] = "Suvalipsa";
a2[true] = "Hello";
foreach(DictionaryEntry en in a2)
    Console.WriteLine($"Keys and Values after updation : {en.Key}, {en.Value}");
/*Keys and Values after updation : Name, Suvalipsa
Keys and Values after updation : 2, Arun
Keys and Values after updation : 1, Subhasis
Keys and Values after updation : True, Hello*/
//Remove element from the hashtable : Remove()
a2.Remove(2);
a2.Remove(true);

//to get the keys and values separately you have to use the  var keyword insted of the DictionaryEntry
foreach (var f in a2.Keys)
    Console.WriteLine($"The keys are {f}");
foreach (var g in a2.Values)
    Console.WriteLine($"The values are {g}");
/*The keys are 1
The keys are Name
The values are Subhasis
The values are Suvalipsa*/


Comment

PREVIOUS NEXT
Code Example
Csharp :: c# method 
Csharp :: c# convert bitmap to image 
Csharp :: insert data to access database c# 
Csharp :: serialize object to json 
Csharp :: c# interface property 
Csharp :: c# how to check the minimum and maximum of numbers 
Csharp :: dbset syntax 
Csharp :: csv to xml using xmldocument c# 
Csharp :: remove control characters from string c# 
Csharp :: animation not playing unity 
Csharp :: How can I get my stripe customer ID? 
Csharp :: string.format() c# 
Csharp :: c# async task constructor 
Csharp :: how to add object in dictionary in c# 
Csharp :: muovere un elemento in c# 
Csharp :: check .net installing 
Csharp :: f sharp global variable 
Csharp :: Read csv file into wpf C# 
Csharp :: <link rel="stylesheet" href="styles/kendo.common.min.css" / 
Csharp :: c# windows forms rtc 
Csharp :: how many zeros in quinnonagintillion 
Csharp :: how to get user browser information in .net core 
Csharp :: c# Unit Test IDbContextFactory 
Csharp :: conditional middleware .net core 
Csharp :: UnityEngine.Mesh:get_vertices() 
Csharp :: C# return dictionary string/integer from comparison of List and Array 
Csharp :: how to pass value to anothe form c# winform 
Csharp :: static variables 
Csharp :: asp zero create feature 
Csharp :: Derived classes of abstract class share property 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =