Table of contents >> Objects > Object initializers
Low importance article, or discussion

In the lesson about instantiation we learned that we can create classes, and afterwards make copies of them, called instances, and we can assign values to the properties of these instances:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
 
namespace HelloWorld
{
    class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Person somePerson = new Person();
            Person otherPerson = new Person();
            
            somePerson.Name = "Doe";
            somePerson.Surname = "John";
            somePerson.Age = 30;
            
            otherPerson.Name = "Doe";
            otherPerson.Surname = "Jane";
            otherPerson.Age = 25;
            
            Console.ReadLine();
        }
    }
}

Using object initializers, which were introduced in C# 3.0, we can do the same thing, but without having to call a constructor, followed by lines and lines of assignment. There is no practical benefit to it, other than making the code easier to read and understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
 
namespace HelloWorld
{
    class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Person somePerson = new Person() { Name = "Doe", Surname = "John", Age = 30 };
            Person otherPerson = new Person() { Name = "Doe", Surname = "Jane", Age = 25 };
            
            Console.ReadLine();
        }
    }
}

It is clear that the declaration and initialization of the two Person instances is much more concise now.

Only accessible properties and variables of an object can be initialized this way, and it is only logical - we couldn't assign values to them in the "traditional" way of declaration either, if they are not public, internal, etc.

If a class defines only constructors with mandatory parameters (no default constructor), you still have to provide values for those parameters when instantiating the object with the object initializer syntax.

Without realizing it, you have already seen this syntax used before, when discussing initialization of arrays, which can have their elements specified during declaration using the object initializer syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myArray = { 1, 2, 3, 4, 5, 6 };
            
            Console.ReadLine();
        }
    }
}

Lastly, if we have a data collection of a more complex type, for instance, an array of type Person, we can initialize using this kind of syntax not only its elements, but also the members of those elements (in other words, nested initialization):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
 
namespace HelloWorld
{
    class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public int Age { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Person[] myArray =
            {
                new Person() { Name = "Doe", Surname = "John", Age = 30 },
                new Person() { Name = "Doe", Surname = "Jane", Age = 25 }
            };
            
            Console.ReadLine();
        }
    }
}

One thing to keep in mind is the fact the object initializer syntax, which assigns values to properties and variables of an object, is being ran after the constructor of that type is called during the instantiation, and finishes its execution. This means that read-only members cannot be assigned using this syntax. Instead, they need to be declared as init-only setter properties, as discused in the properties lesson.