Is a static variable in a library (DLL) shared by

2019-02-05 20:18发布

问题:

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.)

回答1:

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.