Default implementation of a method for C# interfaces?

后端 未结 6 2019
梦如初夏
梦如初夏 2020-12-24 05:35

Is it possible to define an interface in C# which has a default implementation? (so that we can define a class implementing that interface without implementing that particul

6条回答
  •  暖寄归人
    2020-12-24 05:46

    C# v8 will start allowing concrete method implementation in interfaces as well. This will allow your concrete implementation classes to not break when you change the interfaces being implemented in future.

    So something like this will be possible in the next language version:

    interface IA
    {
        void NotImplementedMethod();
        void M() { WriteLine("IA.M"); } //method definition present in the interface
    }
    

    Please refer to this GitHub issue # 288. Also Mads Torgersen talks about this upcoming feature at length in this channel 9 video.

    Note: Current version of C# language in RTM state is v7 at the time of writing this answer.

提交回复
热议问题