Array In C#



Understanding Array in C#

Array in C#

We use an array to store similar type of data together. An array is a data structure that contains a number
of variables called the elements of the array. An array is fixed in size. In C# arrays can be created and
initialized in different ways:

int[] numbers = new int[5];

numbers[0] = 15;
numbers[1] = 6;
numbers[2] = 74;
numbers[3] = 8;
numbers[4] = 29;
First line,int[] numbers = new int[5] is for declaring an array which can keep five integer numbers
sequencially. Then next five lines is used to initialize five integers into the five location of numbers arrays
using index value. Suppose you need to get the third number of this array. You need to write the
following line:
int myNumber = numbers[2];
The value of myNumber will be 74.
If you go out-of-index for initializing or for getting data from an array, you will get an exception,
IndexOutOfRangeException in runtime.
numbers[5] = 8736;
int myNumber = numbers[8];
Both lines throw the IndexOutOfRangeException.
Array can be declare and initialize in a single line
int[] numbers = new int[5]{15,6,37,84,9};
or
int[] numbers = {5,28,72,8,36};
Following line is for declaring and initializing an string array, animals:
string[] animals = { "Elephant", "Cat", "Mouse"};

Walkthrough: Adding Five Numbers using array

1. Select conlose application from template and update the Main() method with following code:
static void Main(string[] args)
{
double[] numbers = new double[5];

3

double sum = 0;
Console.WriteLine("This program adds five numbers");
for (int index = 0; index < numbers.Length ; index++)
{
Console.WriteLine("Enter number " +
(index + 1).ToString());
numbers[index] = Convert.ToDouble(Console.ReadLine());
sum += numbers[index];
}
Console.WriteLine("Sum of five numbers is: " + sum);
Console.ReadKey();
}
2. Run the application.
Here, I use Length property of numbers array to get it’s length. All the remaining codes are self-
describtive so you can easily understand it.
In .Net class library, there’s a class Array which is used to do some operations for an array of any kind of
data type. Sort() method of Array is used to sort an array:
int[] numbers = new int[5] {31, 6, 4, 3, 7};
Array.Sort(numbers);
After running this code snippet you will get the sorted array.
IndexOf() method used to return index of a specific value within array:
int desiredValue = Array.IndexOf(numbers, 100);

Walkthrough: Sorting an array.

We will create an array of integer in which we will keep some integer values and by using Sort method of
Array class we will sort the array and print the elements of the array after sorting.
Steps:
1. Create a console application .
2. We will decalre and intialize an array in following way:
int[] numbers = new int[5] { 3, 6, 4, 3,100 };
3. We will sort an array using sort method and show output as below:
Array.Sort(numbers);
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);

}

Foreach loop:

You are already familiar with for, while, do-while for repeating statements in your code.Here, we will
discuss another interesting looping for iteration and it is Foreach. Foreach is used to iterate through an
arrays or collection. We can define foreach as below:
int[] numbers = { 5, 28, 72, 8, 36 };
foreach (int myNumber in numbers)
{
Console.WriteLine(myNumber);
}
In this code, we initialize an integer array and iterate through it using foreach.Remember, you can’t
initialize (directly) any value in any index position of an array using foreach. Even you can’t change
iteration variable inside foreach loop.
foreach (int myNumber in numbers)
{
myNumber = Convert.ToInt32(Console.ReadLine());
}
This code will show a compile error as we are trying to assign a value to iteration variable, myNumber.

إرسال تعليق

أحدث أقدم