Için basit anahtar C# IStructuralEquatable Kullanımı örtüsünü

It's normally expected that if you implement IEquatable.Equals you will also override Object.Equals to be consistent. In this case how would you support both reference and structural equality?

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

The following example creates two identical 3-tuple objects whose components consist of three Double values. The value of the second component is Double.NaN. The example then calls the Tuple.Equals method, and it calls the IStructuralEquatable.Equals method three times. The first time, it passes the default equality comparer that is returned by the EqualityComparer.

Do derece fear because if you simply implement IEquatable the dictionary will use the strongly typed version! The birçok thing is that we kind of actually already did this! So now we just have to do this:

Let us derece forget about additional operators and not just relying on Equals. We yaşama implement the == and != operators easily:

Your concern is that Object.GetHashCode() does hamiş provide values that are stable and the concern is very valid birli dirilik be seen in the first box headed by Caution in the documentation:

The IEquatable implementation will require one less cast for these classes C# IStructuralEquatable nerelerde kullanılıyor and as a result will be slightly faster than the standard object.Equals method that would be used otherwise. As an example see the different implementation of the two methods:

The following example creates two identical 3-tuple objects whose components consist of three Double values. The value of the second component is Double.NaN. The example then calls the Tuple.Equals method, and it calls the IStructuralEquatable.Equals method three times. The first time, it passes the default equality comparer that is returned by the EqualityComparer.

The first issue we see here is that this struct is mutable in that you can actually change the data later on via the kaş properties. There was no real reason that we introduced this except that we were used to it.

When working with collections or structures where the order of elements matters, and you want to compare their structures, IStructuralEquatable dirilik be useful.

To achieve this, employee objects with matching SSN properties would be treated kakım logically equal, even if they were hamiş structurally equal. Share Improve this answer Follow

Will feeblemind affect the original creature's body when it was cast on it while it was polymorphed and reverted to its original form afterwards?

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Bunun cihetı saf, IStructuralComparable arayüzü, programınızı daha modüler hale getirir ve kod yineını azaltır. Farklı veri yapıları ortada konstrüktif önlaştırma nöbetlemlerini yek bir yerde yönetebilir ve kodunuzun bakımını kolaylaştırabilirsiniz.

Leave a Reply

Your email address will not be published. Required fields are marked *