iJade
iJade

Reputation: 23811

Iterate array in C#

Consider I have a number of arrays as follows:

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

How do I iterate over such arrays like the following?

int i;
int j;
for(i=0; i<3; i++) {
    // Iterate all the above three arrays here
}

I want to dynamically iterate all arrays starting with op by just changing the index.

I'm using C#.

Upvotes: 0

Views: 2833

Answers (6)

volga
volga

Reputation: 11

Use:

for(int i=0;i<9;i++)
{
    for(int j=0;j<9;i++)
    {                             
        // Iterate your string op1,op2,op3 for the desired result.    
    }
}

Upvotes: -3

Abbas
Abbas

Reputation: 14432

You can't do that. Easier would be to add the arrays to a List<T> and iterate the list to iterate the arrays:

List<string[,]> arrays = new List<string[,]>
{
    new string[9, 9],
    new string[9, 9],
    new string[9, 9]
};

foreach(var array in arrays)
{
    // Do something with the array...
}

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109792

You can get clever by writing a method using the params keyword which will automatically create an array of arrays for you.

To do that, you have to write an intermediary wrapper class for the arrays, because the params keyword can only be used with a single dimensional array.

I really only provide this code for the curious - you probably wouldn't really need to go to these lengths in real code. However, if you did find yourself often wanting to iterate over a set of two dimensional arrays, you can use this approach.

After writing the (reusable) helper classes your code to iterate the arrays would look like this:

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

IterateArrays<string>(processArray, op1, op2, op3);

Where the processArray() method would be along these lines:

static void processArray(string[,] array, int index)
{
    Console.WriteLine("Processing array with index " + index);
}

Here's the full compilable example:

using System;

namespace ConsoleApp1
{
    public class ArrayWrapper<T>
    {
        public T[,] Array;

        public static implicit operator ArrayWrapper<T>(T[,] array)
        {
            return new ArrayWrapper<T> {Array = array};
        }
    }

    sealed class Program
    {
        void run()
        {
            string[,] op1 = new string[9, 9];
            string[,] op2 = new string[9, 9];
            string[,] op3 = new string[9, 9];

            IterateArrays<string>(processArray, op1, op2, op3);
        }

        static void processArray(string[,] array, int index)
        {
            Console.WriteLine("Processing array with index " + index);
        }

        public static void IterateArrays<T>(Action<T[,], int> action, params ArrayWrapper<T>[] arrays)
        {
            for (int i = 0; i < arrays.Length; ++i)
                action(arrays[i].Array, i);
        }

        static void Main(string[] args)
        {
            new Program().run();
        }
    }
}

Like I said, this is just to show how you can approach it. It'd just use @thumbmunkeys suggestion in real code.

Upvotes: 2

bosnjak
bosnjak

Reputation: 8624

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

List<string[,]> l = new List<string[,]>();

l.add(op1);
l.add(op2);
l.add(op3);

foreach(string[,] op in l)
{
 // iterate over op here
}

Or, if you don't want the additional lines to add the arrays to the list, you can:

List<string[,]> ops = new List<string[,]>{
    new string[9, 9];
    new string[9, 9];
    new string[9, 9];
}

foreach(string[,] op in ops)
{
 // iterate over op here
}

Upvotes: 1

thumbmunkeys
thumbmunkeys

Reputation: 20764

You can make an array on the fly and iterate:

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

foreach (var item in new[] { op1, op2, op3 })
{
     //...
}

Upvotes: 10

Max
Max

Reputation: 13388

You could create a list<string[,]> containing the string[,]'s

string[,] op1 = new string[9, 9];
string[,] op2 = new string[9, 9];
string[,] op3 = new string[9, 9];

//Create List, containing `string[,]`
List<string[,]> opList = new List<string[,]>();
//Add String[,]'s to list
opList.Add(op1);
opList.Add(op2);
opList.Add(op3);

//Loop over list
foreach(var itm in opList)
{
    //approach string[,]'s here
}

Upvotes: 1

Related Questions