Nullable Value Types | C#

Опубликовано: 19 Март 2022
на канале: 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


На этой странице сайта вы можете посмотреть видео онлайн Nullable Value Types | C# длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь SharpDev 19 Март 2022, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 126 раз и оно понравилось 6 зрителям. Приятного просмотра!