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
1 2 3 4 5 6 7 8 9 10 1 2 3 4 50 5 6 7 8 9 10
- C# | Insert an element into Collection<T> at specified index
- C# | Insert an element into the ArrayList at the specified index
- C# | Performing Specified action on each element of Array
- C# | Finding the index of first element in the array
- C# | Finding the index of last element in the array
- C# | Insert() Method
- C# | Insert at the specified index in StringCollection
- C# | Insert a new entry in OrderedDictionary with specified key and value
- C# | Insert into OrderedDictionary with key and value at specified index
- C# | How to insert the elements of a collection into the List at the specified index
- C# | Insert an object at the top of the Stack - Push Operation
- Array.BinarySearch(Array, Object) Method with examples in C#
- C# | Array.BinarySearch(Array, Object, IComparer) Method
- How to get First Element of the ValueTuple in C#?
- How to get third Element of the ValueTuple in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.