我想借助IComparable<T>.CompareTo
名为. 我写T
Path
var shortest = new List<Path>();
//Fill shortest with elements != null
if (shortest.Contains(null))
throw new System.Exception("Path is null");
shortest.Sort();
if (shortest.Contains(null))
throw new System.Exception("Path is null");
令我惊讶的是,该方法
int IComparable<Path>.CompareTo(Path other)
{
if (other == null)
return -1;
if (!other.valid)
return 1;
if (pfad.Count() > other.pfad.Count())
{
return -1;
}
else if (pfad.Count() < other.pfad.Count())
{
return 1;
}
else
{
if (length > other.length)
return -1;
else
return 1;
}
}
从课堂上
public class Path : IComparable<Path>
从Sort()
with调用other==null
。我更惊讶的是,在第一个代码块中,抛出了第二个异常,这意味着shortest
在排序之后而不是之前包含一个空值。