Fundamentals of Computer Programming with C# HTML version
Chapter 7. Arrays
In This Chapter
In this chapter we will learn about arrays as a way to work with sequences
of elements of the same type. We will explain what arrays are, how we
declare, create, instantiate and use them. We will examine one-dimensional
and multidimensional arrays. We will learn different ways to iterate
through the array, read from the standard input and write to the standard
output. We will give many example exercises, which can be solved using
arrays and we will show how useful they really are.
What Is an "Array"?
Arrays are vital for most programming languages. They are collections of
variables, which we call elements:
Array of 5
0 1 2 3 4
… … … … …
An array’s elements in C# are numbered with 0, 1, 2, … N-1. Those numbers
are called indices. The total number of elements in a given array we call
length of an array.
All elements of a given array are of the same type, no matter whether they
are primitive or reference types. This allows us to represent a group of
similar elements as an ordered sequence and work on them as a whole.
Arrays can be in different dimensions, but the most used are the one-
dimensional and the two-dimensional arrays. One-dimensional arrays are
also called vectors and two-dimensional are also known as matrices.
Declaration and Allocation of Memory for Arrays
In C# the arrays have fixed length, which is set at the time of their
instantiation and determines the total number of elements. Once the length of
an array is set we cannot change it anymore.