Difference between Hashtable and Dictionary in C#

preview_player
Показать описание
In Hashtable, you can store key/value pairs of the same type or of the different type.

In Dictionary, you can store key/value pairs of same type.
-------------
May be you like this
--------------



F

c# playlist:

bootstrap playlist:
Рекомендации по теме
Комментарии
Автор

So what is the difference between types KeyValuePair and DictionaryEntry

whatamitalkingabout
Автор

As per my understanding hashtable if faster than dictionary because it's using hashing algorithm which is in integer so that all operations becomes faster.

prabhatsingh-iepm
Автор

are you in a public bathroom recording this?

Layarion
Автор

class Class1

   {

Hashtable hTable = new Hashtable();

        private void Test()

          {

            hTable.Add(1, "First String");

            hTable.Add(2, "Second String");

            hTable.Add(3, "Third String");

            hTable.Add(2, hTable[3]);

           

        }

       

    }

What's the output of this program 🤔

am_ri_here
Автор

Thank you for your explanation, it helps me a lot
But why when we print out on the screen, the Dictionaries always printed from up to down, but in the hashtable, it printed not the same
I mean if we have the exact code:

//Creating a dictionary using Dictionary<TKey, TValue> class
Dictionary<string, string> My_dict = new Dictionary<string, string>();

//Adding key/value pairs in the Dictionary Using Add() method
My_dict.Add("a1", "C");
My_dict.Add("a2", "C++");
My_dict.Add("a3", "C#");

foreach(KeyValuePair<string, string> element in My_dict)
{
Console.WriteLine("Key:- {0} and Value:- {1}", element.Key, element.Value);
}

//Create a hashtable Using Hashtable class
Hashtable my_hashtable = new Hashtable();

//Adding key/value pair in the hashtable using Add() method
my_hashtable.Add("A1", 1);
my_hashtable.Add("A2", "to");
my_hashtable.Add("A3", 3.89);
my_hashtable.Add("A4", "Tack");
my_hashtable.Add("A5", 237423);

foreach ( DictionaryEntry element in my_hashtable)
{
Console.WriteLine("Key:- {0} and Value:- {1} ", element.Key, element.Value);
}
Console.ReadKey();

the Crl+F5

Key:- a1 and Value:- C
Key:- a2 and Value:- C++
Key:- a3 and Value:- C#
Key:- A3 and Value:- 3.89
Key:- A4 and Value:- Tack
Key:- A1 and Value:- 1
Key:- A5 and Value:- 237423
Key:- A2 and Value:- to

we see here that the hashtable is begine with "A3" and then goes to "A4" and then go back to "A1" and then to "A5". However, in the Dictionary, it's begin from A1 to A2 to A3

Codality