The definition of Nullable<T>
is:
[SerializableAttribute]
public struct Nullable<T> where T : struct, new()
The constraint where T : struct
implies that T
can only be a value type. So I very well understand that I cannot write:
Nullable<string> a; //error. makes sense to me
Because string
is a reference type, not a value type. But I don't really understand why can't I write
Nullable<Nullable<int>> b; //error. but why?
Why is it not allowed? After all, Nullable<int>
is a value-type, and therefore, it can be type argument to Nullablle<T>
.
When I compiled it on ideone, it gives this error (ideone):
error CS0453: The type 'int?' must be a non-nullable value type in order to use it as type parameter 'T' in the generic type or method 'System.Nullable'
Compilation failed: 1 error(s), 0 warnings
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…