empty Arrays >>>array.clear [Go to MSDN View]
1 string[] str =New string[2];2 for(inti =0; I < Str. Length; i++)3Str[i] =i.tostring ();4 array.clear (str, 0, str.) Length); 5 for(inti =0; I < Str. Length; i++)6Console.WriteLine (string. IsNullOrEmpty (Str[i])?"NULL": Str[i]);7 //Output Result:8 //NULL9 //NULL
Initialize Arrays >>>Initialize [Go to MSDN View]
Just for initialization, if the array subkey has been initialized, it does not change its value. (Remove the code below x[1] = 6; try)
1 int[] x =New int[2];2 int[] y =New int[2];3 x[0] = 5; x[1] = 6; 4 x.initialize ();5 y.initialize ();6 for(inti =0; i < x.length; i++)7Console.WriteLine ("x:{0} y:{1}", X[i], y[i]);8 //Output Result:9 //X:5 y:0Ten //X:6 y:0
dynamically modify array size >>>array.resize [go to MSDN View]
The process is to create a new array first and then assign the old array to the past.
1 int[] x =New int[2];2x[0] =5; x[1] =6;3 array.resize (ref X, X.length + 2); 4 for(inti =0; i < x.length; i++)5Console.WriteLine ("X[{0}]={1}", I, X[i]);6 //Output Result:7 //x[0]=58 //x[1]=69 //x[2]=0Ten //x[3]=0
If the array is large and the performance impact is obvious, the list<> AddRange method is recommended. [Go to MSDN View]
1list<int> LST =Newlist<int>();2Lst. ADD (3); Lst. ADD (4);3 int[] x =New int[2];4x[0] =5; x[1] =6;5 lst. AddRange (x);6 for(inti =0; I < LST. Count; i++)7Console.WriteLine ("Lst[{0}]={1}", I, Lst[i]);8 //Output Result:9 //lst[0]=3Ten //lst[1]=4 One //lst[2]=5 A //lst[3]=6
For more information on array copying, flashbacks, sorting, etc., see MSDN
C # emptying arrays & initializing arrays & dynamic arrays