views:

60

answers:

4

Could anybody explain why static property is null?

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;
    }
}
+6  A: 

That because when you write Cc.P1, you're actually referring to Ca.P1 because that's where it is declared (since P1 is static, it is not involved in polymorphism). So in spite of appearances, your code isn't using the Cc class at all, and the Cc static constructor is not executed.

Thomas Levesque
On a side-note, this is why ReSharper will yell at you if you try to do something like this (refer to a static field through a deriving class).
Zor
I have read this question: http://stackoverflow.com/questions/774181/why-cant-i-inherit-static-classes, Do you know how could i do this? static properties aren´t support in interfaces
fravelgue
@fravelgue, how could you do what ? You didn't explain what you were trying to do exactly...
Thomas Levesque
@thomas i think i can´t do it bcos c# doesn´t support static inheritance.
fravelgue
A: 

Try the following:

string s = Cc.P1; // is null
Cc c = new Cc();
s = Cc.P1; // is it still null

If P1 is no longer null, it is because accessing the static P1 (in Ca) does not cause the static instance of Cc to fire (and therefore assign the value in the static constructor).

Paul Hadfield
I don´t want create an object.
fravelgue
@fravelgue: Yes, sorry I understand what you're trying to do - I was just trying to illustrate the call that is needed to get what you want to happen to work. As both myself and others have said, the static constructor on "Cc" will not fire if you just access the static property P1 which is on "Ca". It's not a bug, its how the .NET framework works. If you really have a need to do this, then you'll need to find another way to do it I'm afraid
Paul Hadfield
@paul, thx very much for you response, but it looks static inheritance is not possible in c#, and there isn´t solution for this.
fravelgue
A: 

If you really want the value:

new Cc();  // call the static constructor
string s = Cc.P1; // not null now
Danny Chen
A: 

You misuse some OOD principles in your code. For example, you mix in your classes static behavior (witch is something like Singleton design pattern) and polymorphism (you use abstract base class but without any base class interface). And because we have no such thing like "Static Polymorphism" we should separate this two roles.

If you describe in more details what problem are you trying to solve, maybe you receive more accurate answers.

But anyway you may implement something like this:

public class Cc : Ca
{
    private Cc()
        : base("Test")
    {
       //We may call protected setter here
    }

    private static Ca instance = new Cc();
    public static Ca Instance
    {
        get { return instance; }
    }
}

public abstract class Ca
{
    protected Ca(string p1)
    {
        P1 = p1;
    }

    //You may use protected setter and call this setter in descendant constructor
    public string P1
    {
        get;
        private set;
    }
}


static void Main(string[] args)
{
    string s = Cc.Instance.P1; // is not null
}
Sergey Teplyakov