O'Reilly logo

C# Data Structures and Algorithms by Marcin Jamro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Single-dimensional arrays

A single-dimensional array stores a collection of items of the same type, which are accessible by an index. It is important to remember that indices of arrays in C# are zero-based. This means that the first element has an index equal to 0, while the last one—length of the array minus one.

The example array is shown in the preceding diagram (on the left, indicated by a). It contains five elements with the following values: 9, -11, 6, -12, and 1. The first element has an index equal to 0, while the last one has an index equal to 4.

To use a single-dimensional array, you need to declare and initialize it. The declaration is very simple, because you just need to specify a type of element and a name, as follows:

type[] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required