c sharp

C# Add to Array

When storing a collection that has a similar data type in a variable, arrays are employed. That makes the data storage of similar elements (having similar data types) easy and more feasible. Arrays store the list of items at the adjoining memory location one after another. Occasionally we are required to merge arrays to produce a resultant array. We have different methods through which we can join different arrays, but the condition is that the arrays should be of the same data type. And if we want to add more items to an array, we can also do so. C# has many different techniques that allow the uniting of arrays.

Methods

There are multiple approaches for joining arrays or adding to an array in C#. We’ll go through some of them with syntax and practical examples in the following:

We will explain all of these one by one.

Concat()

Concatenation means joining two arrays of the same data types and then getting a resultant array that merges both arrays. Concatenation itself means it concatenates (joins) things. Concatenation is used to concatenate the message with the variable, i.e., Consloe.WriteLine(”Hello”+ var_1) here (+) is used to join the message with the value stored in the var_1. While working with arrays, we concatenate as Array_1.Concat(Array_2).ToArray(); this joins two arrays and returns a resultant array.

Syntax

Array_1.Concat(Array_2)

The syntax is simple, we have two arrays, and we will concatenate the first array, “Array_1”, with the second array, “Array_2”. The first array’s components will appear in the output array first, followed by the second array’s components. If we want to display the element of the second array first, then simply switch the place of arrays like Array_2.Concat(Array_1). Arrays_1 and Array_2 are two arrays with elements of the same data type. “Concat” will join both arrays.

Example

Now, execute the program for this technique.

In this program, we define the three arrays. The first array will be of integer type and have three elements. The second array will also be of integer type, and it will contain four integer elements in it. The third array will be of integer type, too, because this array will store the result after concatenation, and we will display the result with this array variable. Then the Concat() function is used and it will join both arrays. The elements of the first array will be displayed first because that is the array that we want to concatenate the other with. Concat() will join the arrays, and the ToArray() method will convert them into an array after concatenation. Save the final array in the “result” variable. To display the concatenated array, we will use String.join() method. Here, we will pass two arguments, first will be in double quotes, that is, the separator. The “result” variable will be the second parameter. This method will display the array as we want. The separator used here is space “ ”. Place this function along with its attributes inside the Console.WriteLine(). By this, we can see the output on the console. Console.ReadKey() function will make the console wait for the next action.

Resize()

We can declare an array without specifying its size. But when the array having a fixed size is declared and initialized, we cannot change its elements. To change the size of an array, we have a method of Resize(), which will resize the array, and we will be able to add elements to an existing array.

Syntax

Array.Resize(ref Array_0, size)

Resize() will change the size (elements) of the array. It has two parameters. One is “ref”, which shows the reference of the array whose size we want to change. After the “ref” keyword, writes the variable name “Array_0” where we stored the array. The other one is “size”, the size we want to set for the array.

Example

Let’s write code to utilize this approach.

After declaring the Main() function, create an array of an integer type. It contains three elements in it. Now, use the Array.Resize() function to modify the elements of the required array. Within this method, two attributes are given. First is the “ref” (reference) of the array whose size we want to change. The second argument shows the size of the new array. Now, add two elements in the array using indexes because we have changed the size of the array. It can have five elements now. At index 0, the element “8” is already present. This will remove the “8” from the “0” index and place the new value, “1”. At index “3”, there is no element stored, so the newly assigned value “76” will be placed there. But we haven’t stored any value at the 5th index. If we don’t assign any value, the compiler places 0 on that index. Use the join() method to display the array in a readable format. Here, we set the separator as space. And represent the updated array with Console.WriteLine() method.

Append()

The append() is another technique for adding new components to the array. This methodology is very useful when dealing with file handling, but we can also utilize the append() function to add items to arrays.

Syntax

Arr_1.Append(“ element ”)

The append method takes the array “Arr_1” where we want to append the new item. Inside the parenthesis, we write the “element” that we intend to include in the array.

Example

Now, implement the program related to this function.


Inside the Main() function, define a string-type array and store two strings in it. Then call the append() method. The compiler will append the specified message with the text present inside the “arr”. Then store the result of append in the string type variable. The append() function will add the string at the end of the required array. And after appending, it will convert the result to an array using the ToArray() method. Then we will display the appended array by calling the String.join() function inside the Console.WriteLine().

CopyTo()

The CopyTo() function copies the elements of one array and adds those values to the other array. The one benefit of using this technique is that we can add the elements to the index of our desire, and it will overwrite the existing elements at that index.

Syntax

Arr_1.CopyTo(Arr_2, index)

The CopyTo() will pass two parameters, which include “Arr_2” (the destination array) and “index” where we want to add the elements of the array. “Arr_1” is the array that we will copy to “Arr_2” at the specified index.

Example

Let’s run code for the use of this method.

We have to declare two integer-type arrays and store a few elements. Then copy one array to the other with CopyTo() method. This method has two arguments, “arr_1” shows the array in which we want to add the copied elements, and the other is “0”, the starting index to place the elements of a copied array. The “arr_0” is the array that we want we copy. Then join these arrays by utilizing the join() method. We invoke the Console.WriteLine() function to print the “arr_1” where we add the elements.

Conclusion

This guide is about the methods of how we can add to arrays, whether it is an element or another array. Each technique is completely described, along with its syntax and codes. It depends on the user which methodology he wants to use. According to the program’s specifications, the method is selected. What C# users need to understand about adding to an array is covered in this article. Concate() is the function most frequently used in C# when adding to an array. The append() method adds items to an array and files.

About the author

Ann-ul Hayyat

I'm a professional SEO and technical content writer with extensive experience in these fields as well as writing content for blogs, websites, reports, and other topics related to education, science, sports, and many other areas. I am skilled and experienced in creating SEO-friendly blog posts, technical articles, and other content on themes that you may find interesting.