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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:50:13+00:00 2026-05-21T04:50:13+00:00

abstract class A<T> { List<T> Items { get; set; } } class B {}

  • 0
abstract class A<T> {
  List<T> Items { get; set; }
}

class B {}
class C : A<B> {}

class D : B {}
class E : A<D> {}

static class X {
  public A<B> GetThing(bool f) {
    return f ? new E() : new C();
  }
}

Type of conditional expression cannot be determined because there is no implicit conversion between ‘ConsoleApplication4.E’ and ‘ConsoleApplication4.C’

Now I get the “why” (I think) but I can’t see how to make this compile. I think I have to create an interface which defines variance of some kind and inherit from that, but I’m not sure. But regardless, E() should inherit from C().

Any takers?

TIA

  • 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-21T04:50:14+00:00Added an answer on May 21, 2026 at 4:50 am

    To use generic variance in C# you have to meet all the following conditions:

    1. Use C# 4
    2. The type that varies must be a generic interface or generic delegate
    3. The type parameter must be marked “in” (contravariant) or “out” (covariant)
    4. The type parameter annotations must yield a type which is provably typesafe in all its possible operations
    5. The “source” type argument and the “destination” type argument must have an identity or reference conversion between them.

    Your program meets condition 1 and condition 5 but not conditions 2, 3, or 4.

    There is no way to get the variance you want because you want something that would violate condition 4. Watch what happens when we meet all the conditions except condition 4:

    // Suppose this declaration were legal:
    interface IMyCollection<out T> 
    {
      List<T> Items { get; set; } 
    }
    
    class Animal {}
    class AnimalCollection : IMyCollection<Animal> 
    { 
      public List<Animal> { get; set; }
    }
    
    class Giraffe : Animal {}
    class GiraffeCollection : IMyCollection<Giraffe> 
    {
      public List<Giraffe> { get; set; } 
    }
    
    static class X 
    {
      public IMyCollection<Animal> GetThing() 
      {
        // IMyCollection is covariant in T, so this is legal.
        return new GiraffeCollection(); 
      }
    }
    
    class Tiger : Animal {}
    
    ...
    
    IMyCollection<Animal> animals = X.GetThing(); 
    // GetThing() actually returns a GiraffeCollection
    animals.Items = new List<Animal>() { new Tiger(); }
    

    And a giraffe collection now contains a list of animals that contains a tiger.

    You have to be typesafe if you’re going to use variance. Because the compiler cannot determine the variance annotations to be typesafe, it rejects the declaration of IMyCollection.

    Let’s see how we might do this and be typesafe. The problem is that Items is writable via the interface.

    interface IMyCollection<out T> 
    {
      IEnumerable<T> Items { get; }
    }
    
    class Animal {}
    class AnimalCollection : IMyCollection<Animal> 
    { 
      public IEnumerable<Animal> { get { yield return new Tiger(); } }
    }
    
    class Giraffe : Animal {}
    class GiraffeCollection : IMyCollection<Giraffe> 
    {
      public IEnumerable<Giraffe> { get { yield return new Giraffe(); } } 
    }
    
    static class X 
    {
      public IMyCollection<Animal> GetThing() 
      {
        return new GiraffeCollection();
      }
    }
    
    class Tiger : Animal {}
    
    ...
    
    MyCollection<Animal> animals = X.GetThing(); 
    // GetThing() actually returns a GiraffeCollection
    foreach(Animal animal in animals.Items) { ... } 
    // Items yields a giraffe, which is an animal
    

    Perfectly typesafe. This would be a legal C# 4 program.

    If the design details of covariance and contravariance in C# 4 interests you, you might consider reading my dozen articles on the subject. You can find them here:

    http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/

    Note that these are listed in most-to-least-recent order; start from the bottom.

    If in particular you are interested in the rules for determining when an interface’s annotations are valid, see this article (which I just discovered and fixed some errors in, so I’m glad we had this conversation.)

    http://blogs.msdn.com/b/ericlippert/archive/2009/12/03/exact-rules-for-variance-validity.aspx

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

Sidebar

Related Questions

abstract class Foo { private List<Object> container; private bool update; Foo Foo() { container
I have an abstract class: type TInterfaceMethod = class abstract public destructor Destroy; virtual;
Following a sample of my code: public abstract class<T> { public List<T> GetSomething(string q)
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
When writing an abstract class, or a class that doesn't get instantiated directly... do
I have forms posting data from instances of a particular abstract class: public abstract
I have a generic list class that implements a particular interface. The list items
Hi is it possible to cast a List? i have an abstract class that
I have an abstract class that other classes are inheriting this. public abstract class
I have just implemented some classes: public abstract class Job<T extends ViewerUnion>{ [...] }

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.