Initializing Arrays

The array elements of an array can be initialized, with values of your choice, at the time the array is being created. To accomplish this, you first need to write the declaration of the array variable as usual. However, instead of creating the array object using the new keyword, as in the following

new int [6];

you can specify the list of initialization values explicitly by enclosing them in braces, and assigning this list to the array variable with the assignment operator. The following line follows this procedure:

The array object is automatically created with just enough array elements (six, in this example) to hold the ...

Get C# Primer Plus 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.