In the previous post, we learned about Classes and Constructors in C#.
You know that we use a constructor to initialize an object and put it in an early State.
Topics Covered
Object Initializer in C#
In C#, there is also another way to initialize an object apart from constructors and that is using an object initializer.
An object initializer is simply a syntax or a way of quickly initializing an object without the need to call one of its constructors.
All we need to do is simply avoid creating multiple constructors.
Object Initializer in C# – Example
Let me show you an example:
Imagine we have this Student class with a bunch of fields.
public class Student { public int Id; public string FirstName; public string LastName; public int Age; }
Now, this is a very simple example we only have four fields here.
In a real-world application, you might have a few more fields.
Now with this class, if you want to create constructors for initializing various fields we may end up with a class like this:
public class Student { public int Id; public string FirstName; public string LastName; public int Age; public Student(int id) {} public Student(int id, string firstName) {} public Student(int id, string firstName, string lastName) {} public Student(int id, int age) {} }
As you see here, we have four constructors:
- The first one takes only an id
- The second one takes an id and a firstName
- The third one takes an id with firstName and lastName
- The fourth one takes an id and age
As you can see there are so many different combinations of constructors and this is what we used to do in C# before object initializers were introduced.
By now, you might have already aware that writing code like that is messy and easily becomes out of control.
So the object initializer came to solve this problem.
Object Initializer Syntax in C#
With an object initializer, we don’t need any of these constructors.
We can simply initialize a Student object like this:
var student = new Student { FirstName = "Anders", LastName = "Hejlsberg" };
Note these curly braces here. We are not calling one of the constructors of the Student class. This is what we call object initialization syntax.
So basically, we specify the name of properties and assign them some initial value.
Note that behind the scene when we initialize an object like this, the default or parameterless constructor is going to be called, and then any properties we set here are going to be initialized.
So in this way, we can reserve constructors for scenarios where we really need to use them.
An object cannot behave without being passed some initial values.
That’s pretty much what we need to know about the object initializer in C# from a syntax and background working point of view.
Collections Initializer Syntax
Using collection Initializer Syntax, the collections can also be initialized the same way as we did for class objects.
var s1 = new Student() { Id = 1, FirstName = "Foo" }; var s2 = new Student() { Id = 2, FirstName = "Bar" }; var s3 = new Student() { Id = 3, FirstName = "Baaz" } ; IList studentList = new List() { s1, s2, s3 };
Advantages
- Object initializer makes it easier to initialize the objects making code more readable
- It is useful in multi-threading scenarios
Further Reading
For advanced reading, you may refer to Microsoft’s documentation on object initializer in C#.
⭐C# Mastery – Mosh Hamedani
One of the best courses to master your C# skills right from level zero. This series is divided into 4 sections:
- C# Basics
- C# Intermediate
- C# Advanced
- Unit Testing for C# Developers
Summary
Let’s summarize and focus on takeaway points from this blog post:
- The object initializer gives us another way to initialize the objects in C#.
- Also, we don’t end up writing different overloaded versions of constructors.
- We can reserve constructors for scenarios where we really need to use them.
I hope you enjoyed this post and understood the concept of object initializer in C#. Please share this with your developer friends and colleagues.
Happy Coding { }