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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:09:58+00:00 2026-06-05T17:09:58+00:00

Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar

  • 0

Consider this contrived, trivial example:

    var foo = new byte[] {246, 127};
    var bar = foo.Cast<sbyte>();
    var baz = new List<sbyte>();
    foreach (var sb in bar)
    {
        baz.Add(sb);
    }
    foreach (var sb in baz)
    {
        Console.WriteLine(sb);
    }

With the magic of Two’s Complement, -10 and 127 is printed to the console. So far so good. People with keen eyes will see that I am iterating over an enumerable and adding it to a list. That sounds like ToList:

    var foo = new byte[] {246, 127};
    var bar = foo.Cast<sbyte>();
    var baz = bar.ToList();
    //Nothing to see here
    foreach (var sb in baz)
    {
        Console.WriteLine(sb);
    }

Except that does not work. I get this exception:

Exception type: System.ArrayTypeMismatchException

Message: Source array type cannot be assigned to destination array type.

I find this exception very peculiar because

  1. ArrayTypeMismatchException – I’m not doing anything with arrays, myself. This seems to be an internal exception.
  2. The Cast<sbyte> works fine (as in the first example), it’s when using ToArray or ToList the problem presents itself.

I’m targeting .NET v4 x86, but the same occurs in 3.5.

I don’t need any advice on how to resolve the problem, I’ve already managed to do that. What I do want to know is why is this behavior occurring in the first place?

EDIT:

Even weirder, adding a meaningless select statement causes the ToList to work correctly:

var baz = bar.Select(x => x).ToList();
  • 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-06-05T17:10:00+00:00Added an answer on June 5, 2026 at 5:10 pm

    Okay, this really depends on a few oddities combined:

    • Even though in C# you can’t cast a byte[] to an sbyte[] directly, the CLR allows it:

      var foo = new byte[] {246, 127};
      // This produces a warning at compile-time, and the C# compiler "optimizes"
      // to the constant "false"
      Console.WriteLine(foo is sbyte[]);
      
      object x = foo;
      // Using object fools the C# compiler into really consulting the CLR... which
      // allows the conversion, so this prints True
      Console.WriteLine(x is sbyte[]);
      
    • Cast<T>() optimizes such that if it thinks it doesn’t need to do anything (via an is check like the above) it returns the original reference – so that’s happening here.

    • ToList() delegates to the constructor of List<T> taking an IEnumerable<T>

    • That constructor is optimized for ICollection<T> to use CopyTo… and that’s what’s failing. Here’s a version which has no method calls other than CopyTo:

      object bytes = new byte[] { 246, 127 };
      
      // This succeeds...
      ICollection<sbyte> list = (ICollection<sbyte>) bytes;
      
      sbyte[] array = new sbyte[2];
      
      list.CopyTo(array, 0);
      

    Now if you use a Select at any point, you don’t end up with an ICollection<T>, so it goes through the legitimate (for the CLR) byte/sbyte conversion for each element, rather than trying to use the array implementation of CopyTo.

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

Sidebar

Related Questions

Consider an object chain like: foo.bar.baz.bing In this particular instance, I am given foo
Consider this code: new Ajax.Request('?service=example', { parameters : {tags : 'exceptions'}, onSuccess : this.dataReceived.bind(this)
Consider this contrived example. The following array works fine if I want to convert
Consider this admittedly contrived Generic Definition: private void Foo<T,BASETYPE>(PropertyInfo prop, BASETYPE o1, BASETYPE o2)
Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B
This is a contrived example, but consider a scenario where employees have working locations,
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
I realize this is a very contrived example, but I've simplified the full version
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
Consider the following contrived example of a templated array definition: template <typename t, unsigned

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.