0

I have a base class with a protected-level static variable, a protected-level static function, and a public function:

public class ClassA
{
    protected static int Size = 4;

    public static byte[] DoSomething(byte[] params)
    {
        // use Size somehow
        params = DoSomethingElse(params);
        return params;
    }

    protected static byte[] DoSomethingElse(byte[] params)
    {
        // do whatever
        return params;
    }
}

And a derived class that hides the protected-level variable and function:

public class ClassB : ClassA
{
    public new static int Size = 2;

    protected new static byte[] DoSomethingElse(byte[] params)
    {
        // do something different than base class
        return params;
    }
}

Now, when I call ClassB.DoSomething, I want the Size value and DoSomethingElse from the ClassBto be used, but the ClassA values are used instead. Is there a way to make this use the ClassB variable and method?

  • 1
    what are you asking for is runtime polyorphism for static function...I dont think this is possible.... – Viru Feb 04 '16 at 17:49
  • afaik Viru is correct and it isn't possible - consider for example if you subclass ClassB - what should Size then refer to in ClassC? I couldn't find the definitive answer in the C# spec but since static methods cannot be overridden (only overloaded) I am willing to bet a similar rule applies to static fields/properties – Stephen Byrne Feb 04 '16 at 18:11
  • http://stackoverflow.com/questions/2281775/c-sharp-static-member-inheritance-why-does-this-exist-at-all – sam Feb 04 '16 at 19:02

0 Answers0