在C#中的对象==对象安全 [英] Is Object == Object safe in C#

查看:113
本文介绍了在C#中的对象==对象安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中的以下代码返回false:

In Java the following code can return false:

IntegerPlus o1 = new IntegerPlus(1000);
IntegerPlus o2 = o1;

boolean b1 = o1 == o2;
boolean b2 = o1.Equals (o2);



这也是在C#中的一个问题?还是C#在某种程度上它永远是真实的,即使对象移动得到执行==? (我描述了在Java的问题这里更多的细节。

推荐答案

没有。

在C#/。NET的散列码是不参与到 == != 或等于。如果对象是引用类型,它是由GC移动有什么这个对象,这将影响到默认的比较之外

In C#/.Net hash code is not involved into default implementation of ==, != or Equals. If object is reference type and it is moved by GC there is nothing outside of this object that will impact default comparisons.

修改:不相关的细节下面关于比较。

Edit: unrelated details about comparison below.

== 等于是不是彼此相关的(按照约定除外)。

== and Equals are not related to each other (except by convention).

在C#中你必须的 ==操作符的类/结构可以重载。默认行为是为引用类型比较引用值进行比较系统的价值类型和自定义的'结构'没有 == 运营商自动生成。请注意,其他比较等于并没有参与到运营商的默认定义==

In C# you have operator == that class/struct can overload. Default behavior is to compare references for reference types, value compare for system value types and for custom 'struct' there is no == operator auto-generated. Note that the other comparison Equals is not involved into default definition of operator ==.

有关预定义的值类型,如果它的操作数的值相等,否则为false等号(= =)返回true。对于字符串以外的引用类型,==如果两个操作数指的是同一个对象返回true。

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object.

您还可以提供您的自定义的Object.Equals 上课。对于类重定义 == 等于是重新定义所有的比较方法相关工作始终(<$ C的良好实践$ C> == ,!= 等于(对象等)的GetHashCode ;可能等于(的myType等)

You can also provide your custom Object.Equals for class. Good practice for classes the redefine == or Equals is to redefine all comparison related methods to work consistently (==, !=, Equals(object other), and GetHashCode; possibly Equals(myType other)).

的GetHashCode .NET中的唯一用途是基于散列的集合,比如词典 HashSet的。检查 GetHashCode的准则在C#

The only usage of GetHashCode in .Net is hash-based collections like Dictionary, HashSet. Check GetHashCode Guidelines in C#

自定义比较明显的样品 System.String 字符串) - 这是引用类型,但是行为与常规价值。向比较类型的关系

Obvious sample of custom comparison is System.String (string) - it is reference type but behaves as regular value type in relation to comparison.

因此,回到原始样品


  • 如果 IntegerPlus 结构没有定制==/ 等于:没有 == 自动创建的(新IntegerPlus(42)==新IntegerPluss(42) - 语法错误)。你得到的所有字段值比较等于(自动提供)。

  • 如果 IntegerPlus 不提供自定义的运算符== 等于你参考比较和新IntegerPlus(42)!=新IntegerPluss(42)键,同样为等于

  • 如果 IntegerPlus 仅提供 == ,<$ C之一$ C>!= ,等于不是行为将通过定制实现(可能是由外部观察者无法解释的)
  • $ b $定义b
  • 如果 IntegerPlus 为数值或引用类型,并提供一致的所有4个比较相关的方法,你可以得到价值比较的行为

  • if IntegerPlus is struct without custom "=="/Equals: there is no == automatically created (new IntegerPlus(42) == new IntegerPluss(42) - syntax error). You get value comparison for all fields for Equals (automatically provided).
  • if IntegerPlus is class that does not provide custom operator == or Equals you get reference comparison and new IntegerPlus(42) != new IntegerPluss(42) and same for Equals
  • if IntegerPlus provide only one of ==, !=, Equals than behavior will be as defined by custom implementation (likely unexplainable by external observer)
  • if IntegerPlus is either value or reference type and provides consistent set of all 4 comparison related methods you can get value comparison behavior

这篇关于在C#中的对象==对象安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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