Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use RemoveAt in C# ArrayList?
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<string>();
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 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
Advertisements