Reader Level:
Articles

How to Add Items to a C# List

By Mahesh Chand on Jul 06, 2012
How to add items to a List using C#.
  • 0
  • 0
  • 46054

Add Items 

The Add method adds an item to a List. The following code snippet creates a List and adds items to it by using the Add method. 

// Create a list
List<string> AuthorList = new List<string>();

// Add items using Add method 
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Praveen Kumar");
AuthorList.Add("Raj Kumar");
AuthorList.Add("Nipun Tomar");
AuthorList.Add("Dinesh Beniwal");

The AddRange method is used to add a collection of items. The following code snippet adds a collection of items to a List. 

// Add a range of items
string[] authors = { "Mike Gold", "Don Box",
                        "Sundar Lal", "Neel Beniwal" };
AuthorList.AddRange(authors);

Download Free book: Programming List with C#

Mahesh Chand

C# Corner founder, 10-times Microsoft MVP, technical author, and software architect. Mahesh authored his first book, A Programmer’s Guide to ADO.NET in C# with APress at the age of 25. C# Corner is a memb... Read more

COMMENT USING

Trending up