What does mean "?" after variable in C#?
Check the C# operator list:
x?.y – null conditional member access. Returns null if the left hand operand is null.
x ?? y – returns x if it is non-null; otherwise, returns y.
So helper?.Settings
will return null if helper
is null otherwise it will return helper.Settings
if helper.Settings
is not null and helper.Settings.HasConfig
is not null then it will return the value of helper.Settings.HasConfig
otherwise will return false.
N.B: if helper?.Settings
is null then NULL reference exception will occur.
Well, ?.
is a null-conditional operator
https://msdn.microsoft.com/en-us/library/dn986595.aspx
x?.y
means return null
if x
is null and x.y
otherwise
??
is a null-coalescing operator
https://msdn.microsoft.com/en-us/library/ms173224.aspx
x ?? y
means if x == null
return y
, otherwise x
Combining all the above
helper?.Settings.HasConfig ?? false
means: return false
if
helper == null or
helper.Settings.HasConfig == null
otherwise return
helper.Settings.HasConfig
The code without ??
and ?.
if
can be rewritten into cumbersome
if (!(helper == null
? false
: (helper.Settings.HasConfig == null
? false
: helper.Settings.HasConfig)))