Arrays

Just as enumerations group constants together, so arrays can be thought of as grouping variables together. There's a lot of support for arrays built into C#, and we'll take a look at it in Chapter 6. As you know, arrays store data values by index. In C#, arrays are reference types, so you can create a new array with the new operator. You declare an array as type[], where type is the data type of each element. For example, here's how to declare an array of five integer elements:

int[] array1 = new int[5];

These elements can be addressed with index values 0 to 4, as array1[0], array1[1], up to array1[4]. You can see this at work in Listing 1.5, where we've created a C# array, placed data in one of its elements, and displayed that data. ...

Get Microsoft® Visual C#® .NET 2003 Kick Start 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.