Lists & Object Initialization

This example isn’t a program the does anything special in particular. It’s more of my code showing my note taking process as I’m learning a particular action,  method, etc… I’ve gone back to studying list and dictionary collections. I’ll show you the various different things I’ve been trying over the next few posts. Pay attention to the comments in the code. You will only be able to download the source code for this. No Live example because I didn’t see the need. I’ve tried to explain what I was doing there so I don’t have to give any lengthy explanations here. However if you have any questions please post a comment.

Default.aspx HTML Code File Contents

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ObjectInitializers.Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="resultLabel" runat="server"></asp:Label>

Not much to see here or to interpret. Just a resultLabel.

Default.aspx Code File Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ObjectInitializers
    public partial class Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            // Examples of using a collection instead of an array.
            string result = "";

            // Creating the Car objects.
            // If there isn't a valid constructor in the Car class you can't create the objects unless
            // you use an object initializer below.
            Car carOne = new Car("BMW", "528i", 2010, "Black");
            Car carTwo = new Car("BMW", "745li", 2005, "Black");
            Car carThree = new Car("Ford", "Escape", 2008, "White");

            // Let's create a generic list of car to add the above cars into.
            List<Car> cars = new List<Car>();

            // Instead of adding the objects to the list like this.

            // Add them this way via an object initializer. Cleaner and more concise line of code.
            // Especially if there isn't a constructor created in the Car class.
            // Let's create a generic list of car to add the above cars into.
            List<Car> cars = new List<Car>();
            cars.Add(new Car { Make = "BMW", Model = "528i", Year = 2010, Color = "Black" });
            cars.Add(new Car { Make = "BMW", Model = "745li", Year = 2005, Color = "Black" });
            cars.Add(new Car { Make = "Ford", Model = "Escape", Year = 2008, Color = "White" });

            // And now you can get output the same results to the screen as if you were using a
            // class constructor.
            for (int i = 0; i < cars.Count; i++)
                result += cars.ElementAt(i).FormatDetailsForDisplay();

            resultLabel.Text = result;

    public class Car
        // Public properties
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Color { get; set; }

        // Constructors
        public Car()
            this.Make = "Undefined";
            this.Model = "Undefined";
            this.Year = 1980;
            this.Color = "Undefined";

        public Car(string make, string model, int year, string color)
            this.Make = make;
            this.Model = model;
            this.Year = year;
            this.Color = color;

        // Public methods.
        public string FormatDetailsForDisplay()
            return String.Format("Make: {0} - Model: {1} - Year: {2} - Color: {3}</br>",
                Make, Model, Year, Color);


This is it to the this post. Nothing real fancy except how to use and initialize a list collection on the car class. You will see that I’ve commented out a constructor. Sure you can create your objects this way. This was done to force myself to initialize the new list of car on line 41. You can see that I’m initializing the list and add items to it all in the same lines of code. Instead of a few lines above, the commented out code, where I’m initializing the list objects in one line set of code and then adding them to the list in a seperate step.

Take note of the different ways I’ve shown on how to interate thru the list. You can use the normal for loop or a foreach loop. I’ve grown fond of the foreach loop because it’s a cleaner way to interate thru things and pull out the information needed.

I’m also using a simple method FormatDetailsForDisplay() to return a string to display the results to the screen.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s