Start C# here

Study C#.. Go ahead ! !

Wednesday, July 23, 2008

C# Array

Arrays are collection of Elements having the same DataType stored in Consecutive memory locations. Arrays can only store data of the same type ie, if it’s an integer array the array can only store Integer values. It will not allow any other types to be stored.

Let’s see how we can declare an array in C#.

If you need to create a character array to store ten elements then we can declare it like this…

char[] cArr=new char[10];

To pass values to an Array at the time of declaration we can write an array like this…

char[] cArr={‘H’,’e’,’l’,’l’,’o’,’!’};

Since this is a character array we can only pass single values.

We’ll now see how we create arrays for each type..

Integer Array in C#
--------------------

int[] iArr=new int[10];
or
int[] iArr={1,2,3,4,5};

String Array in C#
-------------------
string[] sArr=new string[10];
or
string[] sArr={“Hello”,”world”};

In an array.. items are stored in index of the array. For retrieving values in an array we must mention the index. Always the first index of an array will be 0 and the last index will be the size of the index minus 1 ie if the size is 10 for an array the last index will be 9.

In the above case of the string array the values “Hello” and "World" will be stored in two index of the array. The index of the “Hello” will be 0 and “World” will be 1.
If we need to just print hello on the screen then we must have to write..

Console.WriteLine(sArr[0]);

Here in the above code sArr is the Array name and 0 is used to mention the Element in which index should be selected likewise if we need to print World then we must have to say sArr[1]..

Now we will check how we can print all items in an Array. For doing this we can use two types of loops.
The first one is for loop…

For loop to display Elements in an Array
-----------------------------------------

for(int i=0;i
{
Console.WriteLine(sArr[i]);
}

Here in the above case we have written int i=0 in the for loop and for printing we have just written it sArr[i] here we says I because its having the values from 0 to the value less than the length of the array.

So, when the loop works for the first time its value will be 0 so it prints “Hello” and when it goes for the second time the value is 1 so it prints “World” and when it goes for the third time the value is not less than 2 ie the length is two for the array and now the value of I is also two so it terminates…..

When we use for loop it have a drawback ie we must know the size of the array. For curing that we have another loop ie foreach loop in c#.

In a foreach loop its not necessary to know the size of the array…
We’ll just see one foreach loop for the same array…

Foreach loop to display items in an array..
-------------------------------------------

foreach(string s in sArr)
{
Console.WriteLine(s);
}

In the above code we are using foreach loop if just see the two loops itself we can see the difference. In a Foreach loop we don’t need to mention the array size to be printed…. When we run the foreach loop it will store the first element in string variable s and prints it and next time it will automatically gets incremented to the next element and prints the second element…. Likewise it will run till the array terminates..

No comments: