2 Dakika Kural için C# IStructuralEquatable Nasıl kullanılır

Wiki Article

comparer IEqualityComparer An object that determines whether the current instance and other are equal.

In this case you don't want to change your class implementation so you don't wantoverride the Equals method. this will define a general way to compare objects in your application.

Other types which implement structural equality/comparability include tuples and anonymous types - which both clearly benefit from the ability to perform comparison based on their structure and content. A question you didn't ask is:

Bey far kakım I see this is only exposed through the StructuralComparisons class. The only way I güç figure out to make this useful is to make a StructuralEqualityComparer helper class bey follow:

Bildiğiniz kadar new işletmenü classlarda kullanıldığı vakit müntesip classtan bir nesne istek edilmekte ve üretilen nesne belleğin Heap kısmında koruma edilmektedir.

The generic tuple classes (Tuple, Tuple, Tuple, and so on) and the Array class provide explicit implementations of the IStructuralEquatable interface. By casting (in C#) or converting (in Visual Basic) the current instance of an array or tuple to an IStructuralEquatable interface value and providing your IEqualityComparer implementation birli an argument to the Equals method, you birey define a custom equality comparison for the array or collection.

1 My understanding is that it's used for collection like types, and encapsulates the structural part of the comparison, but leaved the comparison of the elements to a comparer passed in by the user. But I'm hamiş really sure if I really got it.

Fakat, articles1 ve articles3 dizileri aynı makale saksılıklarına farklı sıralarda ehil başüstüneğundan, CompareTo metodu farklı bir valör döndürür ve bu dizilerin konstrüktif olarak bedel olmadığını belirtir.

(doesn't violate documentation), but it is clearly hamiş as good bey it would be if 0 were replaced with i. Also there's no reason to loop if the code were just going to use a single value from the array.

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

C# IStructuralComparable Determines whether the current collection object precedes, occurs in the same position bey, or follows another object in the sort order.

The example on MSDN gives part of the answer here; it seems to be useful for heterogeneous equality, rather than homogeneous equality - i.e. C# IStructuralEquatable nerelerde kullanılıyor for testing whether two objects (/values) of potentially different types

Just look at the default ValueType.Equals(object) code that gets called otherwise. It's an absolute performance killer that introduces boxing, type evaluation and finally falls back on reflection if any of the fields are reference types.

Being able to specify IStructuralEquatable/IStructuralComparable in such cases is actually useful. It would also be inconvenient to pass a TupleComparer or ArrayComparer everywhere you want to apply this type of comparison. The two approaches are hamiş mutually exclusive.

Report this wiki page