1.10. The C# Array

The built-in array in C# is a fixed-size container holding elements of a single type. When we declare an array object, however, the actual size of the array is not part of its declaration. In fact, providing an explicit size generates a compile-time error—for example,

string []     text; // OK
string [ 10 ] text; // error

We declare a multidimensional array by marking each additional dimension with a comma, as follows:

string [,]    two_dimensions;
string [,,]   three_dimensions;
string [,,,]  four_dimensions;

When we write

string [] messages;

messages represents a handle to an array object of string elements, but it is not itself the array object. By default, messages is set to null. Before we can store elements within the array, ...

Get C# Primer: A Practical Approach 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.