Simple Arrays C# Help

If you need to use multiple objects of the same type, you can use an array. An array is a data structure that contains a number of elements of the same type.

Array Declaration

An array is declared by defining the type of the elements inside the array followed by empty brackets and a variable name; for example, an array containing integer elements is declared like this,

int {} myArray;

Array Initialization

After declaring an array, memory must be allocated to hold all the elements of the array. An array is a reference type, so memory on the heap must be allocated. You do this by initializing the variable of the array using the new operator with the type and the number of elements inside the array. Here you specify the size of the array:

myArray = new int(4);

With this declaration and initialization, the variable myArray references four integer values that are allocated on the managed heap.

Untitled

The array cannot be resized after the size was specified without copying all ,_ elements. If you don’t know the number of elements that should be in the array “in”‘- advance, you can use a collection.

Instead of using a separate line for the declaration and initialization, you can declare and initialize an array in a single line:

int{} myArray = new int(4);

You can also assign values to every array element using an array initializer. Array initializers can be used only while declaring an array variable, not after the array is declared.

int{} myArray’ = new int(4) (4, 7. 11. 2);

If you initialize the array using curly brackets, the size of the array can also be left out, because the compiler can count the number of elements itself:

int{} myArray = new intI] (4. 7. 11, 2);

There’s even a shorter form using the C# compiler. Using curly brackets you can write the array declaration and initialization. The code generated from the compiler is the same as in the previous example.

int{} myArray = (4. 7. 11. 2);

Accessing Array Elements

After an array is declared and initialized, you can access the array elements using an indexer. Arrays only support indexers that have integer parameters.

With custom classes, you can also create indexers that support other types.

With the indexer, you pass the element number to access the array. The indexer always starts with a value of 0 for the first element. The highest number you can pass to the indexer is the number of elements minus one, because the index starts at zero. In the following example, the array myArray is declared and initialized with four integer values. The elements can be accessed with indexer values 0, 1, 2, and 3.

If you don’t know the number of elements in the array, you can use the Length property that is used in  this for statement:

for (int i = 0; i < myArray.Length; i++)
( . Consdle.WriteLine(myArray[i]);

Instead of using a- for statement to iterate through all elements of the array, you can also use the for each statement:

for each (int val in myArra~)
(
Console’. wri teLine (val)

The for each statement makes use of the IEnumerable and IEnumerator interfaces, which are discussed later

Using Reference Types

In addition to being able to declare arrays of predefined types, you can also declare arrays of custom types. Let’s start with this Person class with two constructors, the properties FirstName and LastName using auto-implemented properties, and an override of the ToString () method from the Object class:

public class Person
(
public Person ()
{
}
public Person(string firstName. string lastName)

this.FirstName = firstName;
this.LastName = lastName;
public string FirstName { get; set; }
public string LastName ( get; set;
public override string ToString{)
(
return String.Format(‘{O) {I}’,
FirstName, LastName

Declaring an array of two Person elements is similar to declaring an array of int:

Person[] myPersons = new Person[2];

However, you must be aware that if the elements in the array are reference types, memory must be allocated for every array element. In case you use an item in the array where no memory was allocated, a NullReferenceException is thrown.

You can allocate every element of the array by using an indexer starting from 0:

myPersons [0 ].,,: new Person ( ‘Ayrton’, ‘Senna”);
myPersons[l] = new Person(‘Michael’, ‘Schumacher’);

the objects in the managed heap with the Person array. myPersons is a variable that is stored on the stack. This variable references an array of Person elements that is stored on the managed heap. This array has enough space for two references. Every item in the array references a Person object that is also stored in the managed heap.

Untitled

As with the int type, you can also use an array initializer with custom types:

Person[] myPersons = ( new Person(‘Ayrton’, ‘Senna’),
new Person(‘Michael’,
‘Schumacher”) };

Posted on October 28, 2015 in Arrays

Share the Story

Back to Top