C# Equivalent to Java's Double Brace Initialization?

CsharpProgrammingServer Side Programming

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”};
raja
Published on 31-Aug-2018 15:24:37
Advertisements