Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Initializer in C#
An object initializer in C# allows you to initialize an object's properties or fields at the time of object creation without explicitly calling a constructor with parameters. This feature provides a more readable and concise way to create and initialize objects.
Object initializers use curly braces {} to assign values to accessible properties or fields immediately after creating the object instance.
Syntax
Following is the basic syntax for object initializers −
ClassName objectName = new ClassName() {
PropertyName1 = value1,
PropertyName2 = value2,
FieldName = value3
};
You can also omit the parentheses when using parameterless constructor −
ClassName objectName = new ClassName {
PropertyName1 = value1,
PropertyName2 = value2
};
Using Object Initializers with Properties
Example
using System;
public class Employee {
public int EID { get; set; }
public string EmpName { get; set; }
public string EmpDept { get; set; }
}
public class Demo {
public static void Main() {
Employee empDetails = new Employee() {
EID = 10,
EmpName = "Tim",
EmpDept = "Finance"
};
Console.WriteLine("Employee ID: " + empDetails.EID);
Console.WriteLine("Employee Name: " + empDetails.EmpName);
Console.WriteLine("Department: " + empDetails.EmpDept);
}
}
The output of the above code is −
Employee ID: 10 Employee Name: Tim Department: Finance
Using Object Initializers with Constructor Parameters
Object initializers can be combined with parameterized constructors. The constructor runs first, then the initializer sets additional properties −
Example
using System;
public class Product {
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public Product(int id) {
ID = id;
Console.WriteLine("Constructor called with ID: " + id);
}
}
public class Program {
public static void Main() {
Product product = new Product(101) {
Name = "Laptop",
Price = 899.99m,
Category = "Electronics"
};
Console.WriteLine($"Product: {product.Name}, Price: ${product.Price}, Category: {product.Category}");
}
}
The output of the above code is −
Constructor called with ID: 101 Product: Laptop, Price: $899.99, Category: Electronics
Collection Initializers
Object initializers work seamlessly with collection initializers for properties that are collections −
Example
using System;
using System.Collections.Generic;
public class Department {
public string Name { get; set; }
public List<string> Employees { get; set; } = new List<string>();
}
public class Company {
public string Name { get; set; }
public List<Department> Departments { get; set; } = new List<Department>();
}
public class Program {
public static void Main() {
Company company = new Company {
Name = "TechCorp",
Departments = new List<Department> {
new Department { Name = "IT", Employees = {"Alice", "Bob"} },
new Department { Name = "HR", Employees = {"Carol", "Dave"} }
}
};
Console.WriteLine("Company: " + company.Name);
foreach(var dept in company.Departments) {
Console.WriteLine($"Department: {dept.Name}, Employees: {string.Join(", ", dept.Employees)}");
}
}
}
The output of the above code is −
Company: TechCorp Department: IT, Employees: Alice, Bob Department: HR, Employees: Carol, Dave
Key Benefits
| Feature | Description |
|---|---|
| Readability | Makes object creation and initialization more readable and concise |
| Flexibility | Can initialize any accessible property or field |
| Constructor Compatible | Works with both parameterless and parameterized constructors |
| Collection Support | Integrates well with collection and array initializers |
Conclusion
Object initializers in C# provide a clean and readable way to create and initialize objects in a single statement. They work with constructors and collections, making object creation more expressive and reducing boilerplate code while maintaining type safety and IntelliSense support.
