C# | How to insert an element in an Array? Last Updated : 28 Aug, 2019 Comments Improve Suggest changes Like Article Like Report An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C#. Let's say we have an array and we want to insert an element at a specific position in this array. Here's how to do it. First get the element to be inserted, say x Then get the position at which this element is to be inserted, say pos Create a new array with the size one greater than the previous size Copy all the elements from previous array into the new array till the position pos Insert the element x at position pos Insert the rest of the elements from the previous array into the new array after the pos CSharp // C# program to insert an // element in an array using System; public class GFG { // Main Method static public void Main() { int n = 10; int[] arr = new int[n]; int i; // initial array of size 10 for (i = 0; i < n; i++) arr[i] = i + 1; // print the original array for (i = 0; i < n; i++) Console.Write(arr[i] + " "); Console.WriteLine(); // element to be inserted int x = 50; // position at which element // is to be inserted int pos = 5; // create a new array of size n+1 int[] newarr = new int[n + 1]; // insert the elements from the // old array into the new array // insert all elements till pos // then insert x at pos // then insert rest of the elements for (i = 0; i < n + 1; i++) { if (i < pos - 1) newarr[i] = arr[i]; else if (i == pos - 1) newarr[i] = x; else newarr[i] = arr[i - 1]; } // print the updated array for (i = 0; i < n + 1; i++) Console.Write(newarr[i] + " "); Console.WriteLine(); } } Output: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 50 5 6 7 8 9 10 Comment More infoAdvertise with us Next Article C# | How to insert an element in an Array? C code_r Follow Improve Article Tags : C# CSharp-Arrays Similar Reads C# | Insert an element into the ArrayList at the specified index ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayLis 3 min read C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe 8 min read How to Insert an Element into an Array of Structs at a Specific Position in C? In C, structs allow the users to create user-defined data types which can be used to store data of different types in a single unit. In many use cases, we might use an array of structs to store the structs in contiguous memory locations to access them sequentially. In this article, we will learn how 3 min read C Program to Insert an Element in an Array In this article, we will learn how to insert an element into an array in C.C arrays have a fixed size, so we cannot dynamically increase their memory. However, we can insert an element if the array already have enough memory space to accommodate the new elementsInsert Element at Specific PositionTo 3 min read One Dimensional Arrays in C In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D, 5 min read How to Add an Element to an Array of Structs in C? In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra 3 min read C Program For Insertion Sort Insertion sort is a simple sorting algorithm used to sort a collection of elements in a given order. It is less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort but it is simple to implement and is suitable to sort small data lists.In this article, we 4 min read C Arrays An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in 7 min read How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.Initializing a Dynamic Arrays 2 min read Array of Pointers to Strings in C In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings 2 min read Like