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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:24:37+00:00 2026-05-15T01:24:37+00:00

Can I have a type (for now forgetting its semantics) that can be covariant

  • 0

Can I have a type (for now forgetting its semantics) that can be covariant as well as contravariant?

for example:

public interface Foo<in out T>
{
    void DoFooWith(T arg);
}

Off to Eric Lippert’s blog for the meat and potatoes of variance in C# 4.0 as there’s little else anywhere that covers adequate ground on the subject.


I tried it out anyway, not only does it not allow that, but it tells me I am missing the whole point. I need to understand the link between read-only, write-only and variance.

I guess I got some more reading to do.

But any short, epiphany inducing answers are welcome, meanwhile.

  • 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-15T01:24:37+00:00Added an answer on May 15, 2026 at 1:24 am

    No, you cannot do that.

    Suppose that were legal. You make an IFoo<Giraffe>. Since IFoo is covariant in T, you can convert it via typesafe reference conversion to IFoo<object>. Since it is contravariant, you can convert that to IFoo<Banana>. What possible semantics are there for IFoo<T> such that it makes sense to be able to convert an IFoo of Giraffes to an IFoo of Bananas via reference conversion? Giraffes and Bananas have nothing in common other than being reference types. You cannot possibly have a method on IFoo<Banana> that returns a Banana, because it might actually be an implementation of IFoo<Giraffe>; how would the author of the implementation know to hand out a Banana? You cannot possibly have a method on IFoo<Banana> that takes a Banana for the same reason; the implementor of IFoo<Giraffe> is expecting you to hand him a Giraffe.

    Here’s another way of looking at it:

    • “in T” means (roughly) “T appears only in input positions”.
    • “out T” means (roughly) “T appears only in output positions”.

    Therefore “in out T” would mean… what? As we’ve seen already, it can only mean “T does not appear at all in any method or property.” What’s the point of making a generic type in T where you never use T?

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

Sidebar

Related Questions

I have this anonymous type : var t= new {a=1,b=lalala,c=DateTime.Now}; How can I make
I have an application where users can store items. Now each type of account
I have a menu with submenus that can be toggled (hide/show type deal). Is
I have a templated class, that can have a map type as template parameter.
It looks like you can only have one lifecycle policy per plugin type in
One can say a type parameter T must have a specific supertype S_1: class
I have a textfield where the user can type what ever he want into
I have a gridview: A column ( type int ) How can I get
I have an HTML form where the user can type in any number:5, 8,
I have a data in NSMutableData type. Can I convert the data to a

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.