ConfigurationManager & Static Class

前端 未结 3 1480
故里飘歌
故里飘歌 2021-01-03 03:06

I would like to use ConfigurationManager to access some string values from a static class. However, I need to handle specifically the absence of a valu

3条回答
  •  伪装坚强ぢ
    2021-01-03 03:45

    I'm using something like this:

    public static readonly string someStr  = 
            ConfigurationManager.AppSettings["abc"] ?? "default value";
    

    Or to handle empty string:

    public static readonly string someStr = 
               !String.IsNullOrEmpty(ConfigurationManager.AppSettings["abc"]) ? 
                                 ConfigurationManager.AppSettings["abc"] : "default value";
    

提交回复
热议问题