วันพุธที่ 4 สิงหาคม พ.ศ. 2553

All about array Items on C#

/ Add an item to the end of an existing array
string[] ar1 = new string[] {"I", "Like", "To"}

// create a temporary array with an extra slot
// at the end
string[] ar2 = new string[ar1.Length + 1];

// add the contents of the ar1 to ar2
// at position 0
ar1.CopyTo(ar2, 0);

// add the desired value
ar2.SetValue("Code.", ar1.Length);

// overwrite ar1 with ar2 and voila!
// the contents of ar1 should now be {"I", "Like", "To", "Code."}
ar1 = ar2;

Or use List

List ls = new List();
ls.Add("Hello");

Or

Array.Resize(ref array, newsize);
array[newsize - 1] = "newvalue"

ไม่มีความคิดเห็น: