- Trending Categories
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
What are Add, Remove methods in C# lists?
The List<T> is a collection in C# and is a generic collection. The add and remove methods are used in C# lists for adding and removing elements.
Let us see how to use Add() method in C#.
Example
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"); foreach (string s in sports) { Console.WriteLine(s); } } }
Output
Football Tennis Soccer
Let us see how to use Remove() method in C#.
Example
using System; using System.Collections.Generic; class Program { static void Main() { List<string> sports = new List<string>(); sports.Add("Football"); // add method sports.Add("Tennis"); sports.Add("Soccer"); Console.WriteLine("Old List..."); foreach (string s in sports) { Console.WriteLine(s); } Console.WriteLine("New List..."); sports.Remove("Tennis"); // remove method foreach (string s in sports) { Console.WriteLine(s); } } }
Output
Old List... Football Tennis Soccer New List... Football Soccer
Advertisements