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

The Archive Base Latest Questions

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

If C# can cast an int to an object, why not an int[] to

  • 0

If C# can cast an int to an object, why not an int[] to an object[]?

Simple Program Example:

void Main()
{
    var a = new String[]{"0", "1"};
    var b = new int[]{0, 1};

    AssertMoreThan1(a); // No Exception
    AssertMoreThan1(b); // Exception
}

static void AssertMoreThan1(params object[] v){
    if(v.Length == 1){
        throw new Exception("Too Few Parameters");
    }
}
  • 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-28T19:13:14+00:00Added an answer on May 28, 2026 at 7:13 pm

    If C# can cast an int to an object, why not an int[] to an object[]?

    Your question could be also stated as “what are the covariance rules for array conversions in C#?”

    They are a bit tricky, and broken in several interesting and unfortunate ways.

    First off, we should clearly state what we mean by “covariance”. Covariance is the property that a mapping preserves a relationship. The mapping here is “T goes to array of T”. The relationship is “can be implicitly converted”. For example:

    Giraffe can be implicitly converted to Mammal.

    That’s a relationship between two types. Now apply the mapping to both sides of the relationship:

    Giraffe[] can be converted to Mammal[].

    If the truth of the first statement always entails the truth of the second statement — that is, if the mapping preserves the truth of the relationship — then the mapping is said to be “covariant”.

    As a shorthand, instead of saying “the mapping from T to array of T is a covariant mapping over the implicit conversion relation”, we just say “arrays are covariant” and hope that the rest of that is understood from context.

    OK, now that we have the definition down: Arrays with reference type elements are covariant in C#. Tragically, this is broken covariance:

    class Mammal {}
    class Giraffe : Mammal {}
    class Tiger : Mammal {}
    ...
    Mammal[] mammals = new Giraffe[1];  
    

    This is perfectly legal because arrays of reference type elements are covariant in C#. But then this crashes at runtime:

    mammals[0] = new Tiger();
    

    because mammals is really an array of Giraffes.

    This means that every time you write to an array whose elements are unsealed reference types, the runtime performs a type check and might crash if the type check fails.

    This is my candidate for “worst feature of C#”, but it does in fact work.

    Your question is “why does array covariance not work when the source array is an array of value type and the target array is an array of reference type?”

    Because those two things have a different form at runtime. Suppose you have a byte[] with ten elements. The actual storage reserved for the array elements is ten bytes long. Suppose you are on a 64 bit machine and you have an object[] with ten elements. The storage is eight times bigger!

    Clearly you cannot convert via reference conversion a reference to storage for ten bytes to storage for ten eight-byte references to bytes. The extra seventy bytes don’t come out of nowhere; someone has to allocate them.

    Moreover: who does the boxing? If you have an array of ten objects and each object is a byte, each one of those bytes is boxed. But bytes in a byte array are not boxed. So when you do the conversion, who does the boxing?

    In general in C#, covariant conversions always preserve representation. The representation of a “reference to Animal” is exactly the same as the representation of “reference to Giraffe”. But the representations of “int” and “reference to object” are completely different.

    One expects that casting one array type to another does not allocate and copy a huge array. But we cannot have referential identity between an array of ten bytes and an array of eighty bytes containing ten references, and therefore the entire thing is simply made illegal.

    Now, you might then say, well, what happens when the representations are the same for value types? In fact, this is illegal in C#:

    int[] x = new uint[10];
    

    because in C# the rule is that only covariant array conversions involving only reference types are legal. But if you force it to be done by the runtime:

    int[] x = (int[])(object) new uint[10];
    

    Then the runtime allows it because a four byte int and a four byte uint have the same representation.

    If you want to understand this better then you should probably read my entire series of articles on how covariance and contravariance works in C#:

    • The whole series

    • The specifics of unsafe reference-element array covariance

    • More about value-element array covariance

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

Sidebar

Related Questions

Can you cast a List<int> to List<string> somehow? I know I could loop through
I'm trying to check if an object can cast to a certain type using
How can I cast from a String to a long in a Scala play
How can I cast a bigint variable to a string? DECLARE @id bigint
For example the following cast can be found littered throughout the MSDN documentation: (LPTSTR)&lpMsgBuf
I have a QVariant object within a QTreeWidgetItem, how can I cast it to
I've got an object[] array which contains some mixture of builtin types, like Int/Byte/String,
Can we cast an object with user-defined types, like we do for normal data
I have the following code: public void DeleteAccountsForMonth(int year, int month) { var result
how do I cast void *something to an object in standard C++? Specifically I

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.