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 8008883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:16:28+00:00 2026-06-04T18:16:28+00:00

If an interface specifies a property or method to return another interface, why is

  • 0

If an interface specifies a property or method to return another interface, why is it not allowed for implementations of the first interface to “change” the return type into a more specific type?

Let’s take an example to illustrate:

interface IFoo
{
    IBar GetBar();
}
interface IBar
{ }

class Foo : IFoo
{
    // This is illegal, we are not implementing IFoo properly
    public Bar GetBar()
    {
        return new Bar();
    }
}

class Bar : IBar
{ }

I know how to make it work, that’s not my concern.

I can just either:

  • Change return type of GetFoo() to IBar, or
  • Explicitly implement the interface and just call GetBar from the IFoo.GetBar() method

What I am really asking is the reasoning for not just allowing the code above to compile. Is there any case where the above doesn’t fulfill the contract specified by IFoo.

  • 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-06-04T18:16:30+00:00Added an answer on June 4, 2026 at 6:16 pm

    Usually, I’d say that it would be a case of balancing the benefit against the added complexity of supporting such a feature. (All features take effort to design, document, implement, test, and then developers need to be educated about them too.) Note that there could be some significant complexities if you wanted to support returning a value type which implemented an interface, for example (as that ends up in a different representation, rather than just a reference).

    In this case, I don’t believe the CLR even supports such a feature, which would make it very hard for C# to do so cleanly.

    I agree it would be a useful feature, but I suspect it hasn’t been deemed useful enough to warrant the extra work required.

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

Sidebar

Related Questions

Why DataSource interface does not define the Standard Data Source Properties specified by the
why is it recommended to define service contract as an interface. Any specific advantages
I have extended my entities to implement specific interfaces for its type. I am
(This question does not rely on a specific IoC framework, so the interfaces and
interface I { int J(); } class A : I { public int J(){return
interface If { ... } class Impl implements If { ... } function test(type:Class,
Is it right by saying it's common to have some interface members not implemented
I have the following class #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface Bankdaten : NSManagedObject @property
TL;DR What is wrong with hiding a property in an interface so that I
Here is a specific example which is not CLS-complaint according to VS.NET 2005. Public

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.