Lect8
Lect8
NET
Microsoft C#.NET
Qadeem Khan
IIT, KUST
qadeem_kust@yahoo.com
What Is an "Array"?
Arrays are vital for most programming languages. They are
collections of variables, which we call elements:
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.
Declaring an Array
We declare an array in C# in the following
way:
int[] myArray;
Creation of an Array – the
Operator "new"
In C# we create an array with the help of
the keyword new, which is used to
allocate memory:
int[] myArray = { 1, 2, 3, 4, 5, 6 };
Declaration and Initialization of
an Array – Example
Here is one more example how to declare
and initialize an array:
string[] daysOfWeek = { "Monday",
"Tuesday", "Wednesday","Thursday",
"Friday", "Saturday", "Sunday" };
Access to the Elements of an
Array
We access the array elements directly using
their indices. Each element can be
accessed through the name of the
array and the element’s index
(consecutive number) placed in the
brackets. We can access given
elements of the array both for reading
and for writing, which means we can
treat elements as variables.
myArray[index] = 100;
int[] myArray = new int[6];
myArray[1] = 1;
myArray[5] = 5;
iterate through the array using
a loop statement
Going Out of Bounds of the
Array
The .NET Framework does an automatic
check on each element access attempt, whether
the index is valid or it is out of the range of the
array. When we try to access an invalid (not
existing) element in an array,
aSystem.IndexOutOfRangeException is
thrown. The automatic check really helps the
developers find errors while working with arrays.
Of course, checking for exceptions has its price.
Checks affect the performance, but that’s
nothing compared to avoiding errors like "out of
range", "access to unallocated memory", etc.
Reversing an Array – Example
In the next example we will access
elements and change them using their
indices. The task is to print the elements
in reversed order. We will reverse the
elements of the array using a second,
auxiliary array, where we will keep the
elements of the first one, but in a reversed
order. Note that the length of both arrays is
the same and it stays unchanged after the
first allocation:
Searching into an array
Array Class in C#.NET
The Array class is the base class for all the
arrays in C#. It is defined in the System
namespace. The Array class provides
various properties and methods to work
with arrays.
Properties of the Array Class
Methods of the Array Class
Guess the output??????
Reading an Array from the
Console
We will use a for-loop and the .NET
Framework tools for reading from the console.
Initially we read a line from the console
using Console.ReadLine(), and then we
parse that line to an integer number using
int.Parse() and we set it to the variable n.
We then use the number n as length of the
array.
int n = int.Parse(Console.ReadLine());
int[] array = new int[n];
Again we use a loop to iterate through the
array. At each iteration we set the current
element to what we have read from the
console. The loop will continue n times,
which means it will iterate through the
array and so we will read a value for
each element of the array:
for (int i = 0; i < n; i++)
{
array[i] = int.Parse(Console.ReadLine());
}
string []answer = new string[5]
for(int i = 0;i<answer.length;i++)
{ answer[i]= Console.ReadLine();
}
double[] array = new double[6];
Console.WriteLine("Please Enter 6 Floating numbers");
for (int i = 0; i < 6; i++)
{
array[i] = Convert.ToDouble(Console.ReadLine());
}
double sum = 0;
case "no":
Console.WriteLine("===============================================");
Console.WriteLine("HAVE A NICE DAY SIR");
break;
}