You can use the Add and AddRange methods of List to add an item to a list in C#. The List class provides functionality to add, remove, sort, find, and search items in the collection.
In this below code, learn how to add items to a List using C#.
C# List<T> class represents a collection of a type in C#. List.Add(), List.AddRange(), List.Insert(), and List.InsertRange() methods are used to add and insert items to a List<T>. List<T> is a generic class.
You must import the following namespace before using the List class.
using System.Collections.Generic;
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);
Next > List Tutorial in C#