List.Contains在对象比较上失败 [英] List.Contains fails on object comparison

查看:200
本文介绍了List.Contains在对象比较上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类"Class1",在.NET 2.0中它具有字符串变量"sText".我已经创建了"lstClass1"类的对象列表.设置其字符串变量后,它将存储该类的许多对象.

I have a class "Class1" that has a string variable "sText" in .NET 2.0. I have created a list of object of that class "lstClass1". It stores many objects of that class after setting its string variable.

完整的代码是:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (!lstClass1.Contains(new Class1("text1")))
            lstClass1.Add(new Class1("text1"));
    }

    public List<Class1> lstClass1 = new List<Class1>();
}

public class Class1
{
    public Class1(string sText)
    {
        this.sText = sText;
    }

    public string sText = "";
}

现在,问题是我只想添加具有带有唯一文本值的字符串的类对象.例如,如果lstClass1已经具有字符串变量值"text1"的对象,则它不应允许添加也具有"text1"的对象.所以,我写了代码:

Now, the problem is that I only want to add class objects that has string with unique text value. For example, if lstClass1 already has an object with string variable value "text1" then it should not allow addition of an object that also has "text1". So, I wrote code:

if (!lstClass1.Contains(new Class1("text1")))
    lstClass1.Add(new Class1("text1"));

但是,即使列表中已经存在带有"text1"字符串的对象,也始终允许添加文本"text1".我的假设是,在第一个按钮单击事件"button1_Click"上将添加对象,因为列表为空,但是在下一个按钮上单击List.Contains函数将检查列表中是否已存在带有字符串变量"text1"的对象,并且如果找到,则不会添加.但是,即使列表中已经存在对象,也始终允许添加带有文本"text1"的对象.

But it always allows the text "text1" to be added even when there is already an object in the list with "text1" string. My assumption was that on first button click event "button1_Click" the object will be added because the list is empty but on next button click the List.Contains function will check if there is already an object in the list with string variable "text1" and if found then it will not be added. But it always allow to add object with text "text1" even if it is already present in the list.

请注意:我没有采用简单的字符串列表或字符串列表,因为我想以一种简单的方式来解释我的大型列表,类和对象问题.

Please note: I have not taken a simple list of strings or an of string because I want to explain my large problem of list, classes and objects in a simple way.

推荐答案

使用 Any() 方法:

if (!lstClass1.Any(x => x.sText == "text1"))
    lstClass1.Add(new Class1("text1"));

此代码:

if (!lstClass1.Contains(new Class1("text1")))
    lstClass1.Add(new Class1("text1"));

仅当您为Class1提供Equals()GetHashCode()方法以允许在此类的两个对象之间进行比较时才有效.为此,您的类应实现IEquatable接口. 所以您的Class1可能看起来像这样:

Could only work if you would provide the Equals() and GetHashCode() methods for your Class1 to enable making the comparisons between two objects of this class. To achieve this your class should implement the IEquatable interface. So your Class1 could look like this:

public class Class1 : IEquatable<Class1>
{
    public Class1(string sText)
    {
        this.sText = sText;
    }

    public string sText = "";

    public bool Equals(Class1 other) 
    {
      if (other == null) 
         return false;

      if (this.sText == other.sText)
         return true;
      else
         return false;
    }

    public override int GetHashCode()
    {
      return this.sText.GetHashCode();
    }
}

这篇关于List.Contains在对象比较上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆