Clear Array

  • Thread starter Thread starter ron
  • Start date Start date
R

ron

Hi,
Could i clear the a single dimension array and a jagged
array in the following way.

int[] mySingleArr = new int[10];

mySingleArr = System.Array.Clear();

int[][] myJaggedArr = new int[3][];
myJaggedArr = new int[0][3];
myJaggedArr = new int[1][3];
myJaggedArr = new int[2][3];

myJaggedArr = System.Array.Clear();

Is this possiable without iterating through the arrays
and setting the elements to zero?

Thanks Ron
 
I don't know what exactly you're getting at, but why don't you just use
multi-dimensional arrays like this:

int[,] a2Darray = new int[4, 10];
a2Darray[0,0] = 1;
a2Darray[0,1] = 2;
....[etc]...

What you're doing with your "jagged" array is creating an array inside of an
array.. that's old C style. C# has new built-in features for
multi-dimensional arrays.

Chris LaJoie
 
ron said:
Could i clear the a single dimension array and a jagged
array in the following way.

int[] mySingleArr = new int[10];

mySingleArr = System.Array.Clear();

No, because Array.Clear takes arguments and returns void. You mean:

Array.Clear (mySingleArr, 0, mySingleArr.Length);
int[][] myJaggedArr = new int[3][];
myJaggedArr = new int[0][3];
myJaggedArr = new int[1][3];
myJaggedArr = new int[2][3];

myJaggedArr = System.Array.Clear();

Is this possiable without iterating through the arrays
and setting the elements to zero?

In this case you're not actually wanting to clear myJaggedArr itself,
which would set it back to being an array of 3 null references, but
each member of those three arrays - so you need to do precisely that:

foreach (int[] subArray in myJaggedArr)
{
Array.Clear (subArray, 0, subArray.Length);
}
 
Back
Top