Thanhtu150
Thanhtu150

Reputation: 91

Get many values in a loop

I have a 4 arrays:

var array1[] = {1,2,3,4}
var array2[] = {a,b,c,d}
var array3[] = {A,B,C,D}
var array4[] = {10,20,30,40}

Now, I want to GET 4 values from this 4 array in 1 loop, so how can do it, like this output for 1 loop:

"1,a,A,10"

Upvotes: 0

Views: 90

Answers (3)

chouaib
chouaib

Reputation: 2817

I guess you wanted :

var array2[] = {'a','b','c','d'};
var array3[] = {'A','B','C','D'};

by your 2nd and 3rd arrays

anyway you can loop through them as mentioned in the comment

for(int i=0; i<4; i++)
{
   Console.WriteLine("\"{0},{1},{2},{3}\"", array1[i], array2[i], array3[i], array4[i]);
}

Upvotes: 3

void
void

Reputation: 7890

another way is to put all arrays into one array and loop through it:

    static void Main(string[] args)
    {
        Object[] array1 = { 1, 2, 3, 4 };
        Object[] array2 = { 'a', 'b', 'c', 'd' };
        Object[] array3 = { 'A', 'B', 'C', 'D' };
        Object[] array4 = { 10, 20, 30, 40 };
        var arrays = new Object[][] { array1, array2, array3, array4 };
        string str = "";
        for (int i = 0; i < arrays.Length; i++)
        {
            str += arrays[i][0].ToString()+',';
        }
        str = str.Remove(str.Length - 1);
        Console.WriteLine(str);
        Console.ReadLine();
    }

Output: 1,a,A,10

Upvotes: 0

David Pilkington
David Pilkington

Reputation: 13628

for(int i=0; i<4; i++) 
{
 String a = String.Format("\"{0},{1},{2},{3}\"", array1[i], array2[i], array3[i], array4[i]);

 // Do what you want with the value here
}

Upvotes: 0

Related Questions