我有一个特定的接口查询.默认情况下,接口方法是抽象的和虚拟的,所以如果我们实现该接口并在类中给出定义,我们实际上会覆盖该方法,但是当我们在实现类中将该方法再次标记为虚拟时,为什么编译器不考虑我们实际上在尝试隐藏原始接口虚拟方法.如果我们在基类中有一个虚方法,并且派生类再次将该方法标记为虚拟,那么编译器会发出警告您正在隐藏基类方法,因此如果您故意使用new隐藏基类方法.
public interface ITestInterface
{
void virtualmethod(); // this method is by default virtual.
}
public class TestInterface :ITestInterface
{
public virtual void virtualmethod()
{
// Now compiler should consider that i am actually hiding the interface virtual method.
}
}
Run Code Online (Sandbox Code Playgroud)
如果您为接口构建上述代码并在ILDASM中打开,您将看到如下代码:
.method public hidebysig newslot abstract virtual
instance void virtualmethod() cil managed
{
}//end of method ITestInterface::virtualmethod