Basic Introduction To List In C#

List in C# allows you to manage a collection of items. This is a basic introduction to List and how to add items to it using C# language.

Introduction to List
 
In C#, C# List class provides methods and properties to create a list of objects (classes). 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. 
  1. using System.Collections.Generic;  
We also have the Author class that we will use in the later code examples. 
  1. public class Author  
  2. {  
  3.     // Auto-Initialized properties  
  4.     public string Name { getset; }  
  5.     public string Book { getset; }  
  6.     public double Price { getset; }  
  7.     public string AuthorDetails()  
  8.     {  
  9.         return string.Format("{0} is an author of {1}. Price: ${2}",  
  10.             Name, Book, Price);  
  11.     }  
  12. }  
The following code example creates three lists of numbers, strings, and objects. 
  1. // Create a List of int  
  2. List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};  
  3.   
  4. // Create a List of strings   
  5. List<string> names = new List<string>()  
  6.                     {"Mahesh Chand""Neel Beniwal""Chris Love" };  
  7.   
  8. // Create a List of objects  
  9. List<Author> authors = new List<Author>  
  10. {  
  11.     new Author { Name = "Mahesh Chand", Book = "Apress", Price = 49.95 },  
  12.     new Author { Name = "Neel Beniwal", Book = "Apress", Price = 19.95 },  
  13.     new Author { Name = "Chris Love", Book = "PakT", Price = 29.95 }  
  14. };  
  15. // Read List objects  
The following code example adds more items to the lists. 
  1. // Add more items to List  
  2. numbers.Add(30);  
  3. numbers.Add(35);  
  4. numbers.Add(40);  
  5.   
  6. // Add more items  
  7. names.AddRange(new string[] {"Raj Beniwal""Allen O'neill" });  
The following code example reads the list items. 
  1. // Read List items  
  2. foreach (int num in numbers)  
  3. {  
  4.     Console.Write($"{num}, ");       
  5. }  
  6.   
  7. // Read List items  
  8. foreach (string name in names)  
  9. {  
  10.     Console.Write($"{name}, ");  
  11. }  
  12.   
  13. // Read List objects  
  14. foreach (Author author in authors)  
  15. {  
  16.     Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");  
  17. }  
Listing 1 Is the complete code example.
  1. // Create a List of int  
  2. List<int> numbers = new List<int>(5) { 5, 10, 15, 20, 25};  
  3. // Add more items to List  
  4. numbers.Add(30);  
  5. numbers.Add(35);  
  6. numbers.Add(40);  
  7. // Read List items  
  8. foreach (int num in numbers)  
  9. {  
  10.     Console.Write($"{num}, ");       
  11. }  
  12.   
  13. Console.WriteLine();  
  14.   
  15. // Create a List of strings   
  16. List<string> names = new List<string>()  
  17.                     {"Mahesh Chand""Neel Beniwal""Chris Love" };  
  18. // Add more items  
  19. names.AddRange(new string[] {"Raj Beniwal""Allen O'neill" });  
  20. // Read List items  
  21. foreach (string name in names)  
  22. {  
  23.     Console.Write($"{name}, ");  
  24. }  
  25.   
  26. Console.WriteLine();  
  27.   
  28. // Create a List of objects  
  29. List<Author> authors = new List<Author>  
  30. {  
  31.     new Author { Name = "Mahesh Chand", Book = "Apress", Price = 49.95 },  
  32.     new Author { Name = "Neel Beniwal", Book = "Apress", Price = 19.95 },  
  33.     new Author { Name = "Chris Love", Book = "PakT", Price = 29.95 }  
  34. };  
  35. // Read List objects  
  36. foreach (Author author in authors)  
  37. {  
  38.     Console.WriteLine($"Author: {author.Name}:{author.Book}:{author.Price}");  
  39. }  
Listing 1.
 
Read this complete Tutorial on C# List to learn more about List in C#.