C# - 将 List.Find() 与自定义对象一起使用
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Find() 将查找与作为参数传递的谓词匹配的元素,因此它与 Equals() 或 == 运算符无关。
在本例中,我使用 lambda 表达式 作为谓词。您可能想阅读此内容。对于 Find(),您的表达式应采用一个元素并返回一个布尔值。
在您的情况下,这将是:
为了回答评论中的问题,这与引入 lambda 表达式之前的 .NET 2.0 中的内容相同:
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.
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:
And to answer the question in the comments, this is the equivalent in .NET 2.0, before lambda expressions were introduced:
您可以将 find 与谓词一起使用,如下所示:
这将返回列表中满足谓词定义的条件的第一个对象(即,在我的示例中,我正在查找具有 ID 的对象)。
You can use find with a Predicate as follows:
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).
以前的答案没有考虑到您已经重载了等于运算符并使用它来测试所查找的元素这一事实。在这种情况下,您的代码将如下所示:
或者,如果您不喜欢 lambda 语法,并且已覆盖 object.Equals(object) 或已实现 IEquatable,您可以执行以下操作:
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:
Or, if you don't like lambda syntax, and have overriden object.Equals(object) or have implemented IEquatable<T>, you could do this:
http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx
http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx
很简单,只需使用
list.Find(x => x.name == "stringNameOfObjectToFind");
It's easy, just use
list.Find(x => x.name == "stringNameOfObjectToFind");