C# Equivalent to Java's Double Brace Initialization?



Java’s Double Brace Initialization does the same work what a single brace can achieve in C#.

Double Brace creates and initialize objects in a single Java expression.

Let’s say the following is in Java −

Example

List<String> list = new List<String>() {{
   add("One");
   add("Two");
   add("Three");
   add("Four");
}}

The same you can use for Collection Initializer in C# as −

List<String> list = new List<String>() {"One","Two", “Three”, “Four”};

Advertisements