The RemoveAt() method in C# is used to remove an element in a list at a position you set.
Firstly, set elements in the list −
var subjects = new List();
subjects.Add("Physics");
subjects.Add("Chemistry");
subjects.Add("Biology");
subjects.Add("Science");To remove an element, set the index from where you want to eliminate the element. The following is to remove an element from the 3rd position −
subjects.RemoveAt(2);
Let us see the complete code −
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
var subjects = new List();
subjects.Add("Physics");
subjects.Add("Chemistry");
subjects.Add("Biology");
subjects.Add("Science");
Console.WriteLine("ELEMENTS:");
foreach (var sub in subjects) {
Console.WriteLine(sub);
}
// remove element at 3rd position
subjects.RemoveAt(2);
Console.WriteLine("After removing an element:");
foreach (var sub in subjects) {
Console.WriteLine(sub);
}
}
}Output
ELEMENTS: Physics Chemistry Biology Science After removing an element: Physics Chemistry Science