Lifetime of static variable across postbacks

D

Deckarep

Hey all, this question is somewhat related to ASP.NET but I think it's
very relevant to how the dynamics of the C# language work so I'm
asking it in this forum:

In my webcontrol class that inherits from System.Web.UI.UserControl.
I have a private static variable declared. And on my OnLoad I
increment this int type value by one.

Now, I have five of these instance webcontrols embeded on the same
page each with their own different html.

Now when I hit the page with my browser I'm expecting to see that
static "counter" variable set to 5. Because each of those controls
increments the static variable by one.

Now when I hit the page again on postback that value is now 10....then
another postback to 20....etc...etc.

WTF? Why is this happening? When the webcontrols get unloaded
by .Net should the objects be destroyed and garbage collected at one
point including all members belonging to that object? But no, this
value is persisting across postbacks.

This seams like incorrect behevior on the part of the CLR or
something. If there is a reason why these values are persisting
across postbacks or some MSDN article thouroughly explaining this
beheavior I'd like to be pointed to the URL because this is the first
time I've realized this and frankly it bothers me.

By the way I've already searched google about this and found different
forums of people explaining different reasons which have been
conflicting...which is why I'm asking here to set the record straight.

Here is some sample code...nothing complex here:

public class MarketingUserControl : System.Web.UI.UserControl
{
private static int m_iTourCount = 0; //<---- Why isn't this
resetting this variable across postbacks?
protected override void OnLoad( EventArgs e )
{
base.OnLoad( e );

m_iTourCount++; // <----- Why does this guy persist
across postbacks?
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Deckarep,

Why should it reset across callbacks? A static variable's lifetime is
the app domain that it is hosted in. As long as the app domain is up and
running, the static value will persist.

A postback does not create a new app domain, so the behavior you are
witnessing is expected (and correct). A new app domain is created only when
the other is shut down by its host (IIS will do this if specified to do so
because of memory pressures, or because it is told to recycle the app on a
schedule).

Also, the code you had incrementing the variable was incorrect. Because
you could have multiple page requests on different threads accessing that
value, you should have synchronized access to that variable.

If you want a value that only exists across the generation of a page,
then you need to create a field-level variable for the page, and then have
the controls increment that variable (you won't need synchronization here,
as new instances of the page are created across each request).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top