c# - Good Practice? Redefining Arrays to Change their Size -
i'm working in unity in c#, more general programming practice question.
datatype[] array = new datatype[x]; //further down datatype[] newarray = new datatype[array.length + 1]; (int i=0;i<array.length;i++){ newarray[i] = array[i]; }
is considered bad practice? more/less efficient using arraylist? i'm performing operation @ beginning of level , never again, worth working arraylists?
yes, bad practice. if expect array size change use list instead of array. list works array in terms of indexing. add new items list use add(). can call toarray() on list array it.
http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
if come across situation can't use list , need grow array, use array.resize() shown below.
int[] array = new int[5]; console.writeline(array.length); array.resize<int>(ref array, 20); console.writeline(array.length);
Comments
Post a Comment