C# - 将 List.Find() 与自定义对象一起使用

发布于 2024-10-08 13:23:33 字数 1434 浏览 0 评论 0原文

我正在尝试将 List 与我的自定义类一起使用,并且能够使用 Contains()Find()< 等方法/code>等就行了。我以为我只需要重载运算符 == 但显然,一种方法是将委托方法与 Find() 一起使用...

注意:现在,我已经重载了 Equals() 方法以使 Contains() 方法正常工作,但我仍然无法获得 Find( ) 功能正常工作。

让两者同时工作的最佳方式是什么?

我在 Linux 上使用最新的 C# /.NET Framework 版本和 Mono。

编辑:这是我的代码

using System;
namespace GuerreDesClans
{
public class Reponse : IEquatable<Reponse>
{
    public Reponse ()
    {
        m_statement = string.Empty;
        m_pointage = 0;
    }

    public Reponse (string statement, int pointage)
    {
        m_pointage = pointage;
        m_statement = statement;
    }


    /*
     * attributs privés
     */

    private string m_statement;
    private int m_pointage;


    /*
     * properties
     */

    public string Statement {
        get { return m_statement; }
        set { m_statement = value; }
    }

    public int Pointage {
        get { return m_pointage; }
        set { m_pointage = value; }
    }

    /*
     * Equatable
     */

    public bool Equals (Reponse other)
    {
        if (this.m_statement == other.m_statement)
            return true;
        else
            return false;
    }
}

}

以及我想如何使用 find() 函数搜索我的响应对象...

list.find("statement1"); // would return a Reponse object

I'm trying to use a List<T> with a custom class of mine, and being able to use methods like Contains(), Find(), etc., on the list. I thought I'd just have to overload the operator == but apparently, one way of doing that is to use a delegate method with the Find()...

Note: Right now, I've overloaded the Equals() method to get the Contains() method to work, but I still couldn't get the Find() function to work.

What would be the best way of getting both to work?

I'm using the latest C# /.NET framework version with mono, on linux.

edit: Here's my code

using System;
namespace GuerreDesClans
{
public class Reponse : IEquatable<Reponse>
{
    public Reponse ()
    {
        m_statement = string.Empty;
        m_pointage = 0;
    }

    public Reponse (string statement, int pointage)
    {
        m_pointage = pointage;
        m_statement = statement;
    }


    /*
     * attributs privés
     */

    private string m_statement;
    private int m_pointage;


    /*
     * properties
     */

    public string Statement {
        get { return m_statement; }
        set { m_statement = value; }
    }

    public int Pointage {
        get { return m_pointage; }
        set { m_pointage = value; }
    }

    /*
     * Equatable
     */

    public bool Equals (Reponse other)
    {
        if (this.m_statement == other.m_statement)
            return true;
        else
            return false;
    }
}

}

and how I would like to search my Reponse objects using the find() function...

list.find("statement1"); // would return a Reponse object

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

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

发布评论

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

评论(5

一直在等你来 2024-10-15 13:23:33

Find() 将查找与作为参数传递的谓词匹配的元素,因此它与 Equals() 或 == 运算符无关。

var element = myList.Find(e => [some condition on e]);

在本例中,我使用 lambda 表达式 作为谓词。您可能想阅读此内容。对于 Find(),您的表达式应采用一个元素并返回一个布尔值。

在您的情况下,这将是:

var reponse = list.Find(r => r.Statement == "statement1")

为了回答评论中的问题,这与引入 lambda 表达式之前的 .NET 2.0 中的内容相同:

var response = list.Find(delegate (Response r) {
    return r.Statement == "statement1";
});

Find() will find the element that matches the predicate that you pass as a parameter, so it is not related to Equals() or the == operator.

var element = myList.Find(e => [some condition on e]);

In this case, I have used a lambda expression as a predicate. You might want to read on this. In the case of Find(), your expression should take an element and return a bool.

In your case, that would be:

var reponse = list.Find(r => r.Statement == "statement1")

And to answer the question in the comments, this is the equivalent in .NET 2.0, before lambda expressions were introduced:

var response = list.Find(delegate (Response r) {
    return r.Statement == "statement1";
});
花心好男孩 2024-10-15 13:23:33

您可以将 find 与谓词一起使用,如下所示:

list.Find(x => x.Id == IdToFind);

这将返回列表中满足谓词定义的条件的第一个对象(即,在我的示例中,我正在查找具有 ID 的对象)。

You can use find with a Predicate as follows:

list.Find(x => x.Id == IdToFind);

This will return the first object in the list which meets the conditions defined by the predicate (ie in my example I am looking for an object with an ID).

残月升风 2024-10-15 13:23:33

以前的答案没有考虑到您已经重载了等于运算符并使用它来测试所查找的元素这一事实。在这种情况下,您的代码将如下所示:

list.Find(x => x == objectToFind);

或者,如果您不喜欢 lambda 语法,并且已覆盖 object.Equals(object) 或已实现 IEquatable,您可以执行以下操作:

list.Find(objectToFind.Equals);

Previous answers don't account for the fact that you've overloaded the equals operator and are using that to test for the sought element. In that case, your code would look like this:

list.Find(x => x == objectToFind);

Or, if you don't like lambda syntax, and have overriden object.Equals(object) or have implemented IEquatable<T>, you could do this:

list.Find(objectToFind.Equals);
初与友歌 2024-10-15 13:23:33

http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx

        // Find a book by its ID.
        Book result = Books.Find(
        delegate(Book bk)
        {
            return bk.ID == IDtoFind;
        }
        );
        if (result != null)
        {
            DisplayResult(result, "Find by ID: " + IDtoFind);   
        }
        else
        {
            Console.WriteLine("\nNot found: {0}", IDtoFind);
        }

http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx

        // Find a book by its ID.
        Book result = Books.Find(
        delegate(Book bk)
        {
            return bk.ID == IDtoFind;
        }
        );
        if (result != null)
        {
            DisplayResult(result, "Find by ID: " + IDtoFind);   
        }
        else
        {
            Console.WriteLine("\nNot found: {0}", IDtoFind);
        }
提赋 2024-10-15 13:23:33

很简单,只需使用
list.Find(x => x.name == "stringNameOfObjectToFind");

It's easy, just use
list.Find(x => x.name == "stringNameOfObjectToFind");

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