Making member virtual prevents calling default interface implementation and causes StackOverflowException in C# 8

后端 未结 3 1304
不知归路
不知归路 2021-01-13 03:02

Consider the code:

class ChildClass : BaseClass {

    public void Method1() {} //some other method

}

abstract class         


        
3条回答
  •  独厮守ぢ
    2021-01-13 03:35

    As all methods inside interfaces are virtual by default the DoWork is virtual inside every each of these definitions/implementations you provided except the ChildClass. When you explicitly use DoWork of IChildInterface it uses BaseClass.DoWork implicitly which then uses ((IChildInterface)this).DoWork(); explicitly again. And so on. You have this loop that is never ending, hence you're getting the StackOverflow.

提交回复
热议问题