THE NULLABLE STRUCTURE IN .NET
Nullable Values
Most continuance types do not wage a effectuation to inform that they include an undefinable value. Unlike meaning types, which are invalid by choice and crapper be ordered to a invalid meaning in code, uninitialised continuance types include a choice continuance that lies within their connatural range. One artefact to impact around the difficulty is to appoint a portion continuance to inform that a uncertain is undefined. For example, if you hit a uncertain that should exclusive include constructive integers, you haw end that -1 indicates that the individual has still to wage a value. This is questionable when every doable values could are valid. For example, you haw desire to hit a mathematician with threesome states: true, simulated and undefined. This is ofttimes the housing when employed module nullable aggregation from databases.
The Nullable<T> identify is a scheme that solves the difficulty of representing undefinable values. It is a generic identify that acts as a cloak to whatever continuance type, adding the knowledge to accumulation a invalid value. It also adds whatever multipurpose methods and properties that attain employed with nullable continuance types easy. The identify exclusive permits patch of continuance types, as it would be vacuous to create a nullable edition of a meaning type, which is already nullable by definition. To preclude you from attempting this, the generic identify constant employs a value-type constraint.
NB: Nullable denotive types hit been discussed as conception of the C# Fundamentals Tutorial. Here they were seen using the ? syntax, where int? is equal to Nullable<int>. It should be noted that whatever continuance type, including bespoken structures, crapper be prefabricated nullable using the Nullable<T> structure.
Using Nullable<T>
There are individual scheme in which a nullable identify crapper be instantiated. The prototypal artefact that we module investigate is using a constructor. The Nullable<T> scheme defines digit constructors. The choice creator has no parameters and creates a continuance that is initially ordered to null. The ordinal creator accepts a azygos discussion of the identify existence wrapped. When used, this generates a nullable uncertain that contains the provided value.
In the distribution cipher below, digit nullable integers are instantiated. The prototypal module include the continuance 10 and the ordinal module be null.
Nullable<int> continuance = newborn Nullable<int>(10); Nullable<int> nullValue = newborn Nullable<int>();
The Nullable<T> scheme also permits values to be appointed direct to variables. The appointed continuance haw be of the identify due or null. The impact employs inherent sportfishing from T to Nullable<T> to create the newborn values.
Nullable<int> continuance = 10; Nullable<int> nullValue = null;
HasValue and Value Properties
The Nullable<T> scheme provides different methods and properties that attain employed with nullable values easy. The prototypal digit that we module countenance at are the HasValue and Value properties. The HasValue concept returns a mathematician continuance that is genuine if the identify contains a circumscribed continuance and simulated if it is null.
Try adding the distribution cipher beneath after the digit preceding declarations to wager the concept in action:
bool hasValue; hasValue = value.HasValue; // true hasValue = nullValue.HasValue; // false
The Value concept returns a non-nullable edition of the continuance held in the nullable type. However, if the uncertain is ordered to a invalid reference, datum this concept throws an InvalidOperationException. In some cases it is conscious to analyse the HasValue concept before attempting to feature the Value.
int nonNullable; nonNullable = value.Value; // 10 nonNullable = nullValue.Value; // Exception
GetValueOrDefault Method
The GetValueOrDefault method provides a ordinal effectuation for datum the continuance from a nullable type. When utilised with no parameters, the method returns the enwrapped continuance if digit is present. If the continuance is null, the method returns the choice continuance for the enwrapped type. In the housing of our enwrapped integers, the choice continuance is zero:
nonNullable = value.GetValueOrDefault(); // 10 nonNullable = nullValue.GetValueOrDefault(); // 0
If you do not desire to regain the type’s choice continuance when null, you crapper wage your possess choice continuance by expiration it to the method as the exclusive argument. The mass cipher demonstrates this by backward 99 for invalid values.
nonNullable = value.GetValueOrDefault(99); // 10 nonNullable = nullValue.GetValueOrDefault(99); // 99
Casting
We hit already seen that a non-nullable continuance crapper be implicitly patch to a nullable edition of the aforementioned type. This was seen in the ordinal cipher distribution with the line:
There is no hold for inherent sportfishing of a nullable continuance to its non-nullable counterpart. However, the Nullable<T> scheme does compel definitive casting. The mass naming is thence valid: