The following interface has no errors in a .NET Core Console application with C# 8.0
interface I
{
public abstract void f();
public virtual void g() => Console.WriteLine("g");
public sealed void h() => Console.WriteLine("h");
}
abstract
prevents adding a definition in interface. virtual
and sealed
necessitate a definition in interface. sealed
prevents an implementation of h
in derived classes.
Do abstract
, virtual
and sealed
, when used in interfaces, have any other meaning or applications in current implemented version of C# 8? How and when should they be used in interfaces?
public
,]abstract
, andvirtual
are redundant here. All interface members are virtual (abstract == pure virtual) and prior to C# 8.0, they were always abstract. The abstract/virtual distinction (in a class or, now, in an interface) is simply whether or not there's an implementation.sealed
prevents the method from being explicitly implemented but not from being implicitly implemented, but that shouldn't be the interface designer's concern. – Legrandsealed
seems useless because you can't override yourg
method (in an interface that implementsI
) any more than you could override your sealedh
method, andg
isn't markedsealed
. – Legranduseless
in interfaces. Byuseless
do mean, that they have only the effects I mentioned in the question and no other effects or applications beyond that? – Fightprivate
is optional on a member; if it is omitted, you get it anyway.public static
is required on a user defined operator; if it is omitted, you get an error. And until recently,virtual
was illegal on an interface member because it was redundant. – Hispanicsealed
is useless, whereas the others are merely redundant.sealed
does have an effect in that it prevents the implementing class from providing an implementation, as you pointed out. However, that's entirely contrary to the purpose of interfaces and, indeed, default interface member implementations (the default part, specifically). A sealed interface member implemented by the interface could be achieved by an extension method (even pre-8.0) and that would be more meaningful as to its intent. You'd expect to be able to provide an implementation. – Legrand