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
C# Equivalent to Java's Double Brace Initialization?
Java's Double Brace Initialization is a technique that allows creating and initializing collections in a single expression using an anonymous inner class. C# provides equivalent functionality through Collection Initializers and Object Initializers, which offer cleaner and more efficient syntax.
Java Double Brace Initialization
In Java, double brace initialization uses nested braces where the outer braces create an anonymous class and the inner braces contain an instance initializer −
List<String> list = new List<String>() {{
add("One");
add("Two");
add("Three");
add("Four");
}}
C# Collection Initializer Syntax
C# achieves the same result with a much simpler syntax using collection initializers −
List<string> list = new List<string>() {"One", "Two", "Three", "Four"};
You can also omit the parentheses for even cleaner syntax −
List<string> list = new List<string> {"One", "Two", "Three", "Four"};
Using Collection Initializers
Example with List
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<string> fruits = new List<string> {"Apple", "Banana", "Orange", "Mango"};
Console.WriteLine("Fruits in the list:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Fruits in the list: Apple Banana Orange Mango
Example with Dictionary
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int> {
{"Alice", 25},
{"Bob", 30},
{"Charlie", 35}
};
Console.WriteLine("Ages:");
foreach (var person in ages) {
Console.WriteLine($"{person.Key}: {person.Value}");
}
}
}
The output of the above code is −
Ages: Alice: 25 Bob: 30 Charlie: 35
Using Object Initializers
C# also supports object initializers for setting properties during object creation −
using System;
using System.Collections.Generic;
class Person {
public string Name { get; set; }
public int Age { get; set; }
public override string ToString() {
return $"{Name}, Age: {Age}";
}
}
class Program {
public static void Main() {
List<Person> people = new List<Person> {
new Person { Name = "John", Age = 25 },
new Person { Name = "Jane", Age = 30 },
new Person { Name = "Mike", Age = 35 }
};
Console.WriteLine("People:");
foreach (Person person in people) {
Console.WriteLine(person);
}
}
}
The output of the above code is −
People: John, Age: 25 Jane, Age: 30 Mike, Age: 35
Comparison
| Aspect | Java Double Brace | C# Initializers |
|---|---|---|
| Syntax | Uses anonymous inner class with two sets of braces | Single set of braces with direct values |
| Performance | Creates additional .class file and holds reference to outer class | More efficient, no extra class creation |
| Memory | Can cause memory leaks due to outer class reference | No memory overhead |
| Readability | Verbose syntax | Clean and concise |
Conclusion
C#'s collection and object initializers provide a cleaner, more efficient alternative to Java's double brace initialization. They offer better performance without the memory overhead of anonymous classes, making code more readable and maintainable.
