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 allows 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.
-
- 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.
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
- {
-
- 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.
-
- List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};
-
-
- List<string> names = new List<string>()
- {"Mahesh Chand", "Neel Beniwal", "Chris Love" };
-
-
- 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 }
- };
-
The following code example adds more items to the lists.
-
- numbers.Add(30);
- numbers.Add(35);
- numbers.Add(40);
-
-
- names.AddRange(new string[] {"Raj Beniwal", "Allen O'neill" });
The following code example reads the list items.
-
- foreach (int num in numbers)
- {
- Console.Write($"{num}, ");
- }
-
-
- foreach (string name in names)
- {
- Console.Write($"{name}, ");
- }
-
-
- foreach (Author author in authors)
- {
- Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");
- }
Listing 1 Is the complete code example.
-
- List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};
-
- numbers.Add(30);
- numbers.Add(35);
- numbers.Add(40);
-
- foreach (int num in numbers)
- {
- Console.Write($"{num}, ");
- }
-
- Console.WriteLine();
-
-
- List<string> names = new List<string>()
- {"Mahesh Chand", "Neel Beniwal", "Chris Love" };
-
- names.AddRange(new string[] {"Raj Beniwal", "Allen O'neill" });
-
- foreach (string name in names)
- {
- Console.Write($"{name}, ");
- }
-
- Console.WriteLine();
-
-
- 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 }
- };
-
- foreach (Author author in authors)
- {
- Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");
- }
Listing 1.