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.

CAUTION FOR UNITY ENGINE PROGRAMMERS: This style of null check does not work on Unity.Object objects such as GameObject or MonoBehaviours as those override the equality operators to do a custom null check instead of the object itself becoming null!

Clone this wiki locally