Alex Rivera | Logout

What does a double question mark do in C#?

Asked 2009-10-22T15:49:46.290
92

Possible Duplicates:
?? Null Coalescing Operator --> What does coalescing mean?
What do two question marks together mean in C#?

I couldn't find this question being asked here so I figured I would ask it. What does a double question mark do in C#?

Example:

x = y ?? z;
Edit
Report

3 Answers

107

This is a null coalescing operator. The method above states x is assigned y's value, unless y is null, in which case it is assigned z's value.

answered 2009-10-22T15:51:59.650
34

From Wikipedia:

It's the null-coalesce operator and shorthand for this:

x = (y != null ? y : z);
answered 2009-10-22T15:53:14.083
28

Use y if not null, otherwise use z.

answered 2009-10-22T15:51:43.433

Your Answer