Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 562
PlushEngineCell Avatar asked Jun 16 '16 06:06

PlushEngineCell


People also ask

What does * mean after a variable in C?

When used with an already declared variable, the asterisk will dereference the pointer value, following it to the pointed-to place in memory, and allowing the value stored there to be assigned or retrieved.

What does *() mean in C?

It means that the variable is dereferenced twice. Assume you have a pointer to a pointer to char like this: char** variable = ...; If you want to access the value this pointer is pointing to, you have to dereference it twice: **variable.

What means * After the variable?

It means you're passing the variable by reference. In fact, in a declaration of a type, it means reference, just like: int x = 42; int& y = x; declares a reference to x , called y .

What does ++ before a variable mean?

++x (pre-increment) means "increment the variable; the value of the expression is the final value" x++ (post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"


3 Answers

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))) 
like image 163
Dmitry Bychenko Avatar answered Oct 19 '22 04:10

Dmitry Bychenko


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.

like image 7
Mir Md Faysal Avatar answered Oct 19 '22 05:10

Mir Md Faysal


?. 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.

like image 4
Manish kumar Avatar answered Oct 19 '22 04:10

Manish kumar