Items


Accessing Elements

Another method you may use frequently is the GetValue method.


'Will return the value of the 9th element if there is one.

int myValue = myArray.GetValue(9); 

We can access an array element by passing the item index to the array


int myValue = myArray[0]; 
int myValue = myArray[1];

string[] myArray; 
string[] myArray = new string[3];
myArray[0] = 5;
myArray[1] = 10;
myArray.SetValue(15,2);

GetValue Method



Clear method

You can clear a portion of an array with the Clear method.

System.Array.Clear(ArrayName, Index, Length) 

ArrayName - The array you want to clear
Index - The starting index of the range of elements to clear
Length - The number of elements to clear


clsComponent gaComponentArray(10, 20) 
System.Array.Clear(gaComponentArray, 0, (10 + 1) * (20 + 1))

Another feature is the Clear method. So, if you clear element 2 for instance and you try referencing its length property afterward, you are going to raise a null reference exception.

Array.Clear(aiArrayIntegers, 0, 1) 


Reducing Array Size

This can only be used when you have a one dimensional array.

char[ ] myarray = new char[4]; 
myarray[0] = 'a';
myarray[1] = 'b';
myarray[2] = 'c';
myarray[3] = 'd';
System.Array.Resize(ref myarray, 2);

string[] myArray; 
System.Array.Resize(ref myArray, myArray.Length + 2);


Increasing Array Size

This can only be used when you have a one dimensional array.

char[ ] myarray = new char[2]; 
myarray[0] = 'a';
myarray[1] = 'b';
System.Array.Resize<char>(ref myarray, 4);
myarray[2] = 'c';
myarray[3] = 'd';


Resizing and Preserving

int[2] myarray; 
int[ ] temp = new int[4];
myarray[0] = 'a';
myarray[1] = 'b';
System.Array.Copy(myarray, temp, myarray.Length);
myarray = temp

Vectors

Arrays that have one dimension are called vectors
A single column matrix is called a column vector
A single row matrix is called a row vector



Copy method


System.Array.Copy(MyIntArray, MySecondIntArray, 2) 


CopyTo method

The CopyTo method is another one I really love. This can copy the elements of an array to any object implementing the IList interface. On catch here though, it's only supported in the 1.1 version of the framework.

MyArrayList = System.Array.CopyTo(MyIntArray, 0) 

SharedCopy method

The SharedCopy method does exactly what you think it will do, copy an array or pieces of an array to another array. Remember though, you may me copying reference types so approach it accordingly


Clone method

The Clone method is similar to copy, it makes a clone of the original array.
However, just like copy, you have to be careful when you use clone becuase it makes a Shallow Copy (Shallow Clone) of the objects if they are reference types. As such, if you manipulate the second array it will change the values of the first array and vice versa! Remember also that this has NOTHING to do with Passing ByVal or ByRef.


You can pass the cloned array ByVal, change its values, and both Array1 and Array2 will have their values changed!


MySecondIntArray = MyIntArray.Clone 

Comparing



Index method

starts searching at the beginning of the array

System.Array.Index 

If the value does not exist it returns -1.


LastIndexOf method

starts searching at the end of the array

System.Array.LastIndexOf 

If the value does not exist then it returns -1


BinarySearch method

This provides a faster search
Your array must be sorted first and therefore implement IComparable or a type that implements IComparer


int myArray[] = new int[] 

System.Array.BinarySearch(aiArray, 9)


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext