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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:51:51+00:00 2026-05-20T08:51:51+00:00

This is my very first time to post a question here, so thanks in

  • 0

This is my very first time to post a question here, so thanks in advance to anyone will help me (and maybe others).

Today I’ve been puzzled with covariance and contravariance in C# 4, but I really can’t see any possibility to do what I would.
NOTE: all that is NOT to solve a specific problem, but I’d like to know if there’s any legal way (brand-new co/contra-variance in C# 4) to solve it.
So, don’t get crazy…

Here the sample code:

public interface IBase<in T>
{
    T GetValue();
    ...
}


public class Readable<T> : IBase<T>
{
    public T GetValue() {/* */}
    ...
}


public class Writeable<T> : IBase<T>
{
    public T GetValue() {/* */}
    public void SetValue(T value) {/* */}
    ...
}


public class Program
{
    private List<IBase<object>> _collection = new List<IBase<object>>();


    public static void Main()
    {
        var p = new Program();

        p._collection.Add(new Readable<bool>());    //not allowed
    }
}

The collection should be able to host any of the IBase derivatives, being them of any T.
Of course I may use a non-generic collection or a List and the game is over.
Another idea is having a more common interface IBaseCore, then IBase inherits from.
My question is: without these tricks may I achieve to have a collection of IBase and add items of any IBase?

Thanks to everyone.

  • 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-20T08:51:52+00:00Added an answer on May 20, 2026 at 8:51 am

    EDIT: First things first, your variance is wrong for IBase<T>. It should be out T (covariant) rather than in T (contravariant).

    The problem here is simply that generic covariance and contravariance doesn’t work with value types as the type arguments. So you could add a Readable<string>, but not a Readable<bool>.

    One option is to have a nongeneric base interface:

    public interface IBase
    {
        object GetValueAsObject();
    }
    
    public interface IBase<out T> : IBase
    {
        T GetValue();
        ...
    }
    

    Then you could have a List<IBase>.

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

Sidebar

Related Questions

This is my very first question so I am a bit nervous about it
this is my first post on this website, but I'm all the time getting
I'm not entirely sure how to post this question, but here goes... I have
This is my first post, have been a lurker for a long time, so
I'm sorry for this very newbish question, I'm not much given into web development.
Background First of all, much gratitude to atebits for their very informative blog post
My first post on this site with huge hope:: I am trying to understand
First of all, I love ASP.NET MVC. This question is not a criticism of
Hi this question or problem i have its very hard i have search and
I have written a very simple socket server. It listens in post 63254. First

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.