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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:00:05+00:00 2026-05-13T10:00:05+00:00

Look at the following example (partially taken from MSDN Blog ): class Animal {

  • 0

Look at the following example (partially taken from MSDN Blog):

class Animal { }
class Giraffe : Animal { }

static void Main(string[] args)
{
    // Array assignment works, but...
    Animal[] animals = new Giraffe[10]; 

    // implicit...
    List<Animal> animalsList = new List<Giraffe>();

    // ...and explicit casting fails
    List<Animal> animalsList2 = (List<Animal>) new List<Giraffe>();
}

Is this a covariance problem? Will this be supported in the future C# release and are there any clever workarounds (using only .NET 2.0)?

  • 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-13T10:00:06+00:00Added an answer on May 13, 2026 at 10:00 am

    Well this certainly won’t be supported in C# 4. There’s a fundamental problem:

    List<Giraffe> giraffes = new List<Giraffe>();
    giraffes.Add(new Giraffe());
    List<Animal> animals = giraffes;
    animals.Add(new Lion()); // Aargh!
    

    Keep giraffes safe: just say no to unsafe variance.

    The array version works because arrays do support reference type variance, with runtime checking. The point of generics is to provide compile-time type safety.

    In C# 4 there will be support for safe generic variance, but only for interfaces and delegates. So you’ll be able to do:

    Func<string> stringFactory = () => "always return this string";
    Func<object> objectFactory = stringFactory; // Safe, allowed in C# 4
    

    Func<out T> is covariant in T because T is only used in an output position. Compare that with Action<in T> which is contravariant in T because T is only used in an input position there, making this safe:

    Action<object> objectAction = x => Console.WriteLine(x.GetHashCode());
    Action<string> stringAction = objectAction; // Safe, allowed in C# 4
    

    IEnumerable<out T> is covariant as well, making this correct in C# 4, as pointed out by others:

    IEnumerable<Animal> animals = new List<Giraffe>();
    // Can't add a Lion to animals, as `IEnumerable<out T>` is a read-only interface.
    

    In terms of working around this in your situation in C# 2, do you need to maintain one list, or would you be happy creating a new list? If that’s acceptable, List<T>.ConvertAll is your friend.

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

Sidebar

Related Questions

Lets look at the following example: public class BothPaintAndPrintable implements Paintable,Printable{ public void print()
Please take a look on the following example: class Base { protected: int m_nValue;
Look at the following example: string foo(int i) { string a; ... Process i
Take a look at the following example: public void inc(int num) { num++; }
Have a look at the following Scala example: class A { def foo(x: Int):
I have some difficulties with understanding BindingSource's behaviour. Let's look at following example: Creating
Please look the following query: SELECT ID, START, END FROM TABLEA the result is:
Have a look at following scenario: public class ParentClass { private Integer testVar =
Look at the following two ways to create a new object of class Y:
Is it possible to style JSF commandbutton tags to look like the following example:

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.