Remember that a Hashtable represents a collection of key/value pairs that are organized based on the hash code of the key, and when a element is added to the Hashtable it's stored based on the hash code of the key, and the elements don't preserves the order based in their insertion to the Hashtable's "bag".
As we Know that when an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. We can get the Hash Code with GetHashCode() method.
If you want that the items contained in a collection always be "sorted" you need to use a SortedList instead a HashTable. If you don need the items sorted then use a HashTable, beacuse it's more faster than a SortedList obtaining a value from their "bag". For ex-
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("ht01", "DotNetGuts");
ht.Add("ht02", "EasyTutor.2ya.com");
ht.Add("ht03", "DailyFreeCode.com");
Console.WriteLine("\nht01 hash code= " + "ht01".GetHashCode().ToString() + "\n\n" +
"ht02 hash code= " + "ht02".GetHashCode().ToString() +
"\n\n" + "ht01 hash code= " + "ht03".GetHashCode().ToString() + "\n\n");
Console.WriteLine("..........Hash Table Items..............\n");
foreach (DictionaryEntry entry in ht)
{
Console.WriteLine("{0}\t{1}\n", entry.Key, entry.Value);
}
Console.ReadKey();
Console.ReadLine();
}
}
So If you want that the items contained in a collection always be "sorted" you need to use a SortedList instead a HashTable.
Diffrent Algorithams are used to generate the hash code one of the algorithim is MD5 Hash.
public static string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
As we Know that when an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. We can get the Hash Code with GetHashCode() method.
If you want that the items contained in a collection always be "sorted" you need to use a SortedList instead a HashTable. If you don need the items sorted then use a HashTable, beacuse it's more faster than a SortedList obtaining a value from their "bag". For ex-
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("ht01", "DotNetGuts");
ht.Add("ht02", "EasyTutor.2ya.com");
ht.Add("ht03", "DailyFreeCode.com");
Console.WriteLine("\nht01 hash code= " + "ht01".GetHashCode().ToString() + "\n\n" +
"ht02 hash code= " + "ht02".GetHashCode().ToString() +
"\n\n" + "ht01 hash code= " + "ht03".GetHashCode().ToString() + "\n\n");
Console.WriteLine("..........Hash Table Items..............\n");
foreach (DictionaryEntry entry in ht)
{
Console.WriteLine("{0}\t{1}\n", entry.Key, entry.Value);
}
Console.ReadKey();
Console.ReadLine();
}
}
So If you want that the items contained in a collection always be "sorted" you need to use a SortedList instead a HashTable.
Diffrent Algorithams are used to generate the hash code one of the algorithim is MD5 Hash.
public static string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
No comments:
Post a Comment