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
Collection Initialization in C#
Collection initialization in C# allows you to initialize collections with values at the time of creation using a concise syntax. This feature, introduced in C# 3.0, makes code more readable and eliminates the need for multiple Add() method calls.
Collection initializers work with any collection type that implements IEnumerable and has an Add method, including List<T>, Dictionary<TKey, TValue>, arrays, and custom collections.
Syntax
Following is the syntax for collection initialization −
CollectionType<T> collectionName = new CollectionType<T> { item1, item2, item3 };
For objects with properties, you can combine object and collection initialization −
List<ClassName> list = new List<ClassName> {
new ClassName { Property1 = value1, Property2 = value2 },
new ClassName { Property1 = value3, Property2 = value4 }
};
Using Collection Initialization with Objects
You can initialize collections of custom objects by combining object initialization syntax with collection initialization −
using System;
using System.Collections.Generic;
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() {
var emp1 = new Employee() { EID = 001, EmpName = "Tim", EmpDept = "Finance"};
var emp2 = new Employee() { EID = 002, EmpName = "Tom", EmpDept = "HR"};
IList<Employee> empDetails = new List<Employee> {emp1, emp2 };
// Employee 1
Console.WriteLine("Employee One...");
Console.WriteLine(emp1.EID);
Console.WriteLine(emp1.EmpName);
Console.WriteLine(emp1.EmpDept);
// Employee 2
Console.WriteLine("Employee Two...");
Console.WriteLine(emp2.EID);
Console.WriteLine(emp2.EmpName);
Console.WriteLine(emp2.EmpDept);
}
}
The output of the above code is −
Employee One... 1 Tim Finance Employee Two... 2 Tom HR
Direct Collection Initialization
You can also initialize collections directly without creating separate object variables first −
using System;
using System.Collections.Generic;
public class Student {
public int ID { get; set; }
public string Name { get; set; }
public double Grade { get; set; }
}
public class Program {
public static void Main() {
List<Student> students = new List<Student> {
new Student { ID = 101, Name = "Alice", Grade = 85.5 },
new Student { ID = 102, Name = "Bob", Grade = 92.0 },
new Student { ID = 103, Name = "Charlie", Grade = 78.3 }
};
Console.WriteLine("Student Records:");
foreach(Student student in students) {
Console.WriteLine($"ID: {student.ID}, Name: {student.Name}, Grade: {student.Grade}");
}
}
}
The output of the above code is −
Student Records: ID: 101, Name: Alice, Grade: 85.5 ID: 102, Name: Bob, Grade: 92 ID: 103, Name: Charlie, Grade: 78.3
Dictionary Collection Initialization
Dictionary collections can also be initialized using collection initializer syntax with key-value pairs −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int> {
{"John", 25},
{"Sarah", 30},
{"Mike", 28}
};
Console.WriteLine("Age Dictionary:");
foreach(var pair in ages) {
Console.WriteLine($"{pair.Key}: {pair.Value} years old");
}
}
}
The output of the above code is −
Age Dictionary: John: 25 years old Sarah: 30 years old Mike: 28 years old
Collection Initialization vs Traditional Approach
| Collection Initialization | Traditional Approach |
|---|---|
| More concise and readable syntax | Requires multiple Add() method calls |
| Initialization at declaration time | Separate initialization steps |
| List<int> numbers = new List<int> {1, 2, 3}; | List<int> numbers = new List<int>(); numbers.Add(1); numbers.Add(2); |
Conclusion
Collection initialization in C# provides a clean, readable way to initialize collections with values at the time of creation. This syntax works with any collection implementing IEnumerable with an Add method, making your code more concise and maintainable than traditional initialization approaches.
