前提条件コード コントラクトとして、参照型が null になるのを防ぐためのラッパー クラスを作成しました。
public sealed class NotNullable<T>
where T : class
{
private T t;
public static implicit operator NotNullable<T>(T otherT)
{
otherT.CheckNull("Non-Nullable type");
return new NotNullable<T> {t = otherT};
}
public static implicit operator T(NotNullable<T> other)
{
return other.t;
}
}
これは問題なく動作しますが、Nullable を扱うときのように常にキャストが必要です。
public void Foo(NonNullable<Bar> bar)
{
Console.WriteLine((Bar)bar);
}
NonNullable 型のパラメーターをキャストせずに T 型のように動作させることは可能でしょうか? 仕様番号のように:
public string Foo(Bar! bar)