为什么返回 false ? new Person(“詹姆斯”) == new Person(“詹姆斯”)?

发布于 2024-09-12 10:36:32 字数 551 浏览 9 评论 0原文

我已经覆盖 GetHashCodeEquals ,这两种方法为不同的对象提供相同的结果,但为什么仍然得到 false ?

class Program
{
    static void Main(string[] args)
    {    
        Console.WriteLine(new Person("james") == new Person("james"));    
        Console.ReadKey();
    }    
}

class Person
{
    private string Name;

    public Person(string name)
    {
        Name = name;
    }
    public override int GetHashCode()
    {
        return 1;
    }
    public override bool Equals(object obj)
    {
        return true;
    }
}

I have override GetHashCode and Equals and both methods provide same results for different objects but why still getting false ?

class Program
{
    static void Main(string[] args)
    {    
        Console.WriteLine(new Person("james") == new Person("james"));    
        Console.ReadKey();
    }    
}

class Person
{
    private string Name;

    public Person(string name)
    {
        Name = name;
    }
    public override int GetHashCode()
    {
        return 1;
    }
    public override bool Equals(object obj)
    {
        return true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

往事随风而去 2024-09-19 10:36:32

因为 == 运算符默认为引用相等。它不会调用您的 Equals 方法。

如果需要,您可以覆盖 == 运算符。请参阅:重写 Equals() 和运算符的指南 ==

在 C# 中,有两种不同类型的相等:引用相等(也称为标识)和值相等。值相等是通常理解的相等含义:它意味着两个对象包含相同的值。例如,两个值为 2 的整数具有相等的值。引用相等意味着没有两个对象可以比较。相反,有两个对象引用,并且它们都引用同一个对象。

[...]

默认情况下,运算符 == 通过确定两个引用是否指示同一对象来测试引用相等性。因此,引用类型不必实现运算符 == 即可获得此功能。当类型不可变时,即实例中包含的数据无法更改时,重载运算符 == 来比较值相等而不是引用相等可能很有用,因为作为不可变对象,它们可以被视为与 long 相同因为它们具有相同的值。在非不可变类型中重写运算符 == 不是一个好主意。

Because the == operator defaults to reference equality. It doesn't call your Equals method.

You can override the == operator if you want. See: Guidelines for Overriding Equals() and Operator ==

In C#, there are two different kinds of equality: reference equality (also known as identity) and value equality. Value equality is the generally understood meaning of equality: it means that two objects contain the same values. For example, two integers with the value of 2 have value equality. Reference equality means that there are not two objects to compare. Instead, there are two object references and both of them refer to the same object.

[...]

By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. It is not a good idea to override operator == in non-immutable types.

只为一人 2024-09-19 10:36:32

如果这确实是您想要的,您必须单独重写 == 运算符。

http://msdn.microsoft.com/en-我们/库/ms173147%28VS.80%29.aspx

You have to separately override the == operator if that's really what you want.

http://msdn.microsoft.com/en-us/library/ms173147%28VS.80%29.aspx

極樂鬼 2024-09-19 10:36:32

是的 dtb 是你想要

bool b = new Person("james").Equals(new Person("james")); 

Yes dtb is right you want

bool b = new Person("james").Equals(new Person("james")); 

instead

離人涙 2024-09-19 10:36:32

在本例中,== 是引用相等运算符。它比较两个引用是否相同。

new 运算符总是创建一个 new 对象,因此 new Something()永远与另一个new Something()

您可以重写 == 运算符来执行值比较而不是引用比较。这就是 String 所做的。

另请参阅

相关问题

== is a reference equality operator in this case. It compares if two references are identical.

The new operator always create a new object, so a new Something() will NEVER be an identical reference to another new Something().

You can override the == operator to perform value comparison instead of reference comparison. This is what e.g. String does.

See also

Related questions

一紙繁鸢 2024-09-19 10:36:32

== 运算符检查两个变量是否实际上是对内存中同一对象的引用。在您的示例中,您创建了两个詹姆斯。他们可能是双胞胎(即他们可能具有相同的记忆足迹),但他们不是同一个人(即他们有两个不同的记忆位置)。如果你写:

Person a = new Person("james");
Person b = a;
Console.WriteLine(a == b); 

你会得到true,因为 a 和 b 只是同一个 James 的两个名字。

The == operator checks whether two variables are in fact, literally references to the same object in memory. In your example you create two James. They might be twins (i.e. they might have the identical memory-footprint), but they're not the same person (i.e. they have two different memory locations). If you wrote:

Person a = new Person("james");
Person b = a;
Console.WriteLine(a == b); 

you would get true, because a and b are just two names for the same James.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文