c# null coalescing code example

Example 1: c# 8 null coalescing assignment

// C# 8 null coalescing assignment operator:
a ??= b;
// the same as:
a = a ?? b;

Example 2: null coalescing operator c#

//the null coalescing operator for c# is ??
int? x = null;
int y = 9;
return x ?? y; 
//Will return the value of x if x is not null else return y

Example 3: c# null conditional

//Return stirng representation of nullable DateTime
DateTime? x = null;
return x.HasValue == true ? x.Value.ToString() : "No Date";

Example 4: c# null accessor

int? length = people?.Length; // null if people is null

Example 5: ? : in c#

is this condition true ? yes : no

Tags:

Misc Example