GOTCHA 44 Compilers are lenient toward forgotten overrideoverrides


GOTCHA #44 Compilers are lenient toward forgotten override/overrides

Say a method in the base class is declared virtual/overridable, and you implement a method with the same name and signature in the derived class. What happens if you do not mark the method as override/overrides? When you compile the code and look at the output window in Visual Studio, you will not see any compilation error (see Gotcha #12, "Compiler warnings may not be benign"). However, a warning is generated. This warning tells you, very quietly, that the derived method has been assumed to hide the base class method (see Gotcha #43, "Using new/shadows causes "hideous hiding""). This is a serious warning that should not have been hidden in the output window. In fact, this should not be considered a warning at all, in my opinion. The compiler should jump out of the computer, grab the programmer by the collar, give him a smack, and demand that he fix the code. (Maybe I'm going a bit too far, but you get the point). Consider Example 6-6.

Example 6-6. Accidental hiding

C# (RememberMarkOverride)

 //Base.cs using System; namespace MarkOverride {     public class Base     {         public virtual void Method1()         {             Console.WriteLine("Base.Method1 called");         }     } } //Derived.cs using System; namespace MarkOverride {     public class Derived : Base     {         public void Method1()         {             Console.WriteLine("Derived.Method1 called");         }     } } //Test.cs using System; namespace MarkOverride {     class Test     {         [STAThread]         static void Main(string[] args)         {             Derived d = new Derived();     Base b = d;     d.Method1();     b.Method1();         }     } } 

VB.NET (RememberMarkOverride)

 'Base.vb Public Class Base     Public Overridable Sub Method1()         Console.WriteLine("Base.Method1 called")     End Sub End Class 'Derived.vb Public Class Derived     Inherits Base     Public Sub Method1()         Console.WriteLine("Derived.Method1 called")     End Sub End Class 'Test.vb Module Test     Sub Main()         Dim d As New Derived         Dim b As Base = d         d.Method1()         b.Method1()     End Sub End Module 

In this example, Method1() is declared virtual/overridable in the Base class. However, in the Derived class Method1() has not been marked as override/overrides. This compiles OK, as you can see in Figure 6-4.

Figure 6-4. Visual Studio output window for Example 6-6


But this is misleading; a warning actually is printed, but it scrolls out of the output window. The message is:

     warning CS0114: 'MarkOverride.Derived.Method1()' hides inherited member  'MarkOverride.Base.Method1()'. To make the current member override that implementation,  add the override keyword. Otherwise add the new keyword.

The output produced by the code is shown in Figure 6-5.

Figure 6-5. Output from Example 6-6


It would have been nice if the .NET compilers had erred on the side of caution. If a method is not marked override/overrides, why not assume that it overrides its base-class method, rather than assume that it hides it? Better still, shouldn't this have appeared as a fatal compilation error? Unfortunately it doesn't, so you need to pay attention to these warnings and treat them seriously. You can (and should) configure the compiler to treat warnings as errors. (See Gotcha #12, "Compiler warnings may not be benign.")

IN A NUTSHELL

If you are overriding a method, always remember to mark the method as override/overrides. Otherwise, the compiler just gives you a polite warning and assumes the method is intended to be new/shadows.

SEE ALSO

Gotcha #12, "Compiler warnings may not be benign," Gotcha #42, "Runtime Type Identification can hurt extensibility," Gotcha #43, "Using new/shadows causes "hideous hiding"," Gotcha #45, "Compilers lean toward hiding virtual methods," Gotcha #46, "Exception handling can break polymorphism," and Gotcha #47, "Signature mismatches can lead to method hiding."



    .NET Gotachas
    .NET Gotachas
    ISBN: N/A
    EAN: N/A
    Year: 2005
    Pages: 126

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net