Nullable Value Types | C#

Publicado el: 19 marzo 2022
en el 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


En esta página del sitio puede ver el video en línea Nullable Value Types | C# de Duración hora minuto segunda en buena calidad , que subió el usuario SharpDev 19 marzo 2022, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 126 veces y le gustó 6 a los espectadores. Disfruta viendo!