Remove element from array in C#
To remove a specific element from an array in C# you can useWhere()
Lambda expression from Linq
namespace and filter values by non needed items.Remember to include required namespace:
using System.Linq;
Example
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int itemToRemove = 2;
int[] arr = { 1, 2, 3, 3};
arr = arr.Where(e => e != itemToRemove).ToArray();
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);
Console.WriteLine("Array size: " + arr.Count());
}
}
Output
1
3
3
Array size: 3
3
3
Array size: 3