Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does double question marks mean in c# [duplicate]

Tags:

c#

.net

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

Debugging some code and found ?? inside the code. What does this mean?

like image 808
user496949 Avatar asked Jan 31 '11 06:01

user496949


People also ask

What is the meaning of '?' In C?

Most likely the '?' is the ternary operator. Its grammar is: RESULT = (COND) ? ( STATEMEN IF TRUE) : (STATEMENT IF FALSE) It is a nice shorthand for the typical if-else statement: if (COND) { RESULT = (STATEMENT IF TRUE); } else { RESULT = (STATEMENT IF FALSE);

What is the double question mark operator?

The nullish coalescing operator ( ?? ) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined , and otherwise returns its left-hand side operand.

Why do we use double question marks in C#?

?? and ??= operators (C# reference)returns the value of its left-hand operand if it isn't null ; otherwise, it evaluates the right-hand operand and returns its result. The ??

What do question marks mean in C?

The question mark operator, ?:, is also found in C++. Some people call it the ternary operator because it is the only operator in C++ (and Java) that takes three operands. If you are not familiar with it, it's works like an if-else, but combines operators.


2 Answers

?? is the null-coalescing operator for nullable types.

object obj = canBeNull ?? alternative;

// equivalent to:
object obj = canBeNull != null ? canBeNull : alternative;
like image 126
Mitch Wheat Avatar answered Sep 27 '22 20:09

Mitch Wheat


http://msdn.microsoft.com/en-us/library/ms173224.aspx refer to this for description. it's an operator

The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type.

    // ?? operator example.
    int x = null;

    // y = x, unless x is null, in which case y = -1.
    int y = x ?? -1;

    // Assign i to return value of method, unless
    // return value is null, in which case assign
    // default value of int to i.
    int i = GetNullableInt() ?? default(int);

    string s = GetStringValue();
    // ?? also works with reference types. 
    // Display contents of s, unless s is null, 
    // in which case display "Unspecified".
    Console.WriteLine(s ?? "Unspecified");
like image 42
programmer Avatar answered Sep 27 '22 19:09

programmer