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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:08:13+00:00 2026-06-07T23:08:13+00:00

I have two Interfaces, one of them is a generic one, allowing only Types

  • 0

I have two Interfaces, one of them is a generic one, allowing only Types that derive from the second Interface. They look like this:

public interface IProvider<T> where T : IContent
{
    T getContent(int i);
    void addContent(T content);
}
public interface IContent
{
    string whatIAm();
}

Of course my real Interfaces are more complex but it is enought to show what my problem is.
Now i have for each interface a concrete class:

public class Provider : IProvider<FileContent> 
{
    public FileContent getContent(int i)
    {
        return null;
    }
    public void addContent(FileContent content)
    {
    }
}

public class FileContent : IContent{
    public string whatIAm(){
        return "FileContent";
    }
}

And in my code i want to work with the reference type “IProvider” but the cast goes wrong… Please look at this example:

 static void Main(string[] args)
    {
        Provider p = new Provider(); //works
        IProvider<FileContent> pp = p as IProvider<FileContent>; //also works
        IProvider<IContent> ppp = pp as IProvider<IContent>; //fails :(
    }

ppp is always null. What do i have to change that this cast is working?
Thanks in advance.

  • 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-07T23:08:14+00:00Added an answer on June 7, 2026 at 11:08 pm

    The type argument must match exactly. IProvider<IContent> is a different type than IProvider<FileContent>, there is no inheritance between them.

    Imagine you have an IProvider<IContent> ppp  from your IProvider<FileContent> and a developer tries ppp.addContent(someOtherContentThatIsNoFileContent). That statement is valid for IProvider<IContent>, but it would break type safety, so not allowing such a conversion is the right thing to do. 

    Covariance and Contravariance for generic type parameters allow something like this under certain circumstances, but since your interface uses the type parameter both as in- and output parameter, this won’t apply to it the way it is declared right now.

    EDIT: Look at IEnumerable‘s definition:

    public interface IEnumerable<out T> 
    

    So you know IEnumerable uses T only as output parameter (you can’t add items, only enumerate them), and the out keyword specifies that T is covariant. So you can do

    IEnumerable<String> strings = new List<String>();
    IEnumerable<Object> objects = strings;
    

    If you want to do this, you would have to remove the add method from your interface. Same applies for input parameters and the in keyword on generic type parameters. 

    Your interface would then look like this:

    public interface IProvider<out T> where T : IContent
    {
        T getContent(int i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that instantiates two classes which implement interfaces. I want one
I have an interface that has two toolbars, one attached to the frame and
Let's say that I have two interfaces and a behavior class: public interface Creable
I have two Java interfaces and one implementing class. (I have used Eclipse to
In my system I have two different projects, one defining its interfaces and another
I have two interfaces IHeaderRow , and IDetailRow I then have an object that
I have two entities Visita and Cliente that implements respectively two interfaces IVisita and
Lets say I have two interfaces interface A and interface B: public interface A
imagine there are two interfaces arranged via composite pattern, one of them has a
I have two different classes that share a common interface. Although the functionality is

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.