【发布时间】:2011-05-28 14:21:08
【问题描述】:
我正在尝试将List<T> 与我的自定义类一起使用,并且能够使用列表中的Contains()、Find() 等方法。我以为我只需要重载运算符 == 但显然,这样做的一种方法是使用带有 Find() 的委托方法...
注意:现在,我重载了Equals() 方法以使Contains() 方法工作,但我仍然无法让Find() 函数工作。
让两者发挥作用的最佳方式是什么?
我在 linux 上使用最新的 C# /.NET 框架版本和单声道。
编辑:这是我的代码
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
【问题讨论】:
标签: c# list generics delegates find