Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

vb.net

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

like image 261
Sandeep Thomas Avatar asked Mar 28 '17 16:03

Sandeep Thomas


People also ask

What does a question mark after a variable mean?

Question marks on TypeScript variable are used to mark that variable as an optional variable.

What is question mark after variable C#?

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.

What does question mark mean in asp net?

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.


2 Answers

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(); 
like image 169
Ofir Winegarten Avatar answered Sep 18 '22 23:09

Ofir Winegarten


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

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

like image 31
trashr0x Avatar answered Sep 21 '22 23:09

trashr0x