In .Net is the 'Staticness' of a public static variable limited to an AppDomain or the whole process?

烂漫一生 提交于 2019-11-29 10:53:49

It is per application domain as proven by this example:

public class Foo
{
    public static string Bar { get; set; }
}

public class Test
{
    public Test()
    {
        Console.WriteLine("Second AppDomain: {0}", Foo.Bar);
    }
}

class Program
{
    static void Main()
    {
        // Set some value in the main appdomain
        Foo.Bar = "bar";
        Console.WriteLine("Main AppDomain: {0}", Foo.Bar);

        // create a second domain
        var domain = AppDomain.CreateDomain("SecondAppDomain");

        // instantiate the Test class in the second domain
        // the constructor of the Test class will print the value
        // of Foo.Bar inside this second domain and it will be null
        domain.CreateInstance(Assembly.GetExecutingAssembly().FullName, "Test");
    }
}

It is limited to the AppDomain, in other words, the variable exists as a separate value in each AppDomain.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!