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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:13:55  来源:igfitidea点击:

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

c#asp.netvb.net

提问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()

MSDN: Null-conditional Operators (C# and Visual Basic)

MSDN:空条件运算符(C# 和 Visual Basic)

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