抽象类中的静态属性

fra*_*gue 2 c# static abstract-class

任何人都可以解释为什么静态属性为空?

class Program
{
    static void Main(string[] args)
    {
        string s = Cc.P1; // is null
    }
}

public class Cc
    : Ca
{
    static Cc()
    {
        P1 = "Test";
    }
}

public abstract class Ca
{
    public static string P1
    {
        get;
        protected set;
    }
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*que 6

那是因为当你写的时候Cc.P1,你实际上指的是Ca.P1因为它是声明的地方(因为P1它是静态的,它不涉及多态).因此,尽管出现了,但您的代码根本没有使用Cc该类,并且Cc不执行静态构造函数.