8

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

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

Community
  • 1
  • 1
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 7
    This question is asked more than ten times in stackoverflow itself. Some of them are here. 1. http://stackoverflow.com/questions/827454/what-is-the-operator-for 2. http://stackoverflow.com/questions/3925726/coalesce-operator-in-c 3. http://stackoverflow.com/questions/770096/what-does-mean. – Bipul Jan 31 '11 at 07:07

2 Answers2

19

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

object obj = canBeNull ?? alternative;

// equivalent to:
object obj = canBeNull != null ? canBeNull : alternative;
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
5

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");
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
programmer
  • 878
  • 5
  • 8