Should this Warning be an Error

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: , , ,


Comment Section

Comments are closed.