... all I'm offering is the truth. Nothing more. RSS 2.0
# Friday, February 29, 2008

I was doing some coding and created a derived class today and realized that the compiler was giving a me a silent warning message. Consider the following case where my base class (FirstFloor) has a virtual method which, by definition when a class is derived from it, is permitted to override it. As below I had inadvertently forgot to include the override method in the derived class but things seemed to work as required (against my better understanding).

    public class FirstFloor
    {
        public virtual void Foo()
        {
            Console.WriteLine("FirstFloor.Foo");
        }
    }

    public class SecondFloor : FirstFloor
    {
        public void Foo() //missing the override keyword
        {
            Console.WriteLine("SecondFloor.Foo");
        }
    }

Below is the silent warning that occurred, and unfortunately the compiler is really forgiving and hides the inherited member for you:
image

This is really picking at the details, but in this case you should always use the keyword override even if the compiler is soft on this kind of crime.

    public class SecondFloor : FirstFloor
    {
        public override Foo()
        {
            Console.WriteLine("SecondFloor.Foo");
        }
    }   

One other thing I am doing is starting to setup my compiler to treat warnings as errors, now that is going to catch a whole bunch of other things going on in your code, but that is another blog entry.
image 

Technorati tags: , , ,

 

Friday, February 29, 2008 10:32:41 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] - Trackback
C#
Comments are closed.
Blogroll
Statistics
Total Posts: 330
This Year: 18
This Month: 2
This Week: 0
Comments: 30
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Mark Downie
Sign In
All Content © 2010, Mark Downie
DasBlog theme 'Business' created by Christoph De Baene (delarou)