2

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

What does the ?? notation mean here?

Am I right in saying: Use id, but if id is null use string "ALFKI" ?

public ActionResult SelectionClientSide(string id)
        {
            ViewData["Customers"] = GetCustomers();
            ViewData["Orders"] = GetOrdersForCustomer(id ?? "ALFKI");
            ViewData["id"] = "ALFKI";
            return View();
        }
        [GridAction]
        public ActionResult _SelectionClientSide_Orders(string customerID)
        {
            customerID = customerID ?? "ALFKI";
            return View(new GridModel<Order>
            {
                Data = GetOrdersForCustomer(customerID)
            });
        }
Community
  • 1
  • 1
baron
  • 11,011
  • 20
  • 54
  • 88

3 Answers3

4

That's the null-coalescing operator.

var x = y ?? z;

// is equivalent to:
var x = (y == null) ? z : y;

// also equivalent to:
if (y == null) 
{
    x = z;
}
else
{
    x = y;
}

ie: x will be assigned z if y is null, otherwise it will be assigned y.
So in your example, customerID will be set to "ALFKI" if it was originally null.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
2

It's the null coalescing operator: http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

It provides a value (right side) when the first value (left side) is null.

Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
1

It means "if id or customerID is null, pretend it's "ALFKI" instead.

sblom
  • 26,911
  • 4
  • 71
  • 95