What does mean "?" after variable in C#?

What does this condition mean?

if (!helper?.Settings.HasConfig ?? false)

P.S.

  • helper is variable of some class
  • Settings is some field
  • HasConfig is field too

4 Answers

Well, ?. is a null-conditional operator

x?.y

means return null if x is null and x.y otherwise

?? is a null-coalescing operator

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))) 

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.

?. Operator is known as the safe navigation operator introduced in C# 6. Null Conditional Operator Syntax

The null conditional operator (?.) is colloquially referred to as the "Elvis operator" because of its resemblance to a pair of dark eyes under a large quiff of hair. The null conditional is a form of a member access operator (the .). Here's a simplified explanation for the null conditional operator:

The expression A?.B evaluates to B if the left operand (A) is non-null; otherwise, it evaluates tonull.

Many more details fully define the behavior:

  • The type of the expression A?.B is the type of B, in cases where B is a reference type. If B is a value type, the expression A?.B is the nullable type that wraps the underlying value type represented by B.

  • The specification for the feature mandates that A be evaluated no
    more than once.

  • The null conditional operator short-circuits, which means that you
    can chain multiple ?.operators, knowing that the first null
    encountered prevents the remaining (rightmost) components of the
    expression from being evaluated.

Example:- Suppose we have a Student class

public class Student
{ public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; }
}

Assume that s represents a student. Consider these two statements:

var name = s?.FirstName;
var age = s?.Age;

The variable name is a string. The value of name depends on the value of s. If s is null, name is null. If s is not null, name is the value of s.FirstName. Note that s.FirstName may be null even when s is not.

The variable age is an int? (which is another way of specifying a Nullable). As with name, the value of age depends on the value of s. If s is null, age is an int? with no value. If s is non-null, age is the wrapped value of s.Age.

That's the basics. The power of this feature comes from all the scenarios where this feature enables cleaner code.

0

It will check if "helper" is not NULL before acessing "Settings" property to avoid NullReferenceException. "Old" way was like this: if (helper != null && !helper.Settings......).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like