Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variables in ASP.NET

Tags:

asp.net

There is an article that recommends to store ASP.NET application state in static members of HttpApplication class (in Global.asax.cs).

What about storing application state in static members of other classes?

I tried to do so and it seems that there are several instances of these variables can exist (single instance per AppDomain?). Is it true and should we always use only Application class's static fields? Or it doesn't matter?

like image 216
alexey Avatar asked Mar 02 '26 04:03

alexey


1 Answers

It works pretty well and it's better than Application in many cases (it's strongly typed, for instance). Just make sure you are aware of threading and locking issues.

As a personal experience, I've managed to cache configuration information for ASP.NET app in a static class in a couple Web sites.

like image 76
mmx Avatar answered Mar 03 '26 16:03

mmx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!