会话变量引用后的问号(?) - 这是什么意思

时间:2022-06-05 22:26:36

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

我不明白那个问号(?)是什么意思。没有谷歌搜索帮助我任何提示

2 个解决方案

#1


13  

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

它在尝试调用.ToString()之前对Session(“LightBoxID”)执行空值检查。

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

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

#2


12  

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

#1


13  

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

它在尝试调用.ToString()之前对Session(“LightBoxID”)执行空值检查。

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

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

#2


12  

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