Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:37:32+00:00 2026-05-23T14:37:32+00:00

Interface methods in C# can be implemented explicitly, so that their implementation is invoked

  • 0

Interface methods in C# can be implemented explicitly, so that their implementation is invoked when an instance is explicitly cast to the interface type. Why is this not also supported on virtual methods of classes?

Although working around the ‘multiple inheritance’ issue is unique to interfaces, it seems that for every other reason that explicitly implemented members would be useful for interfaces, they would also be useful for virtual methods. A cleaner return-type covariance model springs to mind.

Edit: By request, an example:

public class Foo {
    ...
}

public class Bar : Foo {
   ...
}

class Base {
   abstract Foo A ();
}

class Dervied {
  private Bar _b;

  Bar A () {
    return _b;
  }

  Foo Base.A () {
    return _b;
  }
}

I am aware of using helper methods to simulate this, but the net effect seems to have any of the bad characteristics that explicit implementation would have, but with a dirtier API. The crux of my question is not how to do return type covariance, but why a similar mechanism for interfaces is not supported for virtual methods.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:37:32+00:00Added an answer on May 23, 2026 at 2:37 pm

    What benefit would that have, besides from allowing something like this?

    class Base
    {
        virtual void M() { }
    }
    
    class Derived : Base
    {
        override void M() { }
    
        override void Base.M() { }
    }
    

    This effectively bakes a violation of the Liskov Substitution Principle into the C# language – if I have a variable of type Base, calling M() on it can do entirely different things depending on whether the run-time type is Base or Derived.

    Explicit interface implementation is different. Say you have this:

    interface IFoo
    {
        void DoStuff();   
    }
    
    interface IBar
    {
        void DoStuff();
    }
    
    class C : IFoo, IBar
    {
        void IFoo.DoStuff() { }
    
        void IBar.DoStuff() { }
    }
    

    This preserves the LSP – if I have an IFoo variable that happens to be of run-time type C, calling DoStuff() on it will get the IFoo implementation of it. Likewise with IBar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we call COM exposed interface methods at runtime one by one? This can
I have a MethodInfo object that represents an explicitly-implemented interface method, as follows. MethodInfo
We have a given REST interface: POST /calculation <data>abc</data> This calculation can be implemented
can i implement some methods of an interface explicitly and some methods implicityly in
Can Eclipse make parameters for generated methods (overwriting, implementing interface, etc.) final, and if
I have a interface that defines some methods with attributes. These attributes need to
I have an interface that defines some methods I would like certain classes to
Is there a way to override extension methods (provide a better implementation), without explicitly
Let's say I have a class that implements the IDisposable interface. Something like this:
How to mock methods with void return type? I implemented an observer pattern but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.