Skip to content

Null Operators

Mario edited this page Sep 6, 2024 · 7 revisions

Null Coalescing Operator

Returns an alternate value on null.

??

Card cardB = cardA ?? new Card("2C"); // If cardA is null, return new Card.

Null Conditional Operator

Provides safe method access. Also known as "Elvis operator."

?. and ?[]

Card cardA = deckA?.Get(1);
Card cardB = deckA?[2];

If deckA turns out to be null, it will return null and not call Get() or do the array access.

Clone this wiki locally