We already know that arrays in C# have a fixed size. That is, we can’t change the size of an array once it’s been generated, and there’s no easy method to add or delete elements from it. The preferred method for creating a dynamic array is to use a List<T>. If you must use arrays, establish a new array to delete elements from the old one.
To simply delete the last member from an array, we can use any of the following methods:
1. Using Enumerable. SkipLast() method
System.Linq.Enumerable.SkipLast() method returns a new collection having elements from the source collection with specified elements from the end of the collection omitted. The following code example demonstrates how to use SkipLast to skip the last element from the sequence.
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; array = array.SkipLast(1).ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,2,3,4 */
2. Using Enumerable.Take() method
System.Linq.Enumerable.Take() method returns a specified number of contiguous elements from the start of a sequence. The following code example demonstrates how to use the Take to return all elements from the array except the last one.
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; array = array.Take(array.Length - 1).ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,2,3,4 */
3. Convert to List<T>
The idea is first to convert the array into a List<T>, then use its RemoveAt() method, which removes the element present at the specified position in the list. To remove the last element, we need to pass the index of the last element. This is demonstrated below:
using System; using System.Collections.Generic; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; List<int> list = new List<int>(array); list.RemoveAt(array.Length - 1); array = list.ToArray(); Console.WriteLine(String.Join(",", list)); } } /* Output: 1,2,3,4 */
4. Using Enumerable.Where() method
Enumerable.Where() method in System. Linq the namespace filters a sequence of values based on a predicate. The following code example demonstrates how we can use the Where method to filter the last element from an array and return the remaining elements.
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; array = array.Where((item, index) => index != array.Length - 1).ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,2,3,4 */
5. Using Array.Resize() method
We can resize the array to a smaller size to contain one less element using the Array.Resize() method. This is demonstrated below:
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; Array.Resize(ref array, array.Length - 1); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,2,3,4 */
It should be noted that the Resize method does not change the original array’s fixed-size nature; rather, it generates a new array with the requested size.
6. Using Enumerable. Skip with Enumerable. Reverse
Reverse() method is used to invert the order of the elements in a sequence and Skip() method bypasses the specified number of items in a sequence and then returns the remaining elements.
The following code example demonstrates how the combination of Skip and Reverse can remove the last element from the array. This approach has low performance and is not recommended.
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; array = array.Reverse().Skip(1).Reverse().ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,2,3,4 */
That’s all about removing the last element from an array in C#.