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

  • SEARCH
  • Home
  • 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 3347110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:22:58+00:00 2026-05-18T01:22:58+00:00

There must be something fundamental about interfaces/generics I have not yet learned. I hope

  • 0

There must be something fundamental about interfaces/generics I have not yet learned. I hope to learn it now.

Here is the scenario:

I have this interface and class:

public interface IInterface
{
    string TestValue { get; set; }
}

public class RealValue: IInterface
{
    public string TestValue { get; set; }
}

If I create a method like this it compiles just fine:

public class RandomTest: IMethodInterface
{
   public IInterface GetRealValue()
   {
       RealValue realValue = new RealValue();
       return realValue;
   }
 }

Note that I am returning an object that implements the interface.

Now, if I add to the RandomTest class a method that returns list then it does not work anymore:

 public List<IInterface> GetRealValues()
 {
    List<RealValue> realValues = new List<RealValue>();
    return realValues;  // ERROR Here <- says it can't convert to a List<IInterface>
 }

So, my guess is that generics can’t figure this out, but why?

Is there a way around this? What do you do when the return value of the method above is locked because you are implementing an interface like this:

public interface IMethodInterface
{
    IInterface GetRealValue();
    List<IInterface> GetRealValues(); // Can't just convert the return types to a concrete 
                                      // class because I am implementing this.  This 
                                      // interface is in a separate project that does not 
                                      // have the concrete classes.
}

Is there any hope? What would you do?

  • 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-18T01:22:59+00:00Added an answer on May 18, 2026 at 1:22 am

    The reason for this is that List<RealValue> is a specific type, which does not inherit List<IInterface>, so it cannot be converted.

    However, in .NET 4.0 you’re in luck. The interface IEnumerable<out T> specifies that T can be the class, or a base class, so you can change your method to:

    IEnumerable<IInterface> GetRealValues();
    

    on .NET 4.0. Note that this only works because IEnumerable has the out keyword specified on the template parameter.

    The out keyword means two things:

    1. The type before which you put the out keyword can only be used for types that go out of the class. So, public T MyMethod() is allowed, but public void MyMethod(T myParam) is not allowed, because this goes into the class;

    2. Because of this restriction, .NET knows that T can be cased to everything that inherits from T. Because of the restriction, this is guaranteed to be a safe operation.

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

Sidebar

Related Questions

I think there must be something subtle going on here that I don't know
Obviously I can do and DateTime.Now.After - DateTime.Now.Before but there must be something more
I know websphere does it, so there must be something that lets apache figure
I have a feeling that there must be client-server synchronization patterns out there. But
I'm trying to understand REST. Under REST a GET must not trigger something transactional
There must be a generic way to transform some hierachical XML such as: <element1
There must be an easy way to do this, but somehow I can wrap
There must be an easy way to do this. I build a Flex app
I assume there must be a system and language independent way to just stick
Yes, There's More Than One Way To Do It but there must be a

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.