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();
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
expr1 ?? expr2
gives the value of expr1 if expr1 is not null. If it is null, it gives the value of expr2.