Is foo as? Foo a complete equivalent of foo as Foo? ?
If yes then why have both?
If no then what's the difference?
as? is the safe cast operator.
Usually, if you try to cast a variable, and it fails, you get a ClassCastException. Using this operator, it just returns null instead in this case.
This means that the return type of the foo as? Foo expression is actually Foo?, because it might return null.
foo as Foo? is a cast of foo to the nullable Foo? type, this can still produce an exception if the variable isn't of that type (i.e. it's not a Foo instance or null).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With