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

后端 未结 12 2649
野趣味
野趣味 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条回答
  •  抹茶落季
    2020-11-29 16:08

    It may make things more elegant to wrap it in a property.

    string MySessionVar
    {
       get{
          return Session["MySessionVar"] ?? String.Empty;
       }
       set{
          Session["MySessionVar"] = value;
       }
    }
    

    then you can treat it as a string.

    if( String.IsNullOrEmpty( MySessionVar ) )
    {
       // do something
    }
    

提交回复
热议问题