Static life cycle

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

How static are static variables in ASP.NET?

This is not a real example, but it's good enough to make the point. Say I
have a custom control with a dictionary. The dictionary is composed of
10,000 strings read from a file and hashed. Obviously I don't want/need to
rebuild the data structure for each page render.

If it were a traditional application, I would use a static constructor and
hashtable. Can I expect this to work in IIS?

I ask because .net seems to rebuild projects on key file changes (like
global.asax). Does this feature destroy the usefullness of static
variables?

Thanks,
Brian
 
static variables are very static - they are shared for all requests
throughout the lifetime of the application (or until an app domain restart).
This is different than the behaviour you'd see in php.

They aren't thread-safe...so you should only write to them in controlled
settings.

Generally, the HttpCache is used for what you are talking about.

Karl
 
static have the same life cycle as the appdomain they are contained in. in
asp.net trems that means they live until a recycle (file change, inactivity
timeout, etc).

-- bruce (sqlwork.com)
 
Back
Top