Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this syntax using new followed by a list inside braces?

Tags:

Ive just seen a piece of code that uses a generic list class to instantiate itself in the following manner:

var foo = new List<string>(){"hello", "goodbye"}; 

The curly braces after the contructor are especially confusing. It reminds me somewhat of

var bar = new string[]{"hi","bye"}; 

but in the past i've wouldve always used:

var foo = new List<string>(new []{"hello", "goodbye"}); 

Has anybody got a link explaining the syntax in the first line of code? I wouldnt even know where to begin with googling it.

like image 225
maxp Avatar asked Feb 24 '12 18:02

maxp


People also ask

What is the use of {} in Python?

In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.

What is the purpose of {} squiggly braces in Java?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What is curly bracket syntax?

curly-bracket language (plural curly-bracket languages) (programming) A programming language whose syntax uses curly brackets to enclose blocks, such as C, C++, Java, or C#.

What are curly braces used for in C++?

Beginner programmers, and programmers coming to C++ from the BASIC language often find using braces confusing or daunting. After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop.


1 Answers

As others have pointed out, that is a collection initializer. Some other features you might not be aware of that were added to C# 3:

  • A collection initializer constructor may omit the parentheses if the argument list is empty. So new List<int> { 10, 20, 30 } is fine.
  • An array initialized with an array initializer may in some cases omit the type. For example, var myInts = new[] { 10, 20, 30}; infers that myInts is int[].
  • Objects may be initialized using a similar object initializer syntax. var c = new Customer() { Name = "Fred" }; is the same as var temp = new Customer(); temp.Name = "Fred"; var c = temp;

The point of these features is to (1) make more things that used to require statements into things that require only expressions; LINQ likes things to be expressions, and (2) to enable richer type inference, particularly for anonymous types.

Finally: there has been some confusion in some of the answers and comments regarding what is required for a collection initializer. To be used with a collection initializer the type must (1) implement IEnumerable (so that we know it is a collection) and (2) have an Add method (so that we can add stuff to it.)

See

http://blogs.msdn.com/b/madst/archive/2006/10/10/what-is-a-collection_3f00_.aspx

for additional thoughts on the design of the feature.

like image 173
Eric Lippert Avatar answered Dec 20 '22 07:12

Eric Lippert