In this article we understand Dictionary and Hashtable.
Dictionary and Hashtable are both used to hold the data as key-value pairs in C#.
Dictionary
The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings. Dictionary requires a special syntax form.
public class Dict
{
static void Main()
{
// Example Dictionary again
Dictionary<string, int> d = new Dictionary<string, int>()
{
{"Cat", 2}, {"dog", 1}};
// Loop over pairs with foreach
foreach (KeyValuePair<string, int> pair in d)
{
Console.WriteLine ("{0}, {1}",pair.Key, pair.Value);
}
foreach (var pair in d)
{
Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
}
Console.ReadKey();
}
}
Hashtable
using System.Collections;
class Program
{
static Hashtable GetHashtable()
{
// Create and return new Hashtable.
Hashtable hashtable = new Hashtable();
hashtable.Add("Delhi", 1000);
hashtable.Add("Varansi", 55);
hashtable.Add("Mumbai", 540);
return hashtable;
}
public static void Main()
{
Hashtable hashtable = GetHashtable();
// See if the Hashtable contains this key.
Console.WriteLine(hashtable.ContainsKey("Varansi"));
// Test the Contains method. It works the same way.
Console.WriteLine(hashtable.Contains("Mumbai"));
// Get value of Mumbai with indexer.
int value = (int)hashtable["Mumbai"];
// Write the value of Mumbai.
Console.WriteLine(value);
}
}
output
True
True
1000
Dictionary and Hashtable are both used to hold the data as key-value pairs in C#.
Dictionary
The Dictionary type provides fast lookups with keys to get values. With it we use keys and values of any type, including ints and strings. Dictionary requires a special syntax form.
- Dictionary is generic type Dictionary<TKey,TValue>
- Dictionary class is a strong type < TKey,TValue > Hence, you must specify the data types for key and value.
- There is no need of boxing/unboxing.
- When you try to access non existing key dictionary, it gives runtime error.
- Dictionary maintains an order of the stored values.
- There is no need of boxing/unboxing, so it is faster than Hashtable.
public class Dict
{
static void Main()
{
// Example Dictionary again
Dictionary<string, int> d = new Dictionary<string, int>()
{
{"Cat", 2}, {"dog", 1}};
// Loop over pairs with foreach
foreach (KeyValuePair<string, int> pair in d)
{
Console.WriteLine ("{0}, {1}",pair.Key, pair.Value);
}
foreach (var pair in d)
{
Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
}
Console.ReadKey();
}
}
Hashtable
- Hashtable is non-generic type.
- Hashtable is a weakly typed data structure, so you can add keys and values of any object type.
- Values need to have boxing/unboxing.
- When you try to access non existing key Hashtable, it gives null values.
- Hashtable never maintains an order of the stored values.
- Hashtable needs boxing/unboxing, so it is slower than Dictionary.
using System.Collections;
class Program
{
static Hashtable GetHashtable()
{
// Create and return new Hashtable.
Hashtable hashtable = new Hashtable();
hashtable.Add("Delhi", 1000);
hashtable.Add("Varansi", 55);
hashtable.Add("Mumbai", 540);
return hashtable;
}
public static void Main()
{
Hashtable hashtable = GetHashtable();
// See if the Hashtable contains this key.
Console.WriteLine(hashtable.ContainsKey("Varansi"));
// Test the Contains method. It works the same way.
Console.WriteLine(hashtable.Contains("Mumbai"));
// Get value of Mumbai with indexer.
int value = (int)hashtable["Mumbai"];
// Write the value of Mumbai.
Console.WriteLine(value);
}
}
output
True
True
1000
0 Comments