Inheritance and Static Properties

I do not understand the following phenomenon, can someone please explain to me that I was mistaken?

public class BaseClass
{
    public BaseClass()
    {
        BaseClass.Instance = this;
    }

    public static BaseClass Instance
    {
        get;
        private set;
    }
}

public class SubClassA : BaseClass
{
    public SubClassA() 
        : base()
    { }
}

public class SubClassB : BaseClass
{
    public SubClassB()
        : base()
    { }
}

class Program
{
    static void Main(string[] args)
    {
        SubClassA a = new SubClassA();
        SubClassB b = new SubClassB();

        Console.WriteLine(SubClassA.Instance.GetType());
        Console.WriteLine(SubClassB.Instance.GetType());

        Console.Read();
    }
}

As I understand it, the compiler must generate a new type inheritance by type, that SubClassA and SubClassB are indeed native types with their own static variables. But it seems that the static part of the class is not inherited, but referenced - that I'm wrong?

+5
source share
3 answers

.NET inheritance only works on an instance basis. Static methods are defined at the type level not at the instance level. This is why redefinition does not work with static methods / properties / events ...

. ..

.NET, . .NET, . () , . ( ). ?

+10

Instance, BaseClass, , ( private).

, SubClassA SubClassB BaseClass . Instance BaseClass, .

SubClassB; Instance Console.WriteLine.

SubClassA SubClassB, Instance SubClassA.

+13

Because of these issues, Singleton classes must be declared private: MSDN Singletons

0
source

All Articles