-2

I know that single ? is used to make a variable nullable, but what does double ?? mean in below code?

txtId.Text = Convert.ToDecimal(conn.LeavesRequests.Max(lr => (decimal?)lr.Id) + 1 ?? 1).ToString();
Kazem
  • 1,450
  • 2
  • 11
  • 13

2 Answers2

0

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

Here's the explanation for this operator:

int? x = null;
// Set y to the value of x if x is NOT null; otherwise,
// if x = null, set y to -1.
int y = x ?? -1;

So if the value of this Convert.ToDecimal(conn.LeavesRequests.Max(lr => (decimal?)lr.Id) + 1 is null then txtId.Text will get value 1 i.e right operand or the evaluated value of Convert.ToDecimal(conn.LeavesRequests.Max(lr => (decimal?)lr.Id) + 1

Rahul Nikate
  • 6,192
  • 5
  • 42
  • 54
0

expr1 ?? expr2 gives the value of expr1 if expr1 is not null. If it is null, it gives the value of expr2.

George
  • 2,436
  • 4
  • 15
  • 30