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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:15:45+00:00 2026-05-16T08:15:45+00:00

I know this is old, yet I am still not very good with understanding

  • 0

I know this is old, yet I am still not very good with understanding those problems. Can anyone tell me why the following does not work (throws a runtime exception about casting)?

public abstract class EntityBase { }
public class MyEntity : EntityBase { }

public abstract class RepositoryBase<T> where T : EntityBase { }
public class MyEntityRepository : RepositoryBase<MyEntity> { }

And now the casting line:

MyEntityRepository myEntityRepo = GetMyEntityRepo(); // whatever
RepositoryBase<EntityBase> baseRepo = (RepositoryBase<EntityBase>)myEntityRepo;

So, can anyone explain how is this invalid? And, I you are not in the mood to explain – is there a line of code I can use to actually do this cast?

  • 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-16T08:15:45+00:00Added an answer on May 16, 2026 at 8:15 am

    RepositoryBase<EntityBase> is not a base class of MyEntityRepository. You’re looking for generic variance which exists in C# to a limited extent, but wouldn’t apply here.

    Suppose your RepositoryBase<T> class had a method like this:

    void Add(T entity) { ... }
    

    Now consider:

    MyEntityRepository myEntityRepo = GetMyEntityRepo(); // whatever
    RepositoryBase<EntityBase> baseRepo = (RepositoryBase<EntityBase>)myEntityRepo; 
    baseRepo.Add(new OtherEntity(...));
    

    Now you’ve added a different kind of entity to a MyEntityRepository… and that can’t be right.

    Basically, generic variance is only safe in certain situations. In particular generic covariance (which is what you’re describing here) is only safe when you only ever get values “out” of the API; generic contravariance (which works the other way round) is only safe when you only ever put values “into” the API (e.g. a general comparison which can compare any two shapes by area can be considered as a comparison of squares).

    In C# 4 this is available for generic interfaces and generic delegates, not classes – and only with reference types. See MSDN for further information, read <plug>read C# in Depth, 2nd edition, chapter 13</plug> or Eric Lippert’s blog series on the topic. Also, I gave a one hour talk about this at NDC in July 2010 – the video is available here.

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

Sidebar

Related Questions

I know this is an old question, but I have spend any hours on
I know this topic is bit old, but i did surf the web and
I know this has been done many times before (some posts are really old
I know this is possible in Perl, but I was wondering if this can
I know this is probably something simple but I can't seem to find anything
This regex thing is getting old. :( Yet another question: I need to count
I'm still very new to Mercurial so please let me know what I'm doing
I know that this question is being asked before but I did not get
Has anyone nailed this down yet? I've read a lot of forum postings and
I know this is an old question and must have been answered hundred times

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.