The null-coalescing operator
?? 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 ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
The null-coalescing operator is right-associative, that is, an expression of the form
C#
a ?? b ?? c
is evaluated as
C#
a ?? (b ?? c)
The
?? operator can be useful in the following scenarios:Paanch:
Peek -- points to an oldest element in the queue without removing it.
Example:
Queue<string> myQueue = new Queue<string>();
myQueue.Enqueue("1");
myQueue.Enqueue("2")
Then,
myQueue.Peek(); //will result "1"
No comments:
Post a Comment