Initializing Array Elements

You can initialize the contents of an array at the time you create it by providing a list of values delimited by curly braces ({}). C# provides a longer and a shorter syntax:

int[] myIntArray = new int[5] { 2, 4, 6, 8, 10 };
int[] myIntArray = { 2, 4, 6, 8, 10 };

In the shorter syntax, C# automatically creates an array of the proper size for the number of elements in the braces. There is no practical difference between these two statements, and most programmers will use the shorter syntax.

Get Learning C# 3.0 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.