What does a double question mark do in C #?

Possible duplicates:
?? Null Coalescing Operator β†’ What does a merge mean? What do two question marks mean in C #?

I could not find this question being asked here, so I decided that I would ask about it. What does a double question mark do in C #?

Example:

x = y ?? z; 
+41
c #
Oct 22 '09 at 15:49
source share
7 answers

This is the operator of zero coalescence. For the method above states x, the value y is assigned if y is not equal to zero, in which case it is assigned the value z.

+48
Oct 22 '09 at 15:51
source share

Use y if not null, otherwise use z

+15
Oct 22 '09 at 15:51
source share

From Wikipedia :

This is the null coalescence operator and the abbreviation for this is:

 x = (y != null ? y : z); 
+11
Oct 22 '09 at 15:53
source share

If y is null, z will be set.

+7
Oct 22 '09 at 15:51
source share

If y is null, then z is assigned.

For example:

 x = Person.Name ?? "No Name"; 

If the name is null, it will be "No name"

+6
Oct 22 '09 at 15:52
source share

As others have claimed, this is a null coalescing operator.

MSDN information about this:

http://msdn.microsoft.com/en-us/library/ms173224.aspx

+1
Oct 22 '09 at 15:53
source share

.Net framework 2.0 supports null values ​​for Nullable types.

here, in this case, it says x is equal to y if it has some value (i.e. is not null), or equal to z

+1
Oct 22 '09 at 15:54
source share



All Articles