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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:53:23+00:00 2026-06-09T00:53:23+00:00

When I try to do this… Item[,] array = new Item[w, h]; // Two

  • 0

When I try to do this…

Item[,] array = new Item[w, h];  // Two dimensional array of class Item, 
                                 //   w, h are unknown at compile time.
foreach(var item in array)
{
    item = new Item();
}

…I get Cannot assign to 'item' because it is a 'foreach iteration variable'.

Still, I’d like to do that.

The idea is to assign default Item class values to existing item.

  • 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-09T00:53:26+00:00Added an answer on June 9, 2026 at 12:53 am

    Okay, now that we know your aim instead of how you were trying to achieve it, it’s much easier to answer your question: you shouldn’t be using a foreach loop. foreach is about reading items from a collection – not changing the contents of a collection. It’s a good job that the C# compiler makes the iteration variable read-only, otherwise it would have let you change the value of the variable without that actually changing the collection. (There’d have to be more significant changes to allow changes to be reflected…)

    I suspect you just want:

    for (int i = 0; i < array.GetLength(0); i++)
    {
        for (int j = 0; j < array.GetLength(1); j++)
        {
            array[i, j] = new Item();
        }
    }
    

    That’s assuming it’s a rectangular array (an Item[,]). If it’s an Item[][] then it’s an array of arrays, and you’d handle that slightly differently – quite possibly with a foreach for the outer iteration:

    foreach (var subarray in array)
    {
        for (int i = 0; i < subarray.Length; i++)
        {
            subarray[i] = new Item();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try this code: class MyStuff: def average(a, b, c): # Get the
Lets try this one more time. I'm posting this to get 2 questions answered
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
it s the first time i try this page. I ve just installed imagemagick
Try this: DateFormat df = new SimpleDateFormat(y); System.out.println(df.format(new Date())); Without reading the javadoc for
If I try this, I just get an exception: System.TypeLoadException : Access is denied:
how to get string between [LOOP]...[/LOOP] i try this but not work $gloop =
I get the following error message when I try this query: $query .= ($tid,
Try this: Make a new XCode4 Applescript project. In the delegate, paste this code:

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.