ARTICLE

How to Add Items to a C# List

Posted by Mahesh Chand Articles | How do I July 06, 2012
How to add items to a List using C#.
Reader Level:

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#

COMMENT USING

Trending up