c# - HashSets don't keep the elements unique if you mutate their identity -


While working with the hashset in C #, I had a nervous problem recently : HashSets does not guarantee the unity of elements; They are not set, they guarantee that the item is not added when the Add (T item) is if the item is in the item.equals (that) < Code> true . It does not keep a small program after it has already manipulated items in the set (which shows Copypasta from my Linux):

  Zero main () {hashaseet & lt; Examiner & gt; Testet = New Hashet & lt; Examiner & gt; (); Testet.Add (new tester (1)); Testet.Add (new tester (2)); Forehach (in tester tester) {tester.Dump (); } Foreach (tester tester in testet) {tester.myint = 3; } Foreach (tester in testet) {tester.Dump (); } Hashaseet & lt; Examiner & gt; Secondhashset = new hashset & lt; Examiner & gt; (testing); Taurus Tester in Taurus (Terror.)}} Class tester {public insent; Public examiner (int i) {this.myint = i; } Public override baul par (object o) {if (o == empty) false return; Examiner = Key tester; If returned (which == empty) wrong; Return (this.myint == that.myint); } Public override int GetHashCode () {this.myint return; } Public override string toasting () {return this.myint.ToString ()}}}   

This will manipulate objects in the archive equally, only when a new hashet is created will do it. What is the advice when I want to work with those sets where I should know that the entries are unique? Do your roll, where does the add (t item) add a copy from the item, and the calculator calculates the copies of the contained objects? The challenge is that each inherent element should be deep-copied, at least in its objects which affect its equality.

Another solution must roll yourself, and accept only those elements that apply INotifyPropertyChanged, and to investigate the incident again on the incident to resemble it, but it is seriously Not limited to mentioning lots of work and performance loss under the hood.

Still, I thought that another possible solution is ensuring that all the fields are constructs in the reader or constructure, all solutions have major drawbacks. Do I have any other options?

You are actually talking about object identification if you are going to haveh items, then No need of identification, so that they can be compared.

  • If that change occurs, then this is not a valid identity method. You currently have public insent it should actually be to read , and should only be set in the constructor.
  • If two objects are conceptually different (i.e. you want them to behave differently in your specific design) then their hash codes should be different.
  • If you have two objects with the same content (i.e. two value objects that have the same field value) then they have the same hash code and should be identical.
  • If your data model says that you can have two objects with the same content but they can not be equal, then you should use a surrogate id, not content.
  • Maybe your objects should be irreversible value types, so the objects can not change
  • If they are fluctuating types, then you should specify a surrogate id (i.e. Such as using a mounting counter ID or using the hash of the object) Changes to a given object

    This is a problem with your tester objects, set Not to recognize you Need to think about Ike is not an easy problem.

Comments