ConfigurationManager & Static Class

前端 未结 3 1496
故里飘歌
故里飘歌 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:31

    static constructors are fine, the great thing about them is that they are guaranteed by the runtime to be executed once and once only - the first time the class is used in any circumstance.

    You could alternatively use the coalesce operator (??) to set a default value:

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

提交回复
热议问题