Q: Are static members in base classes shared across its derived classes The question regards .Net specifically.
Suppose a base class A has 2 derived classes B and C. Further, suppose base class A has a static variable called 's'. Is this static variable values shared between 'B' and 'C', or does each get its own copy of the static variable.
My current knowledge says that normally the static variable is shared between derived classes, unless the base class is Generic. If the base class is generic, then a unique copy of the class is created for each unique combination of its type parameters, and each such copy of the class together with that particular copy's derived classes share the same satatic variable from the base class.
I am actually facing a very unique situation. In my case, all of A, B and C are non-generic. And A has a static member. But the value for this member is different for B and C, which in my understanding should not have been the case (unless my above statement for static variables is incorrect).
EDIT:
Well my application is an ASP.NET app. I am continuing to debug this, but as per my current assessment, its happening like this:


*

*I put a breakpoint where the variable is changed and its value gets updated the first time the breakpoint is hit.

*The next time the breakpoint is hit, the variable has reverted back to its old value and is initialized again. It's a private variable and only changed in a single place.


My guess is maybe either the application app domain has recycled or somehow otherwise the class was unloaded from memory. I am investigation this still, but thanks everyone for the replies.

A: Could you show some code?

public class A
{
    private static string test;
    public string Test { get { return test; } set { test = value; } }

    public override string ToString()
    {
        return Test;
    }
}

public class B : A
{
    public override string ToString()
    {
        return base.ToString();
    }
}

public class C:A
{
    public override string ToString()
    {
        return base.ToString();
    }
}


Test console code

    private static void Test()
    {
        Console.Clear();

        A a = new A();
        B b = new B();
        C c = new C();
        a.Test = "Test A";
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
        b.Test = "Test B";
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
        c.Test = "Test C";
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);

        Console.ReadKey();
    }


Test console result

Test A 
Test A 
Test A 
Test B 
Test B
Test B 
Test C 
Test C
Test C


A: the static member in base class is shared between the derived classes. The value you would see for the variable should be consistent across different derived classes you use to access it.
