Null Operator

Doosan published on
2 min, 322 words

Categories: post

Tags: C#

Null-conditional operators (?.) (?[])

The null-conditional operator applies member access ?. or element access ?[] only when the operand is not null. Otherwise, it returns null.

var device = GetDevice();
if(device !=null && device.DeviceId > 0){
  //do something
}
...
// If device is null, device?.DeviceId is null and the whole comparison becomes false
var device = GetDevice();
if(device?.DeviceId > 0){
  //do something
}

More precisely, device?.DeviceId becomes a nullable value such as int?, and the next > 0 comparison becomes a bool. If device is null, the comparison result is false. If you want to make the fallback value explicit, use something like (device?.DeviceId ?? 0) > 0 instead of device?.DeviceId > 0.

if(PropertyChanged != null){
  PropertyChanged();
}
...
PropertyChanged?.Invoke();

Null-coalescing operators (??)

The null-coalescing operator ?? returns the left operand if it is not null. Otherwise, it returns the right operand.

int? a = null;
int b = a ?? 10;
//b is 10

Null-coalescing assignment (??=)

The null-coalescing assignment operator ??= assigns the right operand to the left operand only when the left operand evaluates to null.

int? a = null;
a ??= 10;
// now a is 10

Null-forgiving operator (T!)

In an enabled nullable annotation context, the null-forgiving operator suppresses nullable warnings for the preceding expression. At runtime, the expression x! evaluates to the result of the underlying expression x.

Person? person = FindPerson();

// nullable warning: person might be null
Console.WriteLine(person.Name);

// no nullable warning, but runtime behavior is unchanged
Console.WriteLine(person!.Name);

! tells the compiler, "I believe this is not null, so suppress the nullable warning." It does not add a runtime null check. If person is actually null, person!.Name can still throw NullReferenceException. Warnings do not block compilation by default, but if the project treats warnings as errors, they can fail the build.

Ref