Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to delete/remove an element from a C# array?
In C#, arrays have a fixed size once created, so you cannot truly delete an element. However, you can remove an element by shifting the remaining elements to fill the gap, or use collections like List<T> for dynamic removal.
Understanding Array Element Removal
When removing an element from an array, the process involves shifting all elements after the target position one position to the left, effectively overwriting the element to be removed.
Using Array Shifting Method
Example
using System;
class Program {
static void Main() {
int[] arr = new int[5] {35, 50, 55, 77, 98};
int pos = 2; // Position to delete (0-based index)
Console.WriteLine("Elements before deletion:");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine($"Element[{i}]: {arr[i]}");
}
// Shift elements to the left
for (int i = pos; i < arr.Length - 1; i++) {
arr[i] = arr[i + 1];
}
Console.WriteLine("\nElements after deletion:");
for (int i = 0; i < arr.Length - 1; i++) {
Console.WriteLine($"Element[{i}]: {arr[i]}");
}
}
}
The output of the above code is −
Elements before deletion: Element[0]: 35 Element[1]: 50 Element[2]: 55 Element[3]: 77 Element[4]: 98 Elements after deletion: Element[0]: 35 Element[1]: 50 Element[2]: 77 Element[3]: 98
Using List<T> for Dynamic Removal
For more flexible element removal, use List<T> which provides built-in methods like RemoveAt() and Remove() −
Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> {35, 50, 55, 77, 98};
Console.WriteLine("Original List:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
// Remove by index
numbers.RemoveAt(2);
Console.WriteLine("<br>\nAfter RemoveAt(2):");
foreach (int num in numbers) {
Console.Write(num + " ");
}
// Remove by value
numbers.Remove(77);
Console.WriteLine("<br>\nAfter Remove(77):");
foreach (int num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Original List: 35 50 55 77 98 After RemoveAt(2): 35 50 77 98 After Remove(77): 35 50 98
Using LINQ Where Method
You can create a new array without specific elements using LINQ's Where() method −
Example
using System;
using System.Linq;
class Program {
static void Main() {
int[] arr = {35, 50, 55, 77, 98};
int indexToRemove = 2;
Console.WriteLine("Original array:");
Console.WriteLine(string.Join(", ", arr));
// Create new array without the element at specified index
int[] newArr = arr.Where((value, index) => index != indexToRemove).ToArray();
Console.WriteLine($"\nArray after removing index {indexToRemove}:");
Console.WriteLine(string.Join(", ", newArr));
}
}
The output of the above code is −
Original array: 35, 50, 55, 77, 98 Array after removing index 2: 35, 50, 77, 98
Comparison of Methods
| Method | Pros | Cons |
|---|---|---|
| Array Shifting | Uses existing array, memory efficient | Manual implementation, array size unchanged |
| List<T> | Built-in methods, dynamic sizing | Slightly more memory overhead |
| LINQ Where | Functional approach, readable code | Creates new array, less efficient for large arrays |
Conclusion
Arrays in C# have fixed sizes, so element removal requires shifting elements or using collections like List<T>. For frequent additions and removals, prefer List<T> over arrays for better performance and convenience.
