JetBrains Rider 2025.2 Help

代码检查:多余的 'abstract' 修饰符

从 C# 8.0 开始,您可以在接口成员上使用 抽象 修饰符。 但此修饰符是可选的,因为接口成员默认是抽象的——也就是说,您无论如何都需要在实现类中重写它们——除非它们有 默认实现 (这也是 C# 8.0 的一项新功能)。

因此,在大多数情况下, 抽象 修饰符是多余的——没有主体的接口成员将编译为类似的字节码。 唯一需要在接口成员上使用 抽象 修饰符的情况是,当该成员取消了基接口中的默认实现时:

interface IBase { public void Foo() { Console.WriteLine("default implementation"); } } class DerivedFromBase : IBase { // no need to override 'Foo' since it has a default implementation } interface IAbstract : IBase { // Implementation without a body cancels the default implementation // in the base interface and has to be 'abstract' abstract void IBase.Foo(); // 'abstract' is redundant because this member is abstract anyway abstract void Bar(); } class DerivedFromAbstract : IAbstract { // Default implementation of 'Foo()' is cancelled // with 'abstract void IBase.M' in 'IAbstract' // therefore we have to provide an implementation here and // cannot use the default implementation from 'IBase' public void Foo() { Console.WriteLine("some implementation"); } // 'Bar' doesn't have a body in the base interface, // therefore we have to provide an implementation anyway public void Bar() { Console.WriteLine("some implementation"); } }
最后修改日期: 2025年 9月 26日