Like in PHP and some other languages, is there a way to add a value to an array without specifying the index?
int[] aWhich = {};
aWhich[] = 1;
Thanks.
Not to an Array or any other type since the indexer operator must have at least one parameter (through it does not have to be an int).
You can add to the end of a List, though:
List<int> aWhich = new List<int>();
aWhich.Add(1);
First of all you have to specify the maximum number of values that the array can hold:
int[] MyArray = new int[14];
Here 14 is the maximum number of values that MyArray can hold.
int value = 0;
void MyFuntion(){
MyArray[value] = 1;
value++;
}
In this way you can add values without specifying index number it will automatically put the index.
You can't. Arrays in C# (and .NET) are immutable (in terms of their size, not necessarely their content) and you access their values by the index. What's you are looking for is a List, ArrayList or something that might suits your need better in the System.Collections or System.Collections.Generic namespaces.
Edit: this method does NOT work for adding a new array item.
myArray[myArray.Length] = newArrayItem;
.Length - # will work for overwriting array items.
List<int>?ArrayListoutside of old legacy apps. You should be usingList.