Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there only one instance of a static variable per process?

If I have the following class:

public class MyClass { public static int MyStaticInt = 0; }

If in the one solution I refer to MyNameSpace.MyClass.MyStaticInt in two different assemblies, am I referring to the same variable?

like image 425
CJ7 Avatar asked Oct 09 '12 08:10

CJ7


2 Answers

Static state is scoped per AppDomain by default and can be configured to be by thread if you use the ThreadStatic attribute.

This means that your assumption is valid if the assemblies are running in the same process and the process has only one application domain.

like image 72
João Angelo Avatar answered Nov 15 '22 11:11

João Angelo


static can mean several things depending on context.

  • By default, you get one instance of the value per AppDomain.
  • If decorated with the ThreadStatic attribute, you get one instance of the value per thread.
  • If contained within a generic class, you get one instance of the value per concrete type.

For your example code, the first condition appears to be the case. In all cases, the specific assembly that the data is defined in does not make any difference.

like image 38
Christian Hayter Avatar answered Nov 15 '22 10:11

Christian Hayter