IStructuralEquatable vs等于? [英] IStructuralEquatable vs Equals?

查看:86
本文介绍了IStructuralEquatable vs等于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 msdn

IStructuralEquatable

定义支持结构对象比较的方法平等.结构相等意味着两个对象相等,因为它们具有相等的值.它不同于引用相等,后者表示两个对象引用相等,因为它们引用相同的物理对象.

不是等于应该做什么?(覆盖 IEquatable 时)?

解决方案

之所以需要IStructuralEquatable,是为了定义一种新的比较方法,该方法适用于所有对象.

>

IStructuralEquatable接口使您可以实现自定义比较以检查集合的结构均等性对象.也就是说,您可以创建自己的结构定义相等并指定将此定义与集合一起使用接受IStructuralEquatable接口的类型.

例如,如果您想要一个列表,该列表将按照特定的定义对所有元素进行排序.在这种情况下,您不想更改您的类实现,因此您不想覆盖Equals方法.

这将定义一种比较应用程序中对象的通用方法.

according to msdn

IStructuralEquatable

Defines methods to support the comparison of objects for structural equality. Structural equality means that two objects are equal because they have equal values. It differs from reference equality, which indicates that two object references are equal because they reference the same physical object.

isnt it what Equals should do ? ( when overriding IEquatable) ?

解决方案

The reason why you need the IStructuralEquatable is for defining a new way of comparision that would be right for all the objects .

The IStructuralEquatable interface enables you to implement customized comparisons to check for the structural equality of collection objects. That is, you can create your own definition of structural equality and specify that this definition be used with a collection type that accepts the IStructuralEquatable interface.

For example if you want a list that will sort all its elements by a specific definition. 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.

这篇关于IStructuralEquatable vs等于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆