//语法糖 int? count=0; //完整写法 Nullable<int> count = 0;
Nullable类型定义
public partial struct Nullable<T> where T : struct { private readonly bool hasValue; // Do not rename (binary serialization) internal T value; // Do not rename (binary serialization) or make readonly (can be mutated in ToString, etc.) [NonVersionable] public Nullable(T value) { this.value = value; hasValue = true; } public readonly bool HasValue { [NonVersionable] get => hasValue; } public readonly T Value { get { if (!hasValue) { ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue(); } return value; } } [NonVersionable] public readonly T GetValueOrDefault() => value; [NonVersionable] public readonly T GetValueOrDefault(T defaultValue) => hasValue ? value : defaultValue; public override bool Equals(object? other) { if (!hasValue) return other == null; if (other == null) return false; return value.Equals(other); } public override int GetHashCode() => hasValue ? value.GetHashCode() : 0; public override string? ToString() => hasValue ? value.ToString() : ""; [NonVersionable] public static implicit operator Nullable<T>(T value) => new Nullable<T>(value); [NonVersionable] public static explicit operator T(Nullable<T> value) => value!.Value; }
标签:NonVersionable,Nullable,c#,hasValue,value,空值,readonly,类型,public From: https://www.cnblogs.com/ZGXF/p/17397308.html