none
Overriding static methods in derived classes RRS feed

  • Question

  • There is a base class which has a static method

    public static GroupingReportMetadata Create (string criteria)

    and then this needs to be oveerriden in a derived class. I know that technically static methods aren't supposed to be overriden but CLR allows you to override in this situation with the new keyword like this:

    public new static GroupingReportMetadata Create (string criteria)

    I want to know if this would be a good practice to do so. I guess a C++ compiler won't even allow you to do so.
    In my opinion, in a .Net environment we can use delegates to reference static methods and invoke them as necessary.

    What is the expert advice on this?


    Rendezvous
    Thursday, April 2, 2009 4:41 PM

Answers

  • I would like to point out that what you are refering to is not CLR feature, but C# feature and it is not called overriding, but hiding. See more details about this in C# ECMA spec, chapter 10.7.1.2 "Hiding through inheritance" (There's an interesting example with hiding parent static method Base.F with a private-scope static method Derived.F. The original static method Base.F will be visible again in class inheritted from your class - MoreDerived).
    If you don't use the new keyword in C#, you will get a compiler warning (not an error) CS0108 "'B.Create' hides inherited member 'A.Create'. Use the new keyword if hiding was intended." Using the new keyword will suppress this warning.
    Also note that you can everytime call hidden methods on parents by specifying their full name without even using delegates:

    class HelloWorld
    {
        static void Main() { B.CallPrint(); }
    }
    
    class
    A { public static void Print() { System.Console.WriteLine("A.Print"); } } class B : A { public static void Print() { System.Console.WriteLine("B.Print"); } public static void CallPrint() { Print(); // Will call B.Print A.Print(); // Will call hidden static method on parent } }

    IMO hiding static methods is perfectly fine, because you have to call static methods with a type name and cannot call them on instance. The important decision is if a method should be static or instance or virtual method.

    Neither C++ nor IL have similar warning and both allow you to hide parent's static methods by default.
    VB has similar warning and keyword 'overloads' for shared (=static) methods. Without it you will get VB compiler warning BC40003 "sub 'Create' shadows an overloadable member declared in the base class 'A'.  If you want to overload the base method, this method must be declared 'Overloads'."

    -Karel

    • Marked as answer by Zhi-Xin Ye Thursday, April 9, 2009 10:09 AM
    Thursday, April 2, 2009 7:20 PM
    Moderator

All replies

  • Do you have any real reason to do that?
    After all, doing this is the same as implementing a new non-virtual method in a derived class. If you KNOW you are using such class, it works, if not, you will not call the inherited method.

    And, if you can use delegates, why not having:
    CreateGroupingReportMetadata
    CreateSubGroupingReportMetadata?

    So it is easier to know if you are calling the main or the sub-method, and both are possible to be used for delegates.

    I will only suggest using such approach (only a Create method) if you NEED to use a factory, and when that factory needs to be Type instead of a delegate, so you must always look for the same method name.
    ------------- Paulo Zemek - System Architect
    Thursday, April 2, 2009 5:08 PM
  • I would like to point out that what you are refering to is not CLR feature, but C# feature and it is not called overriding, but hiding. See more details about this in C# ECMA spec, chapter 10.7.1.2 "Hiding through inheritance" (There's an interesting example with hiding parent static method Base.F with a private-scope static method Derived.F. The original static method Base.F will be visible again in class inheritted from your class - MoreDerived).
    If you don't use the new keyword in C#, you will get a compiler warning (not an error) CS0108 "'B.Create' hides inherited member 'A.Create'. Use the new keyword if hiding was intended." Using the new keyword will suppress this warning.
    Also note that you can everytime call hidden methods on parents by specifying their full name without even using delegates:

    class HelloWorld
    {
        static void Main() { B.CallPrint(); }
    }
    
    class
    A { public static void Print() { System.Console.WriteLine("A.Print"); } } class B : A { public static void Print() { System.Console.WriteLine("B.Print"); } public static void CallPrint() { Print(); // Will call B.Print A.Print(); // Will call hidden static method on parent } }

    IMO hiding static methods is perfectly fine, because you have to call static methods with a type name and cannot call them on instance. The important decision is if a method should be static or instance or virtual method.

    Neither C++ nor IL have similar warning and both allow you to hide parent's static methods by default.
    VB has similar warning and keyword 'overloads' for shared (=static) methods. Without it you will get VB compiler warning BC40003 "sub 'Create' shadows an overloadable member declared in the base class 'A'.  If you want to overload the base method, this method must be declared 'Overloads'."

    -Karel

    • Marked as answer by Zhi-Xin Ye Thursday, April 9, 2009 10:09 AM
    Thursday, April 2, 2009 7:20 PM
    Moderator