- C# Lists Introduction
- C# Lists String Repository
- C# Lists Book Repository
- C# Lists of Class Objects
- C# Lists of Class Objects 2
In a previous post we defined a C# Lists Introduction. In this post we want to define a list of class objects. Let’s create a class of our own called Customers. We want to create a list of these customer objects.
In this first example, we will just create a class called Customer and create an empty list. We will then iterate through the empty list and display it one the console. There won’t be anything to display but it prove that the code works without error.
public class Customer { public int Id { get; set; } public string Name { get; set; } } class Program { static void Main(string[] args) { List<Customer> myCustomers = new List<Customer>(); // empty list foreach (Customer cust in myCustomers) { Console.WriteLine(cust.Name); } } }
Next we can add some customers to our list of customers with the Add() method.
static void Main(string[] args) { List<Customer> myCustomers = new List<Customer>(); // empty list myCustomers.Add(new Customer() { Id = 1, Name = "Jack" }); myCustomers.Add(new Customer() { Id = 2, Name = "Jill" }); foreach (Customer cust in myCustomers) { Console.WriteLine(cust.Name); } }
We will write simple console program, called ListsOfObjects, to display a short list of customers. We will start with a class called Customers. We only need to provide a couple of fields to show how this works. We will have an ID and a Name field.
In the main program we have instantiated a customer object and initialized it with object initialization syntax. In the second line in the main program we have used var instead of Customer. This is type inference. In the third line we created a list of customers. We have created a list of our customer objects. Currently the list is empty and not yet initialized to anything. Next we have created two customers.
In the next line of code we use the Add() method to begin adding customers to the list. The rest of the program is just displaying to the console. We cab use foreach for the iteration of the list.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ListsOfObjects { class Program { public class Customer { // mix fields with a property just for demonstration public int Id = 0; public string Name = ""; public string Status { get; set; } } static void Main(string[] args) { Customer customer1 = new Customer { Id = 1, Name = "Joe Smith", Status = "Active" }; var customer2 = new Customer { Id = 1, Name = "Sally Smithers", Status = "Inactive" }; var customers = new List<Customer>(); customers.Add(customer1); customers.Add(customer2); Console.WriteLine("Customers: {0} and {1}", customer1.Name, customer2.Name); foreach (Customer cust in customers) { if (cust.Status == "Active") { Console.Write("Active: "); Console.WriteLine(cust.Name); } } Console.ReadKey(); } } }
Object Initialization Syntax
The next code sample cleans things up a bit. Here we are going to do something very similar to what we just did above, but we will focus on the object initialization syntax. The is a new project called ListOfObjectsTwo.
namespace ListOfObjectsTwo { class Program { public class Customer { // mix fields with a property just for demonstration public int Id = 0; public string Name = ""; public string Status { get; set; } } static void Main(string[] args) { var customers = new List<Customer> { // using object initialization syntax here new Customer { Name = "Jack Sprat", Status = "Active"}, new Customer { Name = "Sally Slick", Status = "Active"} }; foreach (Customer cust in customers) { Console.WriteLine(cust.Name); } Console.ReadKey(); } } }
A New Class that contains the List
The next step is to create a new class, call it Repository, that contains a list of Customers. That class will have a method so we can add customers to the list. The list will be static. That is in the next post