What "??"

Possible duplicate:
What "??" operator for?

Please explain to me what "??" in the lower code and what is "??" is used for.

if ((this.OrderDate ?? DateTime.MinValue) > DateTime.Today) 

{e.Description = "Order date should not be in the future"; return false; }

the above code is at http://nettiers.com/EntityLayer.ashx

Thanks.

+6
c # .nettiers
source share
1 answer

(This is a duplicate, but it’s hard to find, so I’m satisfied enough to provide another goal for future searches ...)

This is a null-coalescing operator. Essentially, it evaluates the first operand, and if the result is null (either a null reference or a null value for the NULL value type), then it evaluates the second operand. The result - no matter which operand is evaluated last, is effective.

Note that due to its associativity, you can write:

 int? x = E1 ?? E2 ?? E3 ?? E4; 

if E1 , E2 , E3 and E4 are all expressions of type int? - it starts with E1 and lasts until it finds a nonzero value.

The first operand must be a type with a null value, but the second operand may not be null, in which case the general type of the expression is not NULL. For example, suppose E4 is an expression of type int (but everyone else remains int? , then you can make x non-nullable:

 int x = E1 ?? E2 ?? E3 ?? E4; 
+9
source share

All Articles