What is the best way to determine a session variable is null or empty in C#?

后端 未结 12 2644
野趣味
野趣味 2020-11-29 15:33

What is the best way to check for the existence of a session variable in ASP.NET C#?

I like to use String.IsNullOrEmpty() works for strings and wonder

12条回答
  •  -上瘾入骨i
    2020-11-29 15:59

    This method also does not assume that the object in the Session variable is a string

    if((Session["MySessionVariable"] ?? "").ToString() != "")
        //More code for the Code God
    

    So basically replaces the null variable with an empty string before converting it to a string since ToString is part of the Object class

提交回复
热议问题