Add Object to An Object Array


This worked well with my custom Geo1[] Geo[2] objects:


1. // Add an item to the end of an existing array
2. string[] ar1 = new string[] {"I", "Like", "To"}
3.
4. // create a temporary array with an extra slot
5. // at the end
6. string[] ar2 = new string[ar1.Length + 1];
7.
8. // add the contents of the ar1 to ar2
9. // at position 0
10. ar1.CopyTo(ar2, 0);
11.
12. // add the desired value
13. ar2.SetValue("Code.", ar1.Length);
14.
15. // overwrite ar1 with ar2 and voila!
16. // the contents of ar1 should now be {"I", "Like", "To", "Code."}
17. ar1 = ar2;



 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Enter the above security code (required)

 Name (required)

 Email (will not be published) (required)

Your comment is 0 characters limited to 3000 characters.