简体   繁体   English

.net对象相等

[英].net object equality

Lets say we have two objects o1 & o2 defined as System.Object, in my situtaion o1 & o2 can be any of the following types: 假设我们有两个对象o1和o2定义为System.Object,在我的坐标中o1和o2可以是以下任何类型:

  • String
  • Int32 INT32
  • Double
  • Boolean 布尔
  • DateTime 约会时间
  • DBNull 的DBNull

So how can I check that o1 & o2 are equal, therefore are the same object or both have the same type & value. 那么如何检查o1和o2是否相等,因此是相同的对象或两者具有相同的类型和值。

Can I just do o1 == o2 or do I need to do o1.Equals(o2) or something else? 我可以做o1 == o2或者我需要做o1.Equals(o2)还是别的什么?

Thanks, 谢谢,

AJ AJ

I would suggest you use 我建议你用

object.Equals(o1, o2)

as that will cope with nullity as well. 因为这也将应对无效。 (That assumes you want two null references to compare as equal.) (假设您希望两个空引用比较相等。)

You should not use == because operators are not applied polymorphically; 应该使用==因为运营商不采用多态; the types overload == but they don't override it (there's nothing to override). 类型重载 ==但它们不会覆盖它(没有什么可以覆盖)。 If you use 如果你使用

o1 == o2

that will compare them for reference identity , because the variables are declared to be of type object . 这将比较它们的引用标识 ,因为变量被声明为object类型。

Using o1.Equals(o2) will work except in the case where o1 is null - at which point it would throw a NullReferenceException . 使用o1.Equals(o2)将起作用, 除非o1为null的情况下 - 此时它将抛出NullReferenceException

Operator == compare objects by reference, and method Equals compare objects by value. 运算符==按引用比较对象,方法等于按值比较对象。
For Example: 例如:

StringBuilder s1 = new StringBuilder(“Yes”);

StringBuilder s2 = new StringBuilder(“Yes”);

Console.WriteLine(s1 == s2);

Console.WriteLine(s1.Equals(s2));

Will display: 将显示:

False

True

Value objects can be compared either by == or Equals. 值对象可以通过==或Equals进行比较。

I would use Object.Equals(o1,o2) - ref. 我会使用Object.Equals(o1,o2) - ref。 MSDN MSDN

Jon has supplied excellent explanations of why this would be the best usage. Jon提供了很好的解释,说明为什么这是最好的用法。

'Equals' should work for strings and the value types you listed. 'Equals'应该适用于字符串和您列出的值类型。

'==' Will fail for things like the following code because the references to the boxed objects aren't the same: '=='对于以下代码之类的东西会失败,因为对盒装对象的引用不一样:

        int x = 1;
        int y = 1;
        Object o1 = x;
        Object o2 = y;

Edit: I noticed the stringbuilder example given above, but since you are using strings and their equality operator is overridden they'd actually work with either "==" or ".Equals", the following code 编辑:我注意到上面给出的stringbuilder示例,但由于您正在使用字符串并且它们的等式运算符被覆盖,它们实际上可以使用“==”或“.Equals”,以下代码

string s1 = "Yes"; string s1 =“是”;

string s2 = "Yes"; string s2 =“是”;

Console.WriteLine(s1 == s2); Console.WriteLine(s1 == s2);

Console.WriteLine(s1.Equals(s2)); Console.WriteLine(s1.Equals(S2));

Outputs True True 输出True True

object.Equals(obj1, obj2) // This is the way prefered and the best practice for equality comparison.

Thus you might also consider overriding the Equals method when working with custom objects (not the case here appearently). 因此,在处理自定义对象时,您可能还会考虑覆盖Equals方法(这里不是这种情况)。

public class Something {
    public long Id { get; set; }
    public string Name { get; set; }

    public override bool Equals(object obj) {
        if (obj == null)
            return false;

        if (((Something)obj) == null)
            return false;

        Something s = (Something)obj;

        return this.Id == s.Id && string.Equals(this.Name, s.Name);
    }

    public bool Equals(Something s) {
        if (s == null)
            return false;

        return this.Id == s.Id && string.Equals(this.Name, s.Name);
    }
}

This way, you will assure that your custom objects are equal. 这样,您将确保自定义对象相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM