Nullable Value Types | C#

Publicado em: 19 Março 2022
no canal de: SharpDev
126
6

You can follow the playlist here:    • C# Using Directives  

A nullable value type T? represents all values of its underlying value type T and an additional null value.
E.g. for a bool?, true, false, null can be assigned to the variable.
E.g. Scenario you have to save and load an apartment number which is an integer value in a table whose column allows null value.
If the person doesn’t stay in an apartment, default integer value of 0 cannot be allowed to be saved.

Any value type is an instance of generic System.Nullable(T) structure.

Nullable(T) or T? forms are interchangeable.

Properties:
Nullable(T).HasValue - Indicates if instance of nullable type has value

Nullable(T).Value - gets the value of an underlying type if HasValue is true.
Remarks: If HasValue is false, Value property throws an InvalidOperationException.

Methods:
Nullable(T).GetValueOrDefault() - Retrieves the default value of the underlying type if no value has been set.

Nullable(T).GetValueOfDefault(T) - Returns the specified default value if no value has been set.

Nullable types and Is Operator:
DO NOT use the is operator to determine whether an instance is of nullable value type
Is operator cannot distinguish between types of a nullable value type instance and its underlying type instance

Key points to remember:
Nullable(T) or T? forms are interchangeable.
Accessing the Value property can throw InvalidOperationException
Use HasValue property before accessing the Value property.
Is operator cannot be used to distinguish between types of a nullable value type instance and its underlying type instance


Nesta página do site você pode assistir ao vídeo on-line Nullable Value Types | C# duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário SharpDev 19 Março 2022, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 126 vezes e gostou 6 espectadores. Boa visualização!