ほとんどの人は、IComparable<T> を実装する参照型 (クラス) を記述するときに、null は実際のオブジェクトよりも小さいという規則を使用します。しかし、逆の規則を使用しようとすると、興味深いことが起こります。
using System;
using System.Collections.Generic;
namespace SortingNulls
{
internal class Child : IComparable<Child>
{
public int Age;
public string Name;
public int CompareTo(Child other)
{
if (other == null)
return -1; // what's your problem?
return this.Age.CompareTo(other.Age);
}
public override string ToString()
{
return string.Format("{0} ({1} years)", this.Name, this.Age);
}
}
internal static class Program
{
private static void Main()
{
var listOfChilds = new List<Child>
{
null,
null,
null,
null,
new Child { Age = 5, Name = "Joe" },
new Child { Age = 6, Name = "Sam" },
new Child { Age = 3, Name = "Jude" },
new Child { Age = 7, Name = "Mary" },
null,
null,
null,
null,
new Child { Age = 7, Name = "Pete" },
null,
new Child { Age = 3, Name = "Bob" },
new Child { Age = 4, Name = "Tim" },
null,
null,
};
listOfChilds.Sort();
Console.WriteLine("Sorted list begins here");
for (int i = 0; i < listOfChilds.Count; ++i)
Console.WriteLine("{0,2}: {1}", i, listOfChilds[i]);
Console.WriteLine("Sorted list ends here");
}
}
}
上記のコードを実行すると、null 参照が期待どおりにソートされていないことがわかります。どうやら、A と B を比較するときに、A がオブジェクトで B が null の場合、ユーザー定義の比較が使用されますが、逆に A が null で B がオブジェクトの場合は、代わりに何らかの BCL 比較が使用されます。
これはバグですか?