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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:48:44+00:00 2026-05-27T05:48:44+00:00

I have a set of classes and interfaces which have a relatively simple hierarchy,

  • 0

I have a set of classes and interfaces which have a relatively simple hierarchy, however having things inherit and then having a variable which accepts the base class is not working well. I think the problem is that my dynamic types MUST be where : class and not where : IEntity because these types are used in functions which I have no ability to change.

public interface IB<X, Y> 
    where X : class, IEntity 
    where Y : class, IEntity
{
    ...
}

public abstract class A
{
    ...
}

public abstract class B<X, Y> : A, IB<X, Y> 
    where X : class, IEntity 
    where Y : class, IEntity
{
    ...
}

public interface IEntity
{
    ...
}

public class Entity1 : IEntity
{
    ...
}

public class Entity2 : IEntity
{
    ...
}

public class C : B<Entity1, Entity2>, IB<Entity1, Entity2>
{
   ...
}

And then, attempting to use all this in a function…

C c = new C();
IB<IEntity, IEntity> ib = c;

Cannot implicityly or explicity cast this.

How can I get this to work?

  • 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-27T05:48:45+00:00Added an answer on May 27, 2026 at 5:48 am

    Your interface is not covariantly valid. That is to say,

    IB<IEntity, IEntity> and IB<Entity1, Entity2> are not the same thing. To fix this, you can use the out keyword on the type parameters in the interface definition

    public interface IB<out X, out Y> where X : class where Y : class
    

    As a secondary point, you need to cascade your constraints to class B

    public abstract class B<X, Y> : A, IB<X, Y> where X : class where Y : class
    

    With these changes in place, your assignment of C into ib is legal.

    C c = new C(); 
    IB<IEntity, IEntity> ib = c;
    

    (And as Davy8 correctly points out in the comments, this interface covariance feature requires C# 4+.)

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

Sidebar

Related Questions

I have set of classes which inherit from a single super class: Super |
I have written a set of classes and interfaces that are implemented in Moose
I have a set of some classes which are all capable of being constructored
I have a set of XSDs from which I generate data access classes, stored
I have a set of classes which extend an abstract class. The abstract class
I have a set of interfaces which are used in close conjunction with particular
I have a set of classes, each of which need to decide at some
We've got a set of classes which derive from a common set of interfaces
If have a set of classes that all implement an interface. interface IMyinterface<T> {
I have a requirement where in i have a set of classes and they

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.