Menu
Coddy logo textTech

Object Initializers

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 8 of 70.

Object initializers provide a convenient way to set property values when creating an object, without calling a constructor with many parameters.

Traditional way using constructor

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string City { get; set; }
}

Person person = new Person();
person.Name = "Alice";
person.Age = 25;
person.City = "New York";

Using object initializer syntax

Person person = new Person
{
    Name = "Alice",
    Age = 25,
    City = "New York"
};

Object initializers with constructors

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string City { get; set; }
    
    public Person(string name)
    {
        Name = name;
    }
}

// Constructor sets Name, initializer sets other properties
Person person = new Person("Bob")
{
    Age = 30,
    City = "London"
};

Nested object initializers

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
}

public class Person
{
    public string Name { get; set; }
    public Address Address { get; set; }
}

Person person = new Person
{
    Name = "Charlie",
    Address = new Address
    {
        Street = "123 Main St",
        City = "Boston"
    }
};

Creating multiple objects with initializers

Person person1 = new Person { Name = "Alice", Age = 25 };
Person person2 = new Person { Name = "Bob", Age = 30 };
Person person3 = new Person { Name = "Charlie", Age = 35 };

Object initializers make code cleaner and more readable. The properties are set after the constructor runs. You can use initializers with or without constructor parameters, making object creation flexible and concise.

challenge icon

Challenge

Medium

Create three Student objects using different object initializer approaches:

  • student1: Use default constructor with object initializer to set all properties (Name, StudentId, Major, GPA) from input
  • student2: Use object initializer to set only Name = "Bob" and Major = "Engineering"
  • student3: Use constructor with "Charlie" as parameter, then use object initializer to set StudentId = 54321, Major = "Mathematics", GPA = 3.5

Cheat sheet

Object initializers provide a convenient way to set property values when creating an object:

Person person = new Person
{
    Name = "Alice",
    Age = 25,
    City = "New York"
};

Object initializers can be combined with constructors:

Person person = new Person("Bob")
{
    Age = 30,
    City = "London"
};

Nested object initializers allow setting properties of nested objects:

Person person = new Person
{
    Name = "Charlie",
    Address = new Address
    {
        Street = "123 Main St",
        City = "Boston"
    }
};

Multiple objects can be created concisely:

Person person1 = new Person { Name = "Alice", Age = 25 };
Person person2 = new Person { Name = "Bob", Age = 30 };

Try it yourself

using System;

class Program
{
    static void Main()
    {
        string name = Console.ReadLine();
        int studentId = int.Parse(Console.ReadLine());
        string major = Console.ReadLine();
        double gpa = double.Parse(Console.ReadLine());
        
        // TODO: Create student1 using object initializer with all properties
        // Use the default constructor and set Name, StudentId, Major, and GPA
        
        // TODO: Create student2 using object initializer with only Name and Major
        // Name should be "Bob", Major should be "Engineering"
        
        // TODO: Create student3 using the constructor with name parameter "Charlie"
        // Then use object initializer to set StudentId = 54321, Major = "Mathematics", GPA = 3.5
        
        Console.WriteLine($"Student 1: {student1.Name} (ID: {student1.StudentId}) - {student1.Major}, GPA: {student1.GPA}");
        Console.WriteLine($"Student 2: {student2.Name} (ID: {student2.StudentId}) - {student2.Major}, GPA: {student2.GPA}");
        Console.WriteLine($"Student 3: {student3.Name} (ID: {student3.StudentId}) - {student3.Major}, GPA: {student3.GPA}");
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming