spmason
spmason

Reputation: 4088

The effect of static properties in a web context

I need to change a static property on an object in our web application. The property has a default value that is hard-coded into the object. If I change the static property in my Application_Start does that change stick:

A) Forever (well, until the app is recycled)

B) Until the object is GC'd then re-inialised by the next accessor

C) Depends

Note that the property I'd be setting is just a String

Upvotes: 1

Views: 429

Answers (3)

Ray Lu
Ray Lu

Reputation: 26668

Go for A) App Instance Variable

For context storage, refer to httpContext.

Upvotes: 1

David Wengier
David Wengier

Reputation: 10179

In my experience with our web apps here, the answer is A. As far as I know, a static class will never be GCed, it lives on for the life of the process (in this case, the ASP.NET worker process)

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1501646

The scope of a static variable is its AppDomain. So no, it won't get garbage collected - but if the AppDomain is recycled (which can happen a fair amount in ASP.NET) then you'll end up with a "new" static variable, effectively.

Upvotes: 3

Related Questions