Add Item to Array

There is no Add method for basic arrays.
//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 the contents of ar1 should now be {“I”, “Like”, “To”, “Code.”}
ar1 = ar2;

Array.Resize(ref ar1, ar1.Length + 1);
Array.Copy(new object[] { “Code.” }, 0, ar1, ar1.Length – 1, 1);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s