C# List class in .NET provides methods and properties to create a list of objects. You can create a list of simple data types, such as a string or int, or you can create a list of objects, such as classes and structs. C# List class methods allow you to add, remove, and find items in a list object.
The following code snippet creates a list of int.
List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};
The following code snippet creates a list of strings.
// Create a List of strings
List<string> names = new List<string>() {"Mahesh Chand", "Neel Beniwal", "Chris Love" };
You can add items to a list during the initialization or using List.Add() and List.AddRange() methods.
The List is a generic class. You must import the following namespace before using the List class.
using System.Collections.Generic;
We also have the Author class that we will use in the later code examples.
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 example creates three lists of numbers, strings, and objects.
// Create a List of int
List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};
// Create a List of strings
List<string> names = new List<string>()
{"Mahesh Chand", "Neel Beniwal", "Chris Love" };
// 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 }
};
// Read List objects
The following code example adds more items to the lists.
// Add more items to List
numbers.Add(30);
numbers.Add(35);
numbers.Add(40);
// Add more items
names.AddRange(new string[] {"Raj Beniwal", "Allen O'neill" });
The following code example reads the list of items.
// Read List items
foreach (int num in numbers)
{
Console.Write($"{num}, ");
}
// Read List items
foreach (string name in names)
{
Console.Write($"{name}, ");
}
// Read List objects
foreach (Author author in authors)
{
Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");
}
Listing 1 Is the complete code example.
// 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);
// Read List items
foreach (int num in numbers)
{
Console.Write($"{num}, ");
}
Console.WriteLine();
// Create a List of strings
List<string> names = new List<string>()
{"Mahesh Chand", "Neel Beniwal", "Chris Love" };
// Add more items
names.AddRange(new string[] {"Raj Beniwal", "Allen O'neill" });
// Read List items
foreach (string name in names)
{
Console.Write($"{name}, ");
}
Console.WriteLine();
// 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 }
};
// Read List objects
foreach (Author author in authors)
{
Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");
}
Listing 1.
Read this complete Tutorial on C# List to learn more about List in C#.