I have a class (Class B) 开发者_如何学Cthat 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?
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?
The C# compiler generates a warning. I advice you to always compile with 'warnings as errors'.
class B should be
public class ClassB : ClassA{
    public override void TestMethod(){
    }
}
but it can compile without the override - it should generate a warning that if it was intended you need to add the new keyword
public class ClassB : ClassA{
    public new void TestMethod(){
    }
}
checkout this for more information
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论