Why null
can implicit convert to System.Nullable<T>
like this:
int? val = null;
but self defined Nullable<T>
(modified from .net reference source) cannot assign null
, is there some compiler magic? Could anyone tell me more internal implimentation?
[Serializable]
public struct Nullable<T> where T : struct
{
private bool hasValue;
internal T value;
public Nullable(T value)
{
this.value = value;
this.hasValue = true;
}
public bool HasValue
{
get
{
return hasValue;
}
}
public T Value
{
get
{
if (!HasValue)
{
throw new Exception();
}
return value;
}
}
public T GetValueOrDefault()
{
return value;
}
public T GetValueOrDefault(T defaultValue)
{
return 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()
{
return HasValue ? value.GetHashCode() : 0;
}
public override string ToString()
{
return HasValue ? value.ToString() : "";
}
public static implicit operator Nullable<T>(T value)
{
return new Nullable<T>(value);
}
public static explicit operator T(Nullable<T> value)
{
return value.Value;
}
}
test code below, compile error
Nullable<int> x = null; //ERROR Cannot convert null to 'Nullable<int>' because it is a non-nullable value type