How To Create A List Of Objects In C#

Introduction

 
A list is a collection of items that can be accessed by index and provides functionality to search, sort and manipulate list items.
 
The List<T> class defined in the System.Collections.Generic namespace is a generic class and can store any data types to create a list. Before you use the List class in your code, you must import the System.Collections.Generic namespace using the following line. 
  1. using System.Collections.Generic;

Creating a List of Objects

 
The List class can be used to create any type including a class. In this article, we will see how to create a list of a class with several properties.
 
We have a class named Author that has five pubic properties Name, Age, BookTitle, IsMVP and PublishedDate of type string, short, string, bool, and DateTime respectively.
  1. public class Author
  2. {
  3.     private string name;
  4.     private short age;
  5.     private string title;
  6.     private bool mvp;
  7.     private DateTime pubdate;
  8.   
  9.     public Author(string name, short age, string title, bool mvp, DateTime pubdate)
  10.     {
  11.         this.name = name;
  12.         this.age = age;
  13.         this.title = title;
  14.         this.mvp = mvp;
  15.         this.pubdate = pubdate;
  16.     }
  17.   
  18.     public string Name
  19.     {
  20.         get { return name;  }
  21.         set { name = value; }
  22.     }
  23.   
  24.     public short Age
  25.     {
  26.         get { return age; }
  27.         set { age = value; }
  28.     }
  29.     public string BookTitle
  30.     {
  31.         get { return title; }
  32.         set { title = value; }
  33.     }
  34.     public bool IsMVP
  35.     {
  36.         get { return mvp; }
  37.         set { mvp = value; }
  38.     }
  39.     public DateTime PublishedDate
  40.     {
  41.         get { return pubdate; }
  42.         set { pubdate = value; }
  43.     }
  44. }
The following code snippet creates a List of Author type.
  1. List<Author> AuthorList = new List<Author>();
The following code snippet creates the Author objects and adds them to the List.
  1. AuthorList.Add(new Author("Mahesh Chand", 35, "A Prorammer's Guide to ADO.NET"truenew DateTime(2003,7,10)));
  2. AuthorList.Add(new Author("Neel Beniwal", 18, "Graphics Development with C#"falsenew DateTime(2010, 2, 22)));
  3. AuthorList.Add(new Author("Praveen Kumar", 28, "Mastering WCF"truenew DateTime(2012, 01, 01)));
  4. AuthorList.Add(new Author("Mahesh Chand", 35, "Graphics Programming with GDI+"truenew DateTime(2008, 01, 20)));
  5. AuthorList.Add(new Author("Raj Kumar", 30, "Building Creative Systems"falsenew DateTime(2011, 6, 3)));
The following code snippets loop through the List and prints out all the items of the List.
  1. foreach (var author in AuthorList)
  2. {
  3.     Console.WriteLine("Author: {0},{1},{2},{3},{4}", author.Name, author.Age, author.BookTitle, author.IsMVP, author.PublishedDate);
  4. }
Download Free book: Programming List with C#
 
Here is a detailed article: Working with C# List