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.
See this [SO answer][1]:
public static void RemoveAt(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".
[1]: http://stackoverflow.com/a/17841729/1607924
↧