我需要显式实现标准 c# 接口,例如 ( IComparable
, IComparer
, IEquatable
, IEnumerable
, IEnumerator
)。我做对了吗?
class Lemon : IComparable
{
public String name { get; set; }
public int id { get; set; }
public Lemon (String name, int id)
{
this.name = name;
this.id = id;
}
int IComparable.CompareTo(object obj)
{
Lemon other = (Lemon)obj;
if (this.id > other.id)
return 1;
else if (this.id < other.id)
return -1;
else return 0;
}
public void diamond ()
{
Console.WriteLine();
}
public override string ToString()
{
return this.name + " " + this.id;
}
}
现在主要:
static void Main(string[] args)
{
List<IComparable> icL = new List<IComparable>();
IComparable temp = new Lemon("Git", 99);
icL.Add(temp);
icL.Add(new Lemon("Green", 9));
icL.Add(new Lemon("Don", 7));
icL.Add(new Lemon("Simon", 12));
icL.Sort();
foreach (IComparable itm in icL)
{
Console.WriteLine(itm.ToString());
}
Console.WriteLine("----------");
}
所以你怎么看?
另一个问题是当我遍历集合时如何访问方法 diamond ?