You are on page 1of 6

Array in c sharp For complete article please visit

http://awesomecsharp.blogspot.in/2013/06/awesome-c-sharparrays-by-santosh.html

Awesome c sharp :

Arrays

by santosh Hola friends [hi.... friends] welcome to awesome c sharp. Firstly i would like to pray for all my state people who are suffering from heavy monsoon and flood in Uttrakhand (hilly place in north INDIA near Himalayas) in india. It's a very sad moment for the people of uttrakhand Thanks all for supporting uttrakhand in this tragic moment. God bless every one in planet earth. now change the mode and let's back to the work today i m going to post on

ARRAY

Array
Dear Array is a collection of similar data types or memory location. or in other words An Array is a collection of the values of the same data type. For example, you can create an array that store 10 integer type values. The variables in an array are called an array element Array elements are accessed using a using a single name and an index number representing the position of the element within the array. Array is a reference type data type

Declaring an array
An array needs to be declared before it can be used in a program. You can declare an array by using the following statement. data type [] Arrayname; The explanation of the element of preceding statement is as follows. 1 Data type :- It is used to define data type of the element in the array. 2 [ ] :- It is used to define specify the the rank of the array. Rank is used to specify the size of the array 3 ArrayName :- is used to specify the name of the array using which the elements of the array will be initialize and manipulated. The following is the example of the array declaration. int [] my_first_array; In other words lets understand array in simple term Guys i m really bad in presenting live example and this one is not really cool because i m paining young one's hands in example hope my young friends will forgive me..... In school life young one's going school carries their school Bag's which contains different notebooks, books, lunchbox and other things etc. Now think what will happen if they carries all these things of school in their hand without their school bag. It will definitely pain. That's why parents give school bags to their kid. Which solves the purpose to handles all stuffs inside the bag, So we and say that that bag is working as a Array and different stuffs in them are their data. Now young one have to manage only that school bag all stuffs in inside the bag automatically managed inside it. That what array is all about.

In similar way it create complexities to create different different variable and manage them separate which are related to single entity in the program. Hope that u got the concept now lets see it in more detail. Array is a data structure that contains a number of elements of the same type.

Array Declaration
Declaring a array variable doesn't mean that initializing the array in the memory. when an array variable is initialized, you can assign values to the array elements. Array is a reference type, therefore you need to use new keyword to create the instance of the array. You must have noticed that while declaring the array, the size of the array was not mentioned. The size of the array is specified while it is initialized. The following is an example of array declaration. int [] score= new int [5]; in C sharp, the array subscript is always start with zero. therefore the preceding statement creates an array of integer containing 5 elements, from 0 to 4. It will be stored in heap structure in the memory

You can also assign values to every array element using an array initializer. Array initializer can only be used while declaring an array variable, not after the array is declared. int [ ] myarray= new int [5] {10, 20.30,40,50}; note if u initialize the array without using curly brackets, the size of the array can also be left out, as the compiler can count the number of element itself: int [ ] myarray= new int [ ] {10, 20.30,40,50}; There is even a shorter way to do that

int [ ] myarray= {10, 20.30,40,50};

Accessing array element Time to start developer visual studio


Friends if i define a array of 5 index like i m defining here watch carefully int [ ] my_array= new my_array [5]; now u have created 5 index in the heap memory and point to note is that every index can be accessed at any time. Now lets see how to do that open visual studio and do the code that i m doing in this below picture......

code is here using System; class Car { public static void Main() { Console.Title = "Accessing array index Awesome c sharp "; int[] my_array = { 10, 20, 30, 40, 50 }; Console.WriteLine("Accessing index 0 : "+my_array[0]); Console.WriteLine("Accessing index 1 : " + my_array[1]); Console.WriteLine("Accessing index 2 : " + my_array[2]); Console.WriteLine("Accessing index 3 : " + my_array[3]); Console.WriteLine("Accessing index 4 : " + my_array[4]); }}

it is a tiring process to write every time the index number. For simplicity programmers implement loops for accessing indexes of huge array. or dynamic array collection

for( int i=0; i<= my_array.Length-1; i++) { Console.WriteLine(my_array[i]); } instead using for loops programmer using foreach loop for making work easier. using first time this foreach loop for my dear readers. foreach(int num in my_array) { Console.WriteLine(num); } same way u can make any type of array for example string type, float, double type. Dear friends you can also copy your array into another array let's see how to do that is easy and fun to learn

simply transferring values from my_array to second_array it will automatically mange the size of array in transferring. code is here using System; class Car { public static void Main() { Console.Title = "Accessing array index Awesome c sharp "; int[] my_array = { 10, 20, 30, 40, 50 }; int[] second_array = my_array;

Console.WriteLine("Friends copying from my_array to second_array"); foreach (int num in second_array) { Console.WriteLine(num); } }}

For complete article please visit

http://awesomecsharp.blogspot.in/2013/06/awesome-c-sharparrays-by-santosh.html

You might also like