ECMA-334: 11.4 Nullable types
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| C# Language Specification |
|
| © 2006 ECMA International |
11.4 Nullable types
A nullable type is a structure that combines a value of the underlying type together with a null indicator.
More precisely, an instance of a nullable type has two public read-only properties: HasValue, of type bool,
and Value, of the nullable type’s underlying type. HasValue is true for a non-null instance and false for a
null instance. When HasValue is true, the Value property returns the contained value. When HasValue is
false, an attempt to access the Value property results in an exception. A nullable type is classified as a value
type (§11.1).
- nullable-type:
- non-nullable-value-type
?
- non-nullable-value-type
The non-nullable-value-type specified before the ? modifier in a nullable type is called the underlying type
of the nullable type. The underlying type of a nullable type shall be any non-nullable value type or any type
parameter that is constrained (§25.7) to non-nullable value types (that is, any type parameter with a struct
constraint). The underlying type of a nullable type shall not be a nullable type or a reference type. [Example:
int?? and string? are invalid types. end example]
A nullable type can represent all values of its underlying type plus an additional null value.
T? and System.Nullable<T> denote the same type.