Question Mark (?) after session variable reference – What does that mean

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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();

Method 2

It performs a null-check on Session("LightBoxID") before attempting to call .ToString() on it.

MS Docs: Null-conditional operators ?. and ?[]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x