interface - Why doesn't C# support explicitly implemented virtual methods? -
interface methods in c# can implemented explicitly, implementation invoked when instance explicitly cast interface type. why not supported on virtual methods of classes?
although working around 'multiple inheritance' issue unique interfaces, seems every other reason explicitly implemented members useful interfaces, useful virtual methods. cleaner return-type covariance model springs mind.
edit: request, example:
public class foo { ... } public class bar : foo { ... } class base { abstract foo (); } class dervied { private bar _b; bar () { return _b; } foo base.a () { return _b; } } i aware of using helper methods simulate this, net effect seems have of bad characteristics explicit implementation have, dirtier api. crux of question not how return type covariance, why similar mechanism interfaces not supported virtual methods.
what benefit have, besides allowing this?
class base { virtual void m() { } } class derived : base { override void m() { } override void base.m() { } } this bakes violation of liskov substitution principle c# language - if have variable of type base, calling m() on can entirely different things depending on whether run-time type base or derived.
explicit interface implementation different. have this:
interface ifoo { void dostuff(); } interface ibar { void dostuff(); } class c : ifoo, ibar { void ifoo.dostuff() { } void ibar.dostuff() { } } this preserves lsp - if have ifoo variable happens of run-time type c, calling dostuff() on ifoo implementation of it. likewise ibar.
Comments
Post a Comment