You are on page 1of 2

Arrays offer the most efficient method for storing lists of data in C.

They're very
easy for the programmer to create and use, and they're remarkably quick for the
computer to access and update. However, they can waste a lot of memory, so
they're not always the best thing to use. When not used carefully, they can also
cause crashes, bugs and system instability

In simple terms, an array is a data structure or list containing elements of the same type.
Arrays are an intrinsic part of the C family of languages.

In C#, an array starts at 0 which means the first item of the array is stored at offset 0. The
position of the last item of an array is number of items – 1.

The array is a reference type, but the members of the array are created based on their
type. For example, an array of integers contains integers, not boxed integer values. Value
types are initialized to default values.

• numeric (int, long) values are initialized to 0


• bool values are initialized to false
• char values are initialized to ‘’ (null)
• enum values are initialized to 0
• reference values are initialized to null

Arrays are considered to be objects, so just declaring an array does not actually create it.
The new operator is needed to instantiate an array.

int [] intArray;
intArray = new int[5] {0,2,4,6,8}

C# arrays can be fixed length ([5]) or dynamic ([]). A fixed length array stores a
predefined number of elements where as the dynamic array automatically sizes as
elements are added or taken away.

Arrays support the foreach loop. The foreach loop could be considered the most powerful
loop in C#. It iterates through a collection one by one.

foreach (Employee emp in empArray)


Console.WriteLine(“ID: {0}”, emp.ID);

Array types

There are four types of arrays in C#. They are single-dimensional arrays, multi-
dimensional arrays, jagged arrays and mixed arrays.

A single dimensional array is the simplest of all the array types. The array consists of
rows of values (single column).

string[] strCities = new string[4] { “Houston”, “Austin”, “Tuscon”, “Sedona” };


Multi-dimensional arrays are also known as rectangular arrays. This type of array
consists of columns of values as well as rows.

string[,] strCityStates = new string[,]

{ { “Houston”, “TX” }, { “Austin”, “TX” }, { “Tuscon”, “AZ” }, { “Sedona”, “AZ” } };

Jagged arrays are known to be an array of arrays. An element of a jagged array is an


array. For example, there could be an array of addresses where the address itself can be
an array of 4 strings (address, city, state, zip).

Mixed arrays are a combination of multi-dimension and jagged arrays.

You might also like