today i was learning about jagged arrays.So i wrote the following code, i wanted to use foreach loop with it, but i am getting following error:
Cannot convert type 'int[]' to 'int' on line 31.
The code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArraysExplained
{
class Program
{
static void Main(string[] args)
{
//use of jagged arrays
int[][] jaggedarray = new int[3][];
jaggedarray[0] = new int[4];
jaggedarray[1] = new int[2];
jaggedarray[2] = new int[3];
// it means there are 3 rows , in the first row jaggedarray[0] has 4 elements
//in the second row i.e jaggedarray[1] has 2 elements
// in the third row i.e jaggedarray[2] has 3 elements
//store value in first row
for (int i = 0; i < 4; i++)
jaggedarray[0][i] = i;
//store value in second row
for (int i = 0; i < 4; i++)
jaggedarray[1][i] = i*2;
//store value in third row
for (int i = 0; i < 4; i++)
jaggedarray[2][i] = i-1;
//display jagged array, it can be displayed row by row or it can be
//displayed using foreach loop
foreach (int x in jaggedarray)
Console.Write(" "+ x);
}
}
}
i am not able to figure out how to remove that error,also any help is appreciated.