Also known as the “Null-Conditional Operator”. In other words, “operations based on whether an object is Null”, AKA
Nothing in VB.Net.
Some like to think that
?. looks like two eyes and a lock of hair, and call it the “Elvis Operator”. I would prefer to call it the “Superman Operator”, except for the fact that it doesn’t resemble pair of eyes and hair at all. Not on my font, anyway. And I don’t want it to, I want to see code. Without hair getting in the way, so to speak.
So I’m afraid Elvis has left the building, and apparently Superman is dead. And I grew up on the classic TV series Lois and Clark. And in a funny coincidence, Perry White of The Daily Planet is obsessed with both Superman and Elvis Presley. Also funny how I just started watching it again shortly after I was learning about this.
But I digress. The only appropriate and logical term is
Hello Operator. This Operator literally asks the Variable, “Is anyone home?”. If there’s no response, the line of code short-circuits, and execution moves on.
At every step, the Hello Operator will do a Null-Conditional test on the Variable, asking if something is there. If none of the Objects has been Instantiated as New, the entire Statement is ignored. This avoids having to do an
If Variable IsNot Nothing AndAlso Variable.SubObject IsNot Nothing AndAlso ArrayOfSubObjects IsNot ... at each step.
Speaking of which, the
OrElse are Visual Basic’s equivalents of Null-Conditional Operators inside of an If Statement. If the first Condition is False, the rest of the statement won’t be evaluated, which would completely fail if Variable is Nothing.
You can also declare Variables as Nullable, which isn’t always possible with certain Types.
Public Variable As Boolean? means that
Variable can be True, False, or Nothing, meaning no value has ever been set.
The C# Language uses the
?? Operator to choose between two expressions. If the first expression is Null/Nothing, the second is used.
In Visual Basic this
?? Operator is done using the
If() Function, but with only two parameters. So
This = If(One, Two) is the equivalent of
This = One ?? Two.