Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this C# code compile fine when there is an ambiguous virtual method?

I have a class (Class B) that inherits another class (Class A) that contains virtual methods.

Mistakenly, I omitted the override keyword when declaring a (supposed to be) overriding method in Class B.

Class A

public class ClassA{
    public virtual void TestMethod(){
    }
}

Class B

public class ClassB : ClassA{
    public void TestMethod(){
    }
}

The code compiled without a problem. Can anyone explain why?

like image 296
Jimbo Avatar asked May 27 '10 10:05

Jimbo


1 Answers

It's not ambiguous. It should compile with a warning to say that you should either specify "new" or "override" and that the default is effectively "new".

It definitely gives a warning when I try to compile that code - when you say it compiles "fine" and "without a problem" are you ignoring warnings?

like image 155
Jon Skeet Avatar answered Sep 21 '22 11:09

Jon Skeet