我知道可空值类型的机制。但是,我对以下内容感兴趣:
Nullable 值类型正在使用结构(来自https://referencesource.microsoft.com/#mscorlib/system/nullable.cs,ffebe438fd9cbf0e)
public struct Nullable<T> where T : struct {
public Nullable(T value) {
/* ... */
}
public bool HasValue {get;}
public T Value {get;}
}
我可以像这样使用
Nullable<int> i = null;
现在,我以同样的方式创建自己的 Nullable-Struct:
public struct MyNullable<T> where T : struct {
public MyNullable(T value) {
/* ... */
}
public bool HasValue {get;}
public T Value {get;}
}
为什么我做不到
MyNullable<int> i = null;
现在?
我知道,struct 的值不能为 null - 但是为什么 struct Nullable 的值可以为 null?允许这样做的机制在哪里?
Smart猫小萌
相关分类