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 am trying to create a clear method that would clear the array I
It's clear that you can use numeric characters in SQL table names and use
First of all, I want to be clear that I'm not talking about defining
I understand my question is not that clear so let me try explaining it
I know that when delete [] will cause destruction for all array elements and
In C, you can partially initialize a struct or array, with the result that
Edited again because it originally wasn't clear that I'm trying to initialize the arrays
It's clear that Apple has an OpenCL implementation based on Clang and LLVM. There's
Before I start, I want to make it clear that my code is working
I'm getting mixed signals. The warning in the PDO documentation seems pretty clear that

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.