What is the proper way to check for null values?

前端 未结 10 1982
刺人心
刺人心 2020-12-04 08:05

I love the null-coalescing operator because it makes it easy to assign a default value for nullable types.

 int y = x ?? -1;

That\'s great,

10条回答
  •  [愿得一人]
    2020-12-04 08:50

    create an auxiliary function

    public static String GetValue( string key, string default )
    {
        if ( Session[ key ] == null ) { return default; }
        return Session[ key ].toString();
    }
    
    
    string y = GetValue( 'key', 'none' );
    

提交回复
热议问题