gpt4 book ai didi

c# - 以下代码如何调用 Equals、GetHashCode 和 Compare? (神秘)

转载 作者:行者123 更新时间:2023-11-30 14:10:36 31 4
gpt4 key购买 nike

此代码有效,但它如何不实际调用任何列出的方法。它有效,但它的工作原理和原因似乎几乎是神奇的。我实际上从未调用过 Equals、GetHashCode 或 Compare,但代码有效。我也从不在实现两个接口(interface)的 EmployeeComparer 类的构造函数中调用任何东西。

问题:这是在使用新关键字实例化类时自动完成的吗?(这里似乎有些不对劲)

        public class EmployeeComparer : IEqualityComparer<Employee>,
IComparer<Employee>

员工比较器:

        public bool Equals(Employee x, Employee y)
{
return String.Equals(x.Name, y.Name);
}

public int GetHashCode(Employee obj)
{
return obj.Name.GetHashCode();
}

public int Compare(Employee x, Employee y)
{
return String.Compare(x.Name, y.Name);
}

类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public delegate void Del(string e);
Del handler = DelegateMethod;
Dictionary<string, List<Employee>> d = new Dictionary<string,
List<Employee>>();

public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
System.Console.ReadKey();
}

public void testDel(Del d)
{
d.Invoke("L");
}

static void Main(string[] args)
{
Program p = new Program();
//p.handler("Hello World");
//p.handler("DisneyLand");
//p.handler("Cattle Wars");
//p.testDel(p.handler);
var d = new SortedDictionary<string, SortedSet<Employee>>();
//Hashset no duplicates

d.Add("AA", new SortedSet<Employee>(new EmployeeComparer()));
d["AA"].Add(new Employee { Name = "A" });
d["AA"].Add(new Employee { Name = "B"} );
d["AA"].Add(new Employee { Name = "C"} );
d["AA"].Add(new Employee { Name = "C" });
d["AA"].Add(new Employee { Name = "C" });

d.Add("BB", new SortedSet<Employee>(new EmployeeComparer()));
d["BB"].Add(new Employee { Name = "E"} );
d["BB"].Add(new Employee { Name = "F"} );
d["BB"].Add(new Employee { Name = "A"} );

d.Add("CC", new SortedSet<Employee>(new EmployeeComparer()));
d["CC"].Add(new Employee { Name = "Z"} );
d["CC"].Add(new Employee { Name = "X"} );
d["CC"].Add(new Employee { Name = "Y"} );

d.Add("DD", new SortedSet<Employee>(new EmployeeComparer()));
d["DD"].Add(new Employee { Name = "Z" });
d["DD"].Add(new Employee { Name = "U" });
d["DD"].Add(new Employee { Name = "U" });
d["DD"].Add(new Employee { Name = "Q" });
d["DD"].Add(new Employee { Name = "Y" });

foreach (var a in d)
{
Console.WriteLine(a.Key);
foreach (var e in a.Value)
{
Console.WriteLine("\t" + e.Name);
}
}
Console.ReadKey();
}
}

public class EmployeeComparer : IEqualityComparer<Employee>,
IComparer<Employee>
{
public EmployeeComparer() { }

public bool Equals(Employee x, Employee y)
{
return String.Equals(x.Name, y.Name);
}

public int GetHashCode(Employee obj)
{
return obj.Name.GetHashCode();
}

public int Compare(Employee x, Employee y)
{
return String.Compare(x.Name, y.Name);
}
}
}

最佳答案

这根本不是魔法。 SortedSet<T> 的实现调用 Compare (在您的示例中,没有调用 GetHashCodeEquals )。就像您执行以下操作一样:

void Main()
{
MyMethod(new EmployeeComparer());
}
void MyMethod(IComparer<Employee> comparer)
{
var emp1 = new Employee { Name = "George" };
var emp2 = new Employee { Name = "Tom" };
Console.WriteLine(comparer.Compare(emp1, emp2));
}

来自 Main的角度来看,它从未调用过Compare , 它只做了一个 new .它调用的方法是执行 Compare 的方法。 .

关于c# - 以下代码如何调用 Equals、GetHashCode 和 Compare? (神秘),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23200413/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com