C
C#2w ago
Faker

Data structure in C# - Dictionary<TKey, TValue>

Hello guys, I have a quick question... I have a miss-conception, when we say the term "dictionary" ; is it just another term for a hash map/hash table? Or it just uses the idea of hash table behind the scenes? Because if I remember correctly, we also have the concept of Hash set where there (I think), it takes only one generic parameter. How does these 2 differs in terms of one is key-value and the other is just the value please, I'm a bit confused.
39 Replies
Jimmacle
Jimmacle2w ago
yes, the C# hash table is just called dictionary for reasons that i don't know
ero
ero2w ago
you look things up in it
Faker
FakerOP2w ago
hmm but in general they are the same thing ?
Jimmacle
Jimmacle2w ago
yes, different names for the same thing
Faker
FakerOP2w ago
ah ok but hmm I don't understand 1 thing... in a dictionary, we use 2 generic parameters but in a hash set, we use only 1
Jimmacle
Jimmacle2w ago
in a hash set the key and value are the same object, basically
ero
ero2w ago
well a dictionary maps one key to another value a set just contains unique values and, yes, dictionaries use hashing under the hood same reason why dictionary keys must be unique
Angius
Angius2w ago
It's called a "dictionary" probably because "hashtable" was taken by the non-generic collection
Faker
FakerOP2w ago
yep I see... but I'm still confuse for the hash set vs dictionary, why 2 parameters for dictionary while only 1 for the hash set... both do the same job, why dictionary requires 2 parameters, can't we just give the value we wanted to store?
Angius
Angius2w ago
Hashset != hashtable
ero
ero2w ago
they definitely don't do the same job
Faker
FakerOP2w ago
ah ?
Angius
Angius2w ago
Hashset is like a list, but stores unique values Dictionary stores key-value pairs
MODiX
MODiX2w ago
Angius
REPL Result: Success
HashSet<int> hs = [ 1, 1, 1, 2, 4, 6, 7, 3, 4, 4, 2, 2 ]; hs
HashSet<int> hs = [ 1, 1, 1, 2, 4, 6, 7, 3, 4, 4, 2, 2 ]; hs
Result: HashSet<int>
[
1,
2,
4,
6,
7,
3
]
[
1,
2,
4,
6,
7,
3
]
Compile: 280.693ms | Execution: 25.953ms | React with ❌ to remove this embed.
Faker
FakerOP2w ago
oh ok hmm but we shouldn't get an error because HashSet aren't suppose to be able to contain duplicates, no ? ah the thing is we initialize the set but the set automatically removes the duplicate values?
Sehra
Sehra2w ago
you can think of hashset as a dictionary that only store the keys
Faker
FakerOP2w ago
yep I see
MODiX
MODiX2w ago
Sehra
REPL Result: Success
var h = new HashSet<int>();
(h.Add(1), h.Add(2), h.Add(1))
var h = new HashSet<int>();
(h.Add(1), h.Add(2), h.Add(1))
Result: ValueTuple<bool, bool, bool>
{
"item1": true,
"item2": true,
"item3": false
}
{
"item1": true,
"item2": true,
"item3": false
}
Compile: 306.594ms | Execution: 27.314ms | React with ❌ to remove this embed.
Sehra
Sehra2w ago
this part is very useful, it return true if the item was added, false if not
ero
ero2w ago
i'm not sure about that... a HashSet<T> is a collection of Ts. a Dictionary<TKey, TValue> is a collection of KeyValuePair<TKey, TValue>s the keys in a dictionary are hashed but a dictionary doesn't use or act like a hashset
Faker
FakerOP2w ago
by the way dictionary also should have unique keys no ?
ero
ero2w ago
yes
MODiX
MODiX2w ago
Angius
REPL Result: Success
new Dictionary<int, int> {
[1] = 10,
[2] = 12,
[2] = 43,
}
new Dictionary<int, int> {
[1] = 10,
[2] = 12,
[2] = 43,
}
Result: Dictionary<int, int>
{
"1": 10,
"2": 43
}
{
"1": 10,
"2": 43
}
Compile: 211.767ms | Execution: 24.377ms | React with ❌ to remove this embed.
Faker
FakerOP2w ago
yep I see, thanks !
MODiX
MODiX2w ago
Sehra
REPL Result: Failure
new Dictionary<int, int> {
{ 1, 10 },
{ 2, 12 },
{ 2, 43 },
}
new Dictionary<int, int> {
{ 1, 10 },
{ 2, 12 },
{ 2, 43 },
}
Exception: ArgumentException
- An item with the same key has already been added. Key: 2
- An item with the same key has already been added. Key: 2
Compile: 270.283ms | Execution: 23.855ms | React with ❌ to remove this embed.
Sehra
Sehra2w ago
keep in mind they do different things, first use [key] = val to assign, while second calls .Add(key, val)
Faker
FakerOP2w ago
hmm I have a question, is there a difference in the syntax here to initialize the collection? Like I noticed you guys used [] and {} because first code it didn't give an error but remove the duplicate
Angius
Angius2w ago
Literally the message above yours lol
Faker
FakerOP2w ago
ah
Angius
Angius2w ago
The {} syntax gets lowered to a bunch of .Add() calls
Faker
FakerOP2w ago
lmao
Angius
Angius2w ago
While the []= is a direct assignment
Sehra
Sehra2w ago
and HashSet.Add does not throw on duplicates, while Dictionary.Add does
Faker
FakerOP2w ago
yeah I see hmm what does that mean on HashSet, it does not throw an exception ?
Sehra
Sehra2w ago
doesn't throw exception, it returns true/false instead. same with Dictionary.TryAdd
Faker
FakerOP2w ago
ahhh I see
ero
ero2w ago
you really gotta pay more attention to the thread you yourself opened here
Faker
FakerOP2w ago
yep sorry, understood how hash set and dictionary/hash table work now though and how they differ, thanks !
wasabi
wasabi2w ago
It's called a Dictionary because that's what it was called in VB6.

Did you find this page helpful?