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”};

Updated on: 22-Jun-2020

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements