Collection Initializers
When arrays were introduced in Chapter 4, “Language Essentials,” you saw the use of array initializer expressions to create an array and initialize its elements in one operation:
int[] primes = new int[4] { 2, 3, 5, 7 };
Recall that various kinds of type inference can be used to simplify the preceding declaration and initialization, even all the way down to the following:
var primes = new [] { 2, 3, 5, 7 };
What’s of more relevance here is the use of an initializer list delineated by curly braces to supply the array’s elements. You’ll agree that’s way more handy than having to write the following:
int[] primes = new int[4]; ...
Get C# 4.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.