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
What are Add, Remove methods in C# lists?
The List<T> is a generic collection in C# that provides dynamic array functionality. The Add() and Remove() methods are fundamental operations for managing elements in a list, allowing you to add new items and remove existing ones efficiently.
Syntax
Following is the syntax for adding elements to a list −
List<T> listName = new List<T>(); listName.Add(item);
Following is the syntax for removing elements from a list −
bool result = listName.Remove(item); listName.RemoveAt(index);
Using Add() Method
The Add() method appends an element to the end of the list. It increases the list size by one and returns void −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> sports = new List<string>();
sports.Add("Football");
sports.Add("Tennis");
sports.Add("Soccer");
Console.WriteLine("Sports List:");
foreach (string s in sports) {
Console.WriteLine(s);
}
Console.WriteLine("Total items: " + sports.Count);
}
}
The output of the above code is −
Sports List: Football Tennis Soccer Total items: 3
Using Remove() Method
The Remove() method removes the first occurrence of a specific object from the list. It returns true if the item was successfully removed, or false if the item was not found −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> sports = new List<string>();
sports.Add("Football");
sports.Add("Tennis");
sports.Add("Soccer");
Console.WriteLine("Original List:");
foreach (string s in sports) {
Console.WriteLine(s);
}
bool removed = sports.Remove("Tennis");
Console.WriteLine("Remove 'Tennis': " + removed);
Console.WriteLine("\nUpdated List:");
foreach (string s in sports) {
Console.WriteLine(s);
}
}
}
The output of the above code is −
Original List: Football Tennis Soccer Remove 'Tennis': True Updated List: Football Soccer
Using RemoveAt() Method
The RemoveAt() method removes the element at a specific index. This is useful when you know the position of the element to remove −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> {10, 20, 30, 40, 50};
Console.WriteLine("Original List:");
for (int i = 0; i < numbers.Count; i++) {
Console.WriteLine($"Index {i}: {numbers[i]}");
}
numbers.RemoveAt(2); // Remove element at index 2 (value 30)
Console.WriteLine("\nAfter RemoveAt(2):");
for (int i = 0; i < numbers.Count; i++) {
Console.WriteLine($"Index {i}: {numbers[i]}");
}
}
}
The output of the above code is −
Original List: Index 0: 10 Index 1: 20 Index 2: 30 Index 3: 40 Index 4: 50 After RemoveAt(2): Index 0: 10 Index 1: 20 Index 2: 40 Index 3: 50
Comparison of List Methods
| Method | Purpose | Return Type | Parameters |
|---|---|---|---|
| Add() | Adds element to end of list | void | Item to add |
| Remove() | Removes first occurrence of item | bool | Item to remove |
| RemoveAt() | Removes element at specific index | void | Index position |
Conclusion
The Add() and Remove() methods are essential for managing List<T> collections in C#. Add() appends elements to the end, while Remove() finds and removes the first matching element. Use RemoveAt() when you need to remove an element at a specific index position.
