Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is use of "??" [duplicate]

Possible Duplicate:
What is the “??” operator for?

Please explain me what is use of "??" in below code and what is "??" used for.

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

{ e.Description = "The Order Date must not be in the future."; return false; }

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

Thanks.

like image 415
Dr. Rajesh Rolen Avatar asked Oct 05 '10 09:10

Dr. Rajesh Rolen


People also ask

What is duplicate example?

always used before a noun. : exactly the same as something else. I began receiving duplicate copies of the magazine every month.

What does duplicate work mean?

It's called duplicate work – literally redoing work that's already been done – and you can just imagine what it's doing to your productivity.

What does done in duplicate mean?

Definition of in duplicate 1 : so that there are two copies We were required to fill out the paperwork in duplicate. 2 : with an exact copy Please send the contract in duplicate.

Does duplicate mean copy?

Duplicate creates a copy of an item in the same location as the original. Copying (or “Copy To”) creates a copy of an item in a different location that you specify.


1 Answers

(This is a duplicate, but it's hard to search for, so I'm happy enough to provide more another target for future searches...)

It's the null-coalescing operator. Essentially it evaluates the first operand, and if the result is null (either a null reference or the null value for a nullable value type) then it evaluates the second operand. The result is whichever operand was evaluated last, effectively.

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 will start with E1 and progress until it finds a non-null value.

The first operand has to be a nullable type, but e second operand can be non-nullable, in which case the overall expression type is non-nullable. For example, suppose E4 is an expression of type int (but all the rest are still int? then you can make x non-nullable:

int x = E1 ?? E2 ?? E3 ?? E4;
like image 67
Jon Skeet Avatar answered Sep 28 '22 06:09

Jon Skeet