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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:40:29+00:00 2026-05-16T20:40:29+00:00

It is clear that the T[] array type is not covariant as the elements

  • 0

It is clear that the T[] array type is not covariant as the elements of a T[] can be set by index.

And yet, a U[] can be cast to a T[] without any complaints from the compiler as long as U derives from T.

Man[] men = new[] { new Man("Aaron"), new Man("Billy"), new Man("Charlie") };
Person[] people = (Person[])men;

In the above code it appears that men and people do seem to hold a reference to the same Array object. The effect of setting men[0] = new Man("Aidan") can be seen at people[0]. Similarly attempting people[0] = new Woman("Debbie") results in an ArrayTypeMismatchException at runtime*.

Does this mean that the T[] type actually performs type checking on every set call? It seems that this must be necessary if it is allowed to cast arrays in this manner.

I guess my question is just: How is this possible? It’s clear to me that U[] does not derive from T[]. It’s also unclear to me whether I could ever define my own type that would work in this way: actually be invariant but act covariant.


*Though array variance is apparently permitted by the CLR, any language could disallow casting between array types. However, it appears that this behavior is identical in VB.NET:

Dim men = New Man() { New Man("Aaron"), New Man("Billy"), New Man("Charlie") }
Dim people = CType(men, Person())
  • 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-16T20:40:30+00:00Added an answer on May 16, 2026 at 8:40 pm

    This is a special behavior of arrays and cannot be replicated in any other type.

    It is generally regarded as a mistake.

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

Sidebar

Related Questions

I find it annoying that I can't clear a list. In this example: a
As I put together each asp.net page It's clear that most of the time
Now that it's clear what a metaclass is , there is an associated concept
It seems that everybody knows you're supposed to have a clear distinction between the
I'm trying to think of clever, clear, and simple ways to write code that
I created a class Foo that has the method toArray() that returns an Array<Int>
(Note to any future readers: The error, unsurprisingly, is in my code and not
Executive summary: How can I define an arbitrarily-sized 2D array in C? How can
I have a simple 2-column layout with a footer that clears both the right
Is there a function, that clears last line in command prompt? I dont mean

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.