I have had a code snippet comes to modify. In there i found this such syntax.
Session("LightBoxID")?.ToString()
I didn't understand what is that Question mark (?) there means. No googling helped me about any hint
Question marks on TypeScript variable are used to mark that variable as an optional variable.
With C# 8, the placing of a question mark character on ANY type will now be treated as an indicator of weather a variable may or may not be null, not in terms of preventing compilation outright, but in terms of how the compiler responds and warns about it.
The question mark after the Grade type declaration indicates that the Grade property is nullable. A grade that's null is different from a zero grade — null means a grade isn't known or hasn't been assigned yet.
It's the Null-Conditional Operator It's a syntactic sugar for null checking:
return str?.ToString();
will become
if (str == null) { return null; } return str.ToString();
It performs a null-check on Session("LightBoxID")
before attempting to call .ToString()
on it.
MS Docs: Null-conditional operators ?.
and ?[]
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