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

后端 未结 2 1871
小鲜肉
小鲜肉 2020-12-28 14:56

I have had a code snippet comes to modify. In there i found this such syntax.

Session(\"LightBoxID\")?.ToString()

I didn\'t understand what

相关标签:
2条回答
  • 2020-12-28 15:14

    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();
    
    0 讨论(0)
  • 2020-12-28 15:19

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

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

    0 讨论(0)
提交回复
热议问题