vb.net 会话变量引用后的问号 (?) - 这意味着什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43074622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Question Mark (?) after session variable reference - What does that mean
提问by Sandeep Thomas
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
我不明白那里的问号(?) 是什么意思。没有谷歌搜索帮助我任何提示
回答by trashr0x
It performs a null-check on Session("LightBoxID")before attempting to call .ToString()on it.
它Session("LightBoxID")在尝试调用之前执行空检查.ToString()。
回答by Ofir Winegarten
It's the Null-Conditional Operator It's a syntactic sugar for null checking:
它是 Null-Conditional 运算符,它是用于空值检查的语法糖:
return str?.ToString();
will become
会变成
if (str == null)
{
return null;
}
return str.ToString();

