Code inspection: Redundant 'abstract' modifier
Starting from C# 8.0, you can use the abstract
modifier on interface members. But this modifier is optional because interface members are abstract by default — that is, you need to override them anyway in the implementing classes — unless they have a default implementation (which is also a new feature in C# 8.0).
So in most cases the abstract
modifier is redundant — interface members without a body will compile to the similar bytecode. The only case when you need to use the abstract
modifier on an interface member is when this member cancels the default implementation in the base interface:
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"); }
}
Last modified: 11 February 2024