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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:55:28+00:00 2026-06-02T11:55:28+00:00

I already posted this question with a bad sample . This code should be

  • 0

I already posted this question with a bad sample.

This code should be better.

To avoid confusion I summarised some code:

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            IManager<ISpecificEntity> specificManager = new SpecificEntityManager();
            IManager<IAntoherSpecificEntity> anotherSpecificManager = new AnotherSpecificEntityManager();
            Dictionary<string, IManager<IIdentifier>> managers = new Dictionary<string, IManager<IIdentifier>>();
            managers.Add("SpecificManager", (IManager<IIdentifier>)specificManager);
            managers.Add("AnotherSpecificManager", (IManager<IIdentifier>)anotherSpecificManager);

            foreach (var manager in managers.Values)
            {
                IIdentifier entity = manager.Container.GetEntity();
            }
        }
    }

    internal interface IIdentifier
    {
        int Id { get; set; }
    }

    internal interface ISpecificEntity : IIdentifier
    {
        string SpecificValue { get; set; }
    }

    internal class SpecificEntity : ISpecificEntity
    {
        public int Id { get; set; }
        public string SpecificValue { get; set; }
    }


    internal interface IAntoherSpecificEntity : IIdentifier
    {
        string AnotherSpecificValue { get; set; }
    }

    internal class AntoherSpecificEntity : IAntoherSpecificEntity
    {
        public int Id { get; set; }
        public string AnotherSpecificValue { get; set; }
    }

    internal interface IContainer<out TIdentifier> where TIdentifier : IIdentifier
    {
        TIdentifier GetEntity();
    }

    internal interface ISpecificContainer : IContainer<ISpecificEntity>
    {
    }

    internal class SpecificContainer : ISpecificContainer
    {
        public ISpecificEntity GetEntity()
        {
            return new SpecificEntity { SpecificValue = "SpecificValue" };
        }
    }

    internal interface IAnotherSpecificContainer : IContainer<IAntoherSpecificEntity>
    {
    }

    internal class AnotherSpecificContainer : IAnotherSpecificContainer
    {
        public IAntoherSpecificEntity GetEntity()
        {
            return new AntoherSpecificEntity { AnotherSpecificValue = "AnotherSpecificValue" };
        }
    }

    internal interface IManager<TIdentifier> where TIdentifier : IIdentifier
    {
        IContainer<TIdentifier> Container { get; set; }
    }

    internal class SpecificEntityManager : IManager<ISpecificEntity>
    {
        public IContainer<ISpecificEntity> Container { get; set; }
    }

    internal class AnotherSpecificEntityManager : IManager<IAntoherSpecificEntity>
    {
        public IContainer<IAntoherSpecificEntity> Container { get; set; }
    }
}

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

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> or IManager<out TIdentifier> does not work 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-02T11:55:29+00:00Added an answer on June 2, 2026 at 11:55 am

    First of all you get the idea of generics wrong.

    If you Look at Foo its a generic type.
    Foo and Foo are NEW types, they do not derive from List, neither the types are conneted by means of inheritance. Using generics creates new types, its does not derive!

    However what you are looking for is Covariance and Contravariance. This will allow you to create a kind of “generic polymorphism”, but for that you need to specify this within your generic definition. Therefore it will only work for very few framework generics out of the box.

    class Program
    {
        static void Main(string[] args)
        {
            IManager<IIdentifier> f1 = new C1();
            IManager<IIdentifier> f2 = new SpecificEntityManager(); //IManager<ISpecificEntity>
        }
    }
    
    interface IIdentifier { }
    interface ISpecificEntity : IIdentifier { }
    interface IManager<out T> { }
    
    class C1 : IManager<IIdentifier> { }
    class SpecificEntityManager : IManager<ISpecificEntity> { }
    

    Here is what YOU have to change:

    internal interface IContainer<out TIdentifier> where TIdentifier : IIdentifier 
    { 
        TIdentifier GetEntity(); 
    }
    internal interface IManager<out TIdentifier> where TIdentifier : IIdentifier 
    {
        IContainer<IIdentifier> Container { get; }
    }
    internal class SpecificEntityManager : IManager<ISpecificEntity>
    {
        public IContainer<IIdentifier> Container { get; set; }
    }
    internal class AnotherSpecificEntityManager : IManager<IAntoherSpecificEntity>
    {
        public IContainer<IIdentifier> Container { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question was already posted in StackOverflow but I either didnt understand
I have already posted a question about this, but the situation has changed sufficiently
I'm sure that someone already posted this question; however, I would like to get
Before I posted this question, I already looked this , but I couldn't get
This question is a general one, and I've already posted a version of it
I already posted a question about this ( Abandoned instances that will not continue
I have already posted another question about this, but no one seemed to know
I've already posted this question on the authors website , but I thought I
I am sorry if this question is already posted somewhere. I am very new
This question has already been posted before, however, the correct answer doesn't seem to

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.