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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:57:39+00:00 2026-05-16T04:57:39+00:00

I’m trying to walk through a bunch of items, each item has an array

  • 0

I’m trying to walk through a bunch of items, each item has an array of List<> objects that I want to convert to an array of arrays.. Here’s the code to do that:

foreach (IngredientNode i in _snapshot._ingredientMap.Values)
{
   for (int c = 0; c < NUM_TAGS; c++)
   {
      if (i.RecipesByTag[c] == null) continue;
      i.RecipesByTag[c] = i.RecipesByTag[c].ToArray<RecipeNode>();
   } <--- EXCEPTION
}

RecipesByTag has a static type of IEnumerable<RecipeNode>[]. However, its dynamic type is List<RecipeNode>[]. I want to go through each one of those and convert the dynamic type of RecopeNode[]. Under the debugger, this works and i.RecipesByTag gets converted. However, the last curly brace then throws the exception:

Attempted to access an element as a type incompatible with the array.

I have a feeling there’s some sort of stack corruption going on. Can someone explain what’s happening at a technical level here? Thanks!

  • 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-16T04:57:39+00:00Added an answer on May 16, 2026 at 4:57 am

    You shouldn’t have to specify the type argument for the ToArray method, it should be inferred from it’s usage, if you are using strongly typed collections. This is a generic type casting problem. You are trying to put elements in an array of some incompatible type.

    Your problem should boil to this (these arrays are covariant):

    object[] obj = new string[1];
    obj[0] = 5; // compiles fine, yields runtime error
    

    Now, the same thing, with different types (these arrays are also covariant):

    IEnumerable<int>[] x = new List<int>[1];
    x[0] = new int[1]; // compiles fine, yields runtime error
    

    It should be obvious why the type system doesn’t like this. Basically, you look at it as if it was an array of IEnumerable<int> but it’s actually an array of List<int>. You can not put an unrelated type array of int, into that array.

    I believe Eric Lippert explains this very well on his blog.

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

Sidebar

Related Questions

No related questions found

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.