Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何实现平等方法?_C# - Fatal编程技术网

C# 如何实现平等方法?

C# 如何实现平等方法?,c#,C#,在下面的例子中,我如何使clientList在第二个例子中包含5个客户端 我想要list.Contains方法只检查FName和LName字符串,并在检查是否相等时忽略年龄 struct client { public string FName{get;set;} public string LName{get;set;} public int age{get;set;} } 例1: List<client> clientList = new List<

在下面的例子中,我如何使clientList在第二个例子中包含5个客户端

我想要list.Contains方法只检查FName和LName字符串,并在检查是否相等时忽略年龄

struct client
{
    public string FName{get;set;}
    public string LName{get;set;}
    public int age{get;set;}
}
例1:

List<client> clientList = new List<client>();

for (int i = 0; i < 5; i++)
{
    client c = new client();
    c.FName = "John";
    c.LName = "Smith";
    c.age = 10;

    if (!clientList.Contains(c))
    {
        clientList.Add(c);
    }
}

//clientList.Count(); = 1
例2:

List<client> clientList = new List<client>();

for (int i = 0; i < 5; i++)
{
    client c = new client();
    c.FName = "John";
    c.LName = "Smith";
    c.age = i;

    if (!clientList.Contains(c))
    {
        clientList.Add(c);
    }
}

//clientList.Count(); = 5

创建一个实现IEqualityComparer的类,并在list.contains方法中传递对象

创建一个实现IEqualityComparer的类,并在list.contains方法中传递对象

假设您使用的是C 3.0或更高版本,请尝试以下操作:

public class Client : IEquatable<Client>
{
  public string PropertyToCompare;
  public bool Equals(Client other)
  {
    return other.PropertyToCompare == this.PropertyToCompare;
  }
}
下面的代码没有经过测试,但应该是正确的

List<client> clientList = new List<client>();

for (int i = 0; i < 5; i++)
{
    client c = new client();
    c.FName = "John";
    c.FName = "Smith";
    c.age = i;

    var b = (from cl in clientList
             where cl.FName = c.FName &&
                   cl.LName = c.LName
             select cl).ToList().Count() <= 0;

    if (b)
    {
        clientList.Add(c);
    }
}

假设您使用的是C 3.0或更高版本,请尝试以下操作:

下面的代码没有经过测试,但应该是正确的

List<client> clientList = new List<client>();

for (int i = 0; i < 5; i++)
{
    client c = new client();
    c.FName = "John";
    c.FName = "Smith";
    c.age = i;

    var b = (from cl in clientList
             where cl.FName = c.FName &&
                   cl.LName = c.LName
             select cl).ToList().Count() <= 0;

    if (b)
    {
        clientList.Add(c);
    }
}
在您的结构中重写和:

struct client
{
    public string FName { get; set; }
    public string LName { get; set; }
    public int age { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is client))
            return false;
        client c = (client)obj;

        return
            (string.Compare(FName, c.FName) == 0) &&
            (string.Compare(LName, c.LName) == 0);
    }

    public override int GetHashCode()
    {
        if (FName == null)
        {
            if (LName == null)
                return 0;
            else
                return LName.GetHashCode();
        }
        else if (LName == null)
            return FName.GetHashCode();
        else
            return FName.GetHashCode() ^ LName.GetHashCode();
    }
}
此实现处理所有边缘情况

阅读以了解为什么还应覆盖。

覆盖和在您的结构中:

struct client
{
    public string FName { get; set; }
    public string LName { get; set; }
    public int age { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is client))
            return false;
        client c = (client)obj;

        return
            (string.Compare(FName, c.FName) == 0) &&
            (string.Compare(LName, c.LName) == 0);
    }

    public override int GetHashCode()
    {
        if (FName == null)
        {
            if (LName == null)
                return 0;
            else
                return LName.GetHashCode();
        }
        else if (LName == null)
            return FName.GetHashCode();
        else
            return FName.GetHashCode() ^ LName.GetHashCode();
    }
}
此实现处理所有边缘情况


阅读以了解为什么您也应该覆盖。

您说过您希望忽略年龄,但您的两个示例暗示年龄确实被考虑在内。否则,我希望这两个示例在list@Jamiec是的,但我不想在第二个例子中把它考虑进去。你说你想不考虑年龄,但你的两个例子暗示年龄确实被考虑进去了。否则,我希望这两个示例在list@Jamiec是的,但我不希望在第二个示例中考虑它。没有列表。包含可以采用IEqualityComparer的方法版本-该方法是LINQ to对象的一部分,是在上声明的扩展方法IEnumerable@Saurabh正如MarcinJurazek提到的,IEqualityComparer不使用out LINQThere没有列表。包含可以使用IEqualityComparer的方法版本-该方法是LINQ to对象的一部分,是在上声明的扩展方法IEnumerable@Saurabh正如MarcinJurazek提到的,iQualityComparer在没有Linqt的情况下无法正常工作。这项工作非常完美,我还可以在结构上实现接口,因此效果更好:这非常好,我还可以在结构上实现接口,因此效果更好: