次の方法で共有


Cyrus’s Optional

 

While Refactoring the LazyLoader, we produced the code below.  It’s useful any time you want to add a sentinel value to a type.

 

interface IOptional<T>

{

    T Value { get;}

}

class None<T> : IOptional<T>

{

    public T Value

    {

        get { throw new System.NotImplementedException(); }

    }

}

class Some<T> : IOptional<T>

{

    readonly T value;

    public Some(T value)

    {

        this.value = value;

    }

    public T Value

    {

        get { return this.value; }

    }

}

Comments