Add new elements to an array in C#
This post will discuss how to add new elements to an array in C#.
Since we can’t modify the array’s size once it is created in C#, there is no direct way to resize the fixed-size arrays to accommodate additional element(s).
If we need a resizable array-based implementation, the recommended solution is to use a List. As elements are added to the List, its capacity grows automatically. But if you insist on using arrays, you have to instantiate a new array to accommodate the additional element. We can do this with any of the following methods:
1. Using Array.CopyTo() method
We can also use the Array.CopyTo() method to allocate the larger array for accommodating the new element. The following code example shows how to resize the array to add a new element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; public static class Extensions { public static T[] Append<T>(this T[] array, T item) { if (array == null) { return new T[] { item }; } T[] result = new T[array.Length + 1]; array.CopyTo(result, 0); result[array.Length] = item; return result; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4 }; int item = 5; int[] result = array.Append(item); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
The following implementation uses a custom routine in place of the Array.CopyTo() method for copying all the elements of the current array instance to the new array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; public static class Extensions { public static T[] Append<T>(this T[] array, T item) { if (array == null) { return new T[] { item }; } T[] result = new T[array.Length + 1]; for (int i = 0; i < array.Length; i++) { result[i] = array[i]; } result[array.Length] = item; return result; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4 }; int item = 5; int[] result = array.Append(item); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
2. Using Enumerable.Concat() method
We can use Linq’s Concat() method to concatenate elements of the two input sequences. The following code example demonstrates the usage of the Concat() method for appending an element to an array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Linq; public static class Extensions { public static T[] Append<T>(this T[] array, T item) { if (array == null) { return new T[] { item }; } return array.Concat(new T[] { item }).ToArray(); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4 }; int item = 5; int[] result = array.Append(item); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
3. Using Array.Resize() method
The recommended solution to resize a one-dimensional array is by using the Array.Resize() method. This method allocates a new array with the specified size, copies elements from the old array to the new one, and replaces the old array with the new one.
The following example demonstrates the resizing operation with runs in O(n) time, where n is the size of the new array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; public static class Extensions { public static T[] Append<T>(this T[] array, T item) { if (array == null) { return new T[] { item }; } Array.Resize(ref array, array.Length + 1); array[array.Length - 1] = item; return array; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4 }; int item = 5; int[] result = array.Append(item); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
4. Convert to List
The idea is first to convert the array into a List, then append the specified element at the end of this list, and cast the result back to an array. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections.Generic; public static class Extensions { public static T[] Append<T>(this T[] array, T item) { List<T> list = new List<T>(array); list.Add(item); return list.ToArray(); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4 }; int item = 5; int[] result = array.Append(item); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
We can do this in a single step by combining the list constructor with the initializer syntax.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Collections.Generic; public static class Extensions { public static T[] Append<T>(this T[] array, T item) { return new List<T>(array) { item }.ToArray(); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4 }; int item = 5; int[] result = array.Append(item); Console.WriteLine(String.Join(",", result)); } } /* Output: 1,2,3,4,5 */ |
That’s all about adding new elements to an array in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)