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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:34:45+00:00 2026-05-14T23:34:45+00:00

I think this might be a pretty simple question, but I haven’t been able

  • 0

I think this might be a pretty simple question, but I haven’t been able to figure it out yet. If I’ve got a 2-dimensional array like so:

int[,] array = new int[2,3] { {1, 2, 3}, {4, 5, 6} };

What’s the best way to iterate through each dimension of the array with a nested foreach statement?

  • 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-14T23:34:45+00:00Added an answer on May 14, 2026 at 11:34 pm

    If you want to iterate over every item in the array as if it were a flattened array, you can just do:

    foreach (int i in array) {
        Console.Write(i);
    }
    

    which would print

    123456

    If you want to be able to know the x and y indexes as well, you’ll need to do:

    for (int x = 0; x < array.GetLength(0); x += 1) {
        for (int y = 0; y < array.GetLength(1); y += 1) {
            Console.Write(array[x, y]);
        }
    }
    

    Alternatively you could use a jagged array instead (an array of arrays):

    int[][] array = new int[2][] { new int[3] {1, 2, 3}, new int[3] {4, 5, 6} };
    foreach (int[] subArray in array) {
        foreach (int i in subArray) {
            Console.Write(i);
        }
    }
    

    or

    int[][] array = new int[2][] { new int[3] {1, 2, 3}, new int[3] {4, 5, 6} };
    for (int j = 0; j < array.Length; j += 1) {
        for (int k = 0; k < array[j].Length; k += 1) {
            Console.Write(array[j][k]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I imagine this is pretty dead-simple, but somehow I haven’t been able to get
I mostly develop using C#, but I think this question might be suitable for
okay this might be a pretty lame and basic question but its stuck in
Firstly, This might seem like a long question. I don't think it is... The
You might think that this question is already answered. However, I couldn't find the
This might be a very simple thing. Check out the normal sql query below
This might be a pretty basic question, or peculiar to my set up (hopefully
Note: This might seem like a Super User question at first, but please read
I think this might be a problem with the theme I'm using ( Nitobe
This might be something very straight forward and I really think it should work

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.