P
P
pavel_arz2021-10-17 13:33:51
C++ / C#
pavel_arz, 2021-10-17 13:33:51

How to access the value of a dictionary by the key Dictionary in C#?

How can I access a type dictionary by key in C#? I need to get the value in the dictionary by the key . How to do it right? Dictionary<Dictionary<string, string>, int>intDictionary

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2021-10-17
@pavel_arz

The Dictionary builds a hash table based on the values ​​returned by the GetHashCode method of each key, and compares the keys using the Equals method. The Dictionary class, as far as I know, does not override these methods, which means that GetHashCode is created from a pointer. That is, the same object with which a new key was added can be used as a key, or if the link to it is lost, then it can be obtained through iteration of the keys.
Never use such constructions! The Dictionary is designed to only work with immutable keys that always return the same hash based on the contents of the object.
Although if this Dictionary<Dictionary<string, string>, int> is created with a comparator that calculates a hash from Dictionary<string, string> and checks the equality of objects by content, then you can simply create a new Dictionary<string, string> with the necessary keys and values ​​and use it as a key. And if not created, then you can create a new Dictionary based on it already with a comparator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question