C# Static Variables - Scope and Persistence

C# Static variables - scope and persistence

They will persist for the duration of AppDomain. Changes done to static variable are visible across methods.

MSDN:

If a local variable is declared with the Static keyword, its lifetime is longer than the execution time of the procedure in which it is declared. If the procedure is inside a module, the static variable survives as long as your application continues running.

See following for more details:

  • C#6 Language Specification - Static Variables
  • C#6 Language Specification - Application Startup
  • MSDN: Static Variable
  • MSDN: Variable Lifetime

Scope of private static variable in non static class

Since B is static it'll be shared between all sessions; the thread-safe (what if two sessions are trying to access / increment it simultaneously?) implementation is

   public int GetSession()
{
return Interlocked.Increment(ref B);
}

Edit: If we want to emulate B++, not ++B (and return B before incrementing - see Jeppe Stig Nielsen's comment) we can just subract 1:

   public int GetSession()
{
// - 1 Since we want to emulate B++ (value before incrementing), not ++B
return Interlocked.Increment(ref B) - 1;
}

How to persist data in a static variable when shared across library?

Static values are persisted across an application domain. As long as your application is running and didn't create a new appdomain it will remain the same. Or another piece of code is changing it.

Windows Phone 8 is a bit more complex because event tiles and the application tend to run in different app domains:

Static variable value different in background agent

If you have a static value across event tiles and background processes your last remaining option is to store it to a persistent data store / isolated storage.

Lifetime of ASP.NET Static Variable

Static variables persist for the life of the app domain. So the two things that will cause your static variables to 'reset' is an app domain restart or the use of a new class. In your case with static variables stored in an aspx Page class, you may be losing the static variables when ASP.NET decides to recompile the aspx Page into a new class, replacing the old page class with the new one.

For those reasons if the system decide to restart or replace the class (.NET doesn't kill or unload classes/assemblies in a running app domain) then your static variables will reset because you are getting a new class with the restart or replacement. This applies to both aspx Pages and classes in the App_Code folder

ASP.NET will replace a class if for any reason thinks that need to recompile it (see ASP.NET dynamic compilation).

You can't prevent the loss of static variables from an app domain restart, but you can try to avoid it from class replacement. You could put your static variables in a class that is not an aspx page and is not in the App_Code directory. You might want to place them on a static class somewhere in your program.

public static class GlobalVariables
{
public static int SomeGlobalUnsecureID;
public static string SomeGlobalUnsecureString;
}

The static variables are per pool, that is means that if you have 2 pools that runs your asp.net site, you have 2 different static variables. (Web garden mode)

The static variables are lost if the system restarts your asp.net application with one of this way.

  1. the pool decide that need to make a recompile.
  2. You open the app_offline.htm file
  3. You make manual restart of the pool
  4. The pool is reach some limits that you have define and make restart.
  5. For any reason you restart the iis, or the pool.

This static variables are not thread safe, and you need to use the lock keyword especial if you access them from different threads.

Since an app restart will reset your statics no matter what, if you really want to persist your data, you should store the data in a database using custom classes. You can store information per-user in Session State with a database session state mode. ASP.NET Application State/Variables will not help you because they are stored in memory, not the database, so they are lost on app domain restart too.

Static fields vs Session variables

No, using static variables for this is not the way to go:

  • If your AppDomain is recycled, all your static variables will be "reset"
  • Static variables don't scale horizontally - if you load-balance your application, a user who hits one server then a different one won't see the data store in the static variables in the first server
  • Most importantly, static variables will be shared by all access to that server... it won't be on a per-user basis at all... whereas from your description, you wouldn't want user X to see user Y's information.

Fundamentally, you have two choices for propagating information around your application:

  • Keep it client-side, so each request gives the information from the previous steps. (This can become unwieldy with large amounts of information, but can be useful for simple cases.)
  • Keep it server-side, ideally in some persistent way (such as a database) with the client providing a session identifier.

If you can use load-balancing to keep all users going to the same server, and if you don't mind sessions being lost when the AppDomain is recycled1 or a server going down, you can keep it in memory, keyed by session ID... but be careful.


1 There may be mechanisms in ASP.NET to survive this, propagating session information from one AppDomain to another - I'm not sure

What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?

A static variable shares the value of it among all instances of the class.

Example without declaring it static:

public class Variable
{
public int i = 5;
public void test()
{
i = i + 5;
Console.WriteLine(i);
}
}

public class Exercise
{
static void Main()
{
Variable var1 = new Variable();
var1.test();
Variable var2 = new Variable();
var2.test();
Console.ReadKey();
}
}

Explanation: If you look at the above example, I just declare the int variable. When I run this code the output will be 10 and 10. Its simple.

Now let's look at the static variable here; I am declaring the variable as a static.

Example with static variable:

public class Variable
{
public static int i = 5;
public void test()
{
i = i + 5;
Console.WriteLine(i);
}
}

public class Exercise
{
static void Main()
{
Variable var1 = new Variable();
var1.test();
Variable var2 = new Variable();
var2.test();
Console.ReadKey();
}
}

Now when I run above code, the output will be 10 and 15. So the static variable value is shared among all instances of that class.

Does C# support the use of static local variables?

No, C# does not support this. You can come close with:

private static System.Text.RegularExpressions.Regex re =
new System.Text.RegularExpressions.Regex("\\(copy (\\d+)\\)$");

private static string AppendCopyToFileName(string f)
{

}

The only difference here is the visibility of 're'. It is exposed to the classm not just to the method.

The re variable will be initialized the first time the containing class is used in some way. So keep this in a specialized small class.



Related Topics



Leave a reply



Submit