为什么struct不能有无参数构造函数?为CLR做这件事有什么问题?为什么不允许这样做?请解释一下,因为我不明白.
当我写作
Nullable<Nullable<DateTime>> test = null;
我收到编译错误:
The type 'System.Datetime?' must be a non-nullable value type in order to use it as a paramreter 'T' in the generic type or method 'System.Nullable<T>'
但Nullable<T>它是struct如此,它应该是不可空的.
所以我试着创建这个struct:
public struct Foo<T> where T : struct
{
private T value;
public Foo(T value)
{
this.value = value;
}
public static explicit operator Foo<T>(T? value)
{
return new Foo<T>(value.Value);
}
public static implicit operator T?(Foo<T> value)
{
return new Nullable<T>(value.value);
} …Run Code Online (Sandbox Code Playgroud)