Well, let's say that there is a class:

Person, which contains the ID and FIO.

There, the Equals method and GetHashCode method are redefined so that it compares the ID to determine uniqueness.

Let's say I will store all this in a HashSet . However, how can I quickly get the actual item from my collection if necessary?

The TryGetValue method has been added to the latest version of the Framework. It turns out that I can only get through the search?

The dictionary seems to me that using it is unnecessarily a Dictionary<Person,Person> ...

UPD

My motivation is that why should I use a Dictionary if the element itself can be a key => when creating a HashSet I can pass a custom comparator, which for example will support uniqueness by ID, and then I would like to get the current value from the hash through Hash.TryGetValue(new Person(){ID=100500})

  • 2
    What do you have in mind? If to receive a copy on id, then usually do the dictionary Dictionary <ID, Person>. Redefining Person's Getals and GetHashCode are a bit weird ... - Andrey NOP
  • I also did not understand what should happen as a result - Andrew
  • In our project, a Dictionary<IdType, Person> used for a similar purpose. So what to do? - Alexander Petrov
  • one
    You can take the source of the latest version of Hashset and use it in your project instead of the collection from BCL - Andrey NOP
  • one
    In general, the fact that you want to get an object from HashSet that has a HashCode that matches the desired one is meaningless. Moreover, the search checks not only HashCode, but also the equality of the two elements, through Equal. In its meaning, each non-identical object should have its own HashCode and I am silent, that they should not coincide with Equal. That is, the very object that you are looking for - this is the object from HashSet according to the HashSet logic. And the fact that the reference elements can match all the fields and at the same time having different links is already your problem. In general, HashSet is clearly not created for this. - John

1 answer 1

To begin with, we define that HashSet is a set of unique elements that allows you to quickly check for the presence of a specific element in a set, but not to search for this element by any sign. Also mention that all elements that you add to a HashSet must be immutable with the correct implementation of Equals and GetHashCode. For example, here is an incorrect one:

 public class Person { public int ID {get;set;} public override int GetHashCode()=>ID.GetHashCode(); } 

With this implementation, when you can change the object's hashcode, this code will work incorrectly.

 var set = new HashSet<Person>(); var person = new Person() {ID = 10}; set.Add(person); Console.WriteLine(set.Contains(person)); // true person.ID = 20; Console.WriteLine(set.Contains(person)); // false 

Further, if you need to find an object, then this should already be a search structure. For example, if you want to search for an object by key, then you need a structure with a key-object association (associative array), which is a hash table or dictionary.

That is, if the task in checking for the uniqueness of an element in a set is HashSet, while searching for an element in a set is a dictionary / hash table.