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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:37:18+00:00 2026-06-16T00:37:18+00:00

I wanted to do something like co-variance in C# but I dont have here

  • 0

I wanted to do something like co-variance in C# but I dont have here any inheritance .

I have this code :

public interface IBirthday
{
   void Dance ();
}


public class Birthday:IBirthday
{
 public void Dance()
    {}
 }


void Main()
{
  List<Birthday> l= new List<Birthday>();
  List<IBirthday> d = l; //<--- How can I accomplish that ? 
}

HOw can I make this work ? (besides iterating and build manually ( linq or loop))

List<IBirthday> d = list of birthdays ? 

does iterating/linq is the only choice?

  • 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-16T00:37:19+00:00Added an answer on June 16, 2026 at 12:37 am

    It’s not possible to assign a List<Birthday> to a List<IBirthday> without creating a brand new list (it can be hidden from you, but it must happen somewhere).

    If what you were trying to do were possible then what would happen when someone did:

    public class UnBirthday : IBirthday { ... }
    
    d.Add(new UnBirthday());
    

    Well, it’s a List<IBirthday>, so it thinks that it’s able to add the new item. But the underlying list is actually a List<Birthday>, and you can’t add an UnBirthday to that list. Given the choice between allowing this and just crashing at runtime, C# made the decision (correctly, in my opinion) of just not allowing it in the first place.

    The only way to utilize generic argument covariance, which is what you’re trying to do, is to assign it to something that can only read information out, and never put information in. One example of this is IEnumerable<T>. Every single Birthday is an IBirthday, and there’s no way for IEnumerable to be given an IBirthday that’s not a Birthday, so saying:

    List<Birthday> l= new List<Birthday>();
    IEnumerable<IBirthday> d = l;
    

    works just fine.

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

Sidebar

Related Questions

So I have always wanted to do something like this, but never knew how
I wanted to do something like this code, but the date does not let
So I wanted to have something like in this image . Basically you have
I wanted to design something like this. I tried hard to style my code
So I wanted to do something like this: @Component @Scope(value=request, proxyMode=ScopedProxyMode.INTERFACES) public class MyBean
e.g. if you have code that does something like this somewhere in your codebase:
If I wanted to do something like this: collection.each do |i| return nil if
I wanted something like in this img (text btn and arrow btn of same
I wanted to create something like zazzle.com or puma.jp/tribes ( see this SO post
when i have a link like this which can be longer but i've put

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.