Get the value of a nullable type

suggest change

Given following nullable int

int? i = 10;

In case default value is needed, you can assign one using null coalescing operator, GetValueOrDefault method or check if nullable int HasValue before assignment.

int j = i ?? 0;
int j = i.GetValueOrDefault(0);
int j = i.HasValue ? i.Value : 0;

The following usage is always unsafe. If i is null at runtime, a System.InvalidOperationException will be thrown. At design time, if a value is not set, you’ll get a Use of unassigned local variable 'i' error.

int j = i.Value;

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Nullable types:
* Get the value of a nullable type

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
37 Nullable types
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script