Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

覆盖ToString,GetHashCode,Equals方法有问题 #18

Open
tony-tang opened this issue Mar 31, 2015 · 1 comment
Open

覆盖ToString,GetHashCode,Equals方法有问题 #18

tony-tang opened this issue Mar 31, 2015 · 1 comment

Comments

@tony-tang
Copy link

代码:

/// <summary>
/// 测试覆盖系统方法
/// </summary>
public class Test12
{
    public string Name;
    public int Age;

    public override string ToString()
    {
        return string.Format("Name:{0}, Age:{1}", Name, Age);
    }

    public override bool Equals(object obj)
    {
        if (!(obj is Test12))
        {
            return false;
        }
        var o = (Test12) obj;
        return Name == o.Name && Age == o.Age;
    }

    public override int GetHashCode()
    {
        //for test
        return 123456789;
    }

    public static void TestOverrideObjectMethod()
    {
        Test12 t1 = new Test12 {Name = "Test1", Age = 18};
        Test12 t2 = new Test12 {Name = "Test1", Age = 18};

        Logger.Log("t1.ToString(): " + t1);
        Logger.Log("t1.GetHashCode(): " + t1.GetHashCode());
        Logger.Log("t1.Equals(t2): " + t1.Equals(t2));
    }
}

执行结果:

t1.ToString(): CLRSharp.CLRSharp_Instance
t1.GetHashCode(): 30880833
t1.Equals(t2): False

用例代码: tony-tang@e337e71

@lightszero
Copy link
Owner

这些是无法实现的,类型系统隔离

--- 原始邮件 ---

发件人: "tony-tang" [email protected]
已发: 2015年3月31日 上午9:30
收件人: "lightszero/LSharp" [email protected]
主题: [LSharp] 覆盖ToString,GetHashCode,Equals方法有问题 (#18)

代码:

/// <summary>
/// 测试覆盖系统方法
/// </summary>
public class Test12
{
    public string Name;
    public int Age;

    public override string ToString()
    {
        return string.Format("Name:{0}, Age:{1}", Name, Age);
    }

    public override bool Equals(object obj)
    {
        if (!(obj is Test12))
        {
            return false;
        }
        var o = (Test12) obj;
        return Name == o.Name && Age == o.Age;
    }

    public override int GetHashCode()
    {
        //for test
        return 123456789;
    }

    public static void TestOverrideObjectMethod()
    {
        Test12 t1 = new Test12 {Name = "Test1", Age = 18};
        Test12 t2 = new Test12 {Name = "Test1", Age = 18};

        Logger.Log("t1.ToString(): " + t1);
        Logger.Log("t1.GetHashCode(): " + t1.GetHashCode());
        Logger.Log("t1.Equals(t2): " + t1.Equals(t2));
    }
}

执行结果:

t1.ToString(): CLRSharp.CLRSharp_Instance
t1.GetHashCode(): 30880833
t1.Equals(t2): False

用例代码: tony-tang@e337e71


Reply to this email directly or view it on GitHub:
#18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants