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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:19:42+00:00 2026-06-02T12:19:42+00:00

to avoid confusion I summarised some code: namespace ConsoleApplication1 { class Program { static

  • 0

to avoid confusion I summarised some code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            IManager<ISpecificEntity> specificManager = new SpecificEntityManager();
            IManager<IIdentifier> manager = (IManager<IIdentifier>) specificManager;
            manager.DoStuffWith(new SpecificEntity());
        }
    }

    internal interface IIdentifier
    {
    }

    internal interface ISpecificEntity : IIdentifier
    {
    }

    internal class SpecificEntity : ISpecificEntity
    {
    }

    internal interface IManager<TIdentifier> where TIdentifier : IIdentifier
    {
        void DoStuffWith(TIdentifier entity);
    }

    internal class SpecificEntityManager : IManager<ISpecificEntity>
    {
        public void DoStuffWith(ISpecificEntity specificEntity)
        {
        }
    }
}

When I debug the code I get an InvalidCastException in Main().

I know that ISpecificEntity implements IIdentifier.
But obviously a direct cast from an IManager<ISpecificEntity> into an IManager<IIdentifier> does not work.

I thought working with covariance could do the trick but changing IManager<TIdentifier> into IManager<in TIdentifier> does not help either.

So, is there a way do cast specificManager into an IManager<IIdentifier>?

Thanks and all the best.

  • 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-02T12:19:47+00:00Added an answer on June 2, 2026 at 12:19 pm

    With IManager<IIdentifier> you can do such thing:

    IIdentifier entity = new NotSpecificEntity();
    manager.DoStuffWith(entity);
    

    That will lead to exception, in your SpecificEntityManager, because it accepts only parameters of type ISpecificEntity

    UPDATE:
    You can read more about covariance and contravariance in C# at Eric Lippert’s blog

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

Sidebar

Related Questions

Suppose I have the following classes: public class Test { public static void main(String[]
I use Django social-auth (omab version just to avoid any confusion with the other
To avoid the long class path issue in windows, i modified my build.xml like[1]
I am a novice in Java World. How to avoid confusion over variable declaration
Just to avoid confusion I am talking about linq to object and nothing else.
Basically, I want to know what is best to avoid future problems and confusions
To avoid crashing anything I specify the version number for every gem in my
How would I avoid nil checks using Object.try for the following? <%= image_tag(PeriodState.where(sla_id =
I'm trying to avoid handling this improperly encoded request on the server. I have
How do I avoid read locks in my database? Answers for multiple databases welcome!

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.