23

I know that a static variable used in a web application is shared for all users across the web application. If I have a library (DLL) that uses some static private variable, do all applications using that library share the value of that variable?

For example, say I have the following code in my DLL:

private static bool isConnected = false;

public static void Connect()
{
    // TODO: Connect.
    isConnected = true;
}

public static void Disconnect()
{
    // TODO: Disconnect.
    isConnected = false;
}

Then in Application A, I call myDLL.Connect() which sets the value of isConnected to True. Then I have some Application B that does the same thing. If Application A later calls myDLL.Disconnect(), does Application B see isConnected as False because the two applications share the same DLL file with a static variable? The DLL file would, in this case, be literally the same file in the same file path.

(I previously asked a somewhat similar question about web applications here. It is not related.)

Community
  • 1
  • 1
Devin Burke
  • 13,642
  • 12
  • 55
  • 82

1 Answers1

28

No they won't. They are loaded in separate AppDomains and cannot see each other's instances.

Even if they refer to same physical file, each application gets its own private instance of the assembly.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • does that mean if X application using the same DLL then there would be X instances of the assembly one for each APP? just confirming once. – Rahul Jul 25 '11 at 15:01
  • 4
    Short answer - Yes. Long Answer: The keyword here is `AppDomain`. If there are `X AppDomains` then there would be `X` instances. By default, each App will have its own AppDomain which means for `X` Apps, there will be `X` instances. There are ways where you can load multiple apps in the same domain but that's a different discussion. – Mrchief Jul 25 '11 at 15:11
  • 1
    Thanks, didn't knew this. +1 for that – Rahul Jul 25 '11 at 16:28