Static Operation of the Access Configuration Manager Class

I am considering creating a static class to handle all access to Web.config web windows. For example, it would look like this:

public static class ConfigManager { public static string Timeout = ConfigurationManager.AppSettings["Timeout"]; public static string Version = ConfigurationManager.AppSettings["Version"]; } 

I believe that this will give me a central place to change keys in the application settings if I want to change it in the future and give me intellisense for all configuration parameters in my application.

My question is how this will work, as I'm not sure how static works work under the hood. I hope that the first time I accessed one of the properties, all properties will be read from the configuration and put into memory, and that all subsequent hits will then simply go into memory instead of looking at the configuration. Unfortunately, this would mean that runtime changes in the configuration would not take effect. I also thought that it is possible that only the property I'm looking for will be loaded, or that they will all be loaded every time I access any property.

Does anyone know how to behave under the hood when the static property value from the configurations matters?

+4
source share
1 answer

Static means that there will be only one instance of this class or variable in memory.

Since you selected a static class, the values ​​will be set once by your assignment when invoking the static constructor. This will happen the first time you use a class.

At any time, when you access the variable after this, it will retrieve the value from memory.

If you are concerned about the possibility of changing values ​​at runtime, you can use the property instead, and then implement a caching strategy that will update this property over a period of time.

+2
source

All Articles