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.

I hardly missed the override keyword when declaring an overriding method (presumably) in class B.

Class A

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

Class B

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

Compiled code without problems. Can someone explain why?

+6
override inheritance methods c #
source share
3 answers

This is not ambiguous. It should compile with a warning to say that you must either specify "new" or "override" and that by default it is "new".

It definitely gives a warning when I try to compile this code - when you say it compiles β€œfine” and β€œno problem”, do you ignore the warnings?

+7
source share

The C # compiler generates a warning. I advise you to always compile warnings as errors.

+1
source share

class B should be

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

but it can compile without overriding - it should generate a warning that if it is intended, you need to add a new keyword

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

checkout this for more information

0
source share

All Articles