- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 remove an item from a C# list by using an index?
To remove an item from a list in C# using index, use the RemoveAt() method.
Firstly, set the list −
List<string> list1 = new List<string>() { "Hanks", "Lawrence", "Beckham", "Cooper", };
Now remove the element at 2nd position i.e. index 1
list1.RemoveAt(1);
Let us see the complete example −
Example
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<string> list1 = new List<string>() { "Hanks", "Lawrence", "Beckham", "Cooper", }; Console.Write("Initial list..."); foreach (string list in list1) { Console.WriteLine(list); } Console.Write("Removing element from the list..."); list1.RemoveAt(1); foreach (string list in list1) { Console.WriteLine(list); } } }
Output
Initial list... Hanks Lawrence Beckham Cooper Removing element from the list... Hanks Beckham Cooper
Advertisements