The List class in C# and .NET represents a strongly typed list of objects. List provides functionality to create a collection of objects, find list items, sort list, search list, and manipulate list items. In List, T is the type of objects. The code examples in this article demonstrates how to add items to a List using C#.
C# List 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.
Previous article: Basic Introduction To List In C#
List is a generic class. You must import the following namespace before using the List class.
using System.Collections.Generic;
We can add items to a list during the object initialization.
The following code snippet initialize a list of integers with 5 items.
// Create a List of int
List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};
The following code snippet initialize a list of strings with 3 items.
// Create a List of strings
List<string> names = new List<string>()
{"Mahesh Chand", "Neel Beniwal", "Chris Love" };
Now, let’s create a list of objects and add items during the initialization.
public class Author
{
// Auto-Initialized properties
public string Name { get; set; }
public string Book { get; set; }
public double Price { get; set; }
public string AuthorDetails()
{
return string.Format("{0} is an author of {1}. Price: ${2}",
Name, Book, Price);
}
}
The following code snippet initialize a list of objects (Author class) with 3 items.
// Create a List of objects
List<Author> authors = new List<Author>
{
new Author { Name = "Mahesh Chand", Book = "Apress", Price = 49.95 },
new Author { Name = "Neel Beniwal", Book = "Apress", Price = 19.95 },
new Author { Name = "Chris Love", Book = "PakT", Price = 29.95 }
};
List.Add() and List.AddRange() methods
List.Add() method adds an object to the end of the List<T>. List.AddRange() method adds a collection of objects to the end of the List<T>.
The following code example adds three int objects to the end of the List<int> using Add method. The code also adds a collection of int using List.AddRange() method.
// Create a List of int
List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};
// Add more items to List
numbers.Add(30);
numbers.Add(35);
numbers.Add(40);
// Add a Range
int[] prime = new int[] { 1, 3, 5, 7, 11, 13};
numbers.AddRange(prime);
// Read List items
foreach (int num in numbers)
{
Console.Write($"{num}, ");
}
The following code example adds three strings to the end of the List<string>. The code also adds a collection of string using List.AddRange() method.
// Create a List of strings
List<string> names = new List<string>()
{"Mahesh Chand", "Neel Beniwal", "Chris Love" };
// Add more items to the list
names.Add("Author One");
names.Add("Author Two");
names.Add("Author Three");
// Add more items
names.AddRange(new string[] {"Raj Beniwal", "Allen O'neill" });
// Read List items
foreach (string name in names)
{
Console.Write($"{name}, ");
}
The following code example adds three Author objects to the end of the List<Author>. The code also adds a collection of Author using List.AddRange() method.
// Create a List of objects
List<Author> authors = new List<Author>
{
new Author { Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95 },
new Author { Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95 },
new Author { Name = "Chris Love", Book = "Practical PWA", Price = 29.95 }
};
// Add more items to the list
authors.Add(new Author { Name = "Mahesh Chand", Book = "Graphics with GDI+", Price = 49.95 });
authors.Add(new Author { Name = "Mahesh Chand", Book = "Mastering C#", Price = 54.95 });
authors.Add(new Author { Name = "Mahesh Chand", Book = "Jumpstart Blockchain", Price = 44.95 });
// Create a new List of Author
List<Author> duplicateAuthors = new List<Author>
{
new Author { Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95 },
new Author { Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95 },
new Author { Name = "Chris Love", Book = "Practical PWA", Price = 29.95 }
};
// Add List using AddRange
authors.AddRange(duplicateAuthors);
// Read List objects
foreach (Author author in authors)
{
Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");
}
Robin WilsonPosted Mar 6, 2020, 10:28 AM
Thanks very helpful!
Rushi MehtaPosted Oct 7, 2018, 10:34 PM
Useful article share
kalu singh raoPosted Jul 26, 2016, 1:46 AM
Can we add int and string together in list ?