【发布时间】:2012-06-11 08:52:22
【问题描述】:
我将要从列表中删除的项目设置为 null,然后通过 IComparable 方法 CompareTo 对列表进行排序,以便 null 项目位于顶部...然后在列表上使用 RemoveRange 函数但无法如此.. . 我认为以下代码没有问题:
try
{
foreach (Invoice item in inv)
{
if (item.qty == 0)
{
item.CustomerName = null;
item.qty = 0;
i++;
}
}
inv.Sort();
inv.RemoveRange(0, i);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#region IComparable<Invoice> Members
public int CompareTo(Invoice other)
{
return this.CustomerName.CompareTo(other.CustomerName);
}
#endregion
错误发生在 inv.RemoveRange(0,i);说:无法比较数组中的两个元素
为什么会这样??
【问题讨论】:
-
null.CompareTo(null)? -
哦,是的! ...那我该如何解决这个问题呢?
-
排序方法不会把 null 的项目放在列表的顶部吗?
标签: list c#-4.0 sorting generic-list