Like others have already mentioned, arrays have a fix size. Once created the size can’t be “changed”. However it’s possible to “resize” an array, but it actually creates a new array and copies all values over.
public static void RemoveAt<T>(ref T[] arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
// moving elements downwards, to fill the gap at [index]
arr[a] = arr[a + 1];
}
// finally, let's decrement Array's size by one
Array.Resize(ref arr, arr.Length - 1);
}
RemoveAt(ref colors, 2); // removes Color.white.
Using a generic List is recommended since it only creates a new array if necessary. So if you add / remove a lot elements a List will be better in speed and “garbage production”.
Most people will tell you to use Lists instead, however that’s a cop-out and doesnt really answer the question (but they’re probably right)
if you really need to remove an element from an array, and you can’t use lists or any other libraries, I’m pretty sure the only way is to create a new array and replace the old one with it.
below is an example of a function that you could use to do that
int[] removeAtIndex(int[] inputArray, int index){
int[] outputArray= new int[inputArray.length-1];
for(int i=0;i<index;i++){
outputArray_=inputArray*;*_
} int writeLoc=index; int readLoc=index+1; for(int writeLoc=index;writeLoc<outputArray.Length;writeLoc++){ outputArray[writeLoc]=inputArray[readLoc]; readloc++; } return outputArray; }