Csharp Arrays
Csharp Arrays
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contig uous memory locations. T he lowest address corresponds to the first element and the hig hest address to the last element.
Declaring Arrays
T o declare an array in C#, you can use the following syntax:
datatype[] arrayName;
where, datatype is used to specify the type of elements to be stored in the array. [ ] specifies the rank of the array. T he rank specifies the size of the array. arrayName specifies the name of the array. For example,
double[] balance;
Initializing an Array
Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assig n values to the array. Array is a reference type, so you need to use the new keyword to create an instance of the array. For example,
double[] balance = new double[10];
You can assig n values to the array at the time of declaration, like:
double[] balance = { 2340.0, 4523.69, 3421.0};
In the preceding case, you may also omit the size of the array, like:
int [] marks = new int[] { 99, 98, 92, 97, 95};
You can also copy an array variable into another targ et array variable. In that case, both the targ et and source would point to the same memory location:
int [] marks = new int[] int[] score = marks; { 99, 98, 92, 97, 95};
When you create an array, C# compiler implicitly initializes each array element to a default value depending on the array type. For example for an int array all elements would be initialized to 0.
Following is an example, which will use all the above-mentioned three concepts viz. declaration, assig nment and accessing arrays:
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; } /* output each array element's value */ for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); } Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result:
Element[0] Element[1] Element[2] Element[3] Element[4] Element[5] Element[6] Element[7] Element[8] Element[9] = = = = = = = = = = 100 101 102 103 104 105 106 107 108 109
When the above code is compiled and executed, it produces the following result:
Element[0] Element[1] Element[2] Element[3] Element[4] Element[5] Element[6] Element[7] Element[8] Element[9] = = = = = = = = = = 100 101 102 103 104 105 106 107 108 109
C# Arrays in Detail
Arrays are important to C# and should need lots of more detail. T here are following few important concepts related to array which should be clear to a C# prog rammer:
Conc ept Multi-dimensional arrays Jag g ed arrays Passing arrays to functions Param arrays T he Array Class
Desc ription C# supports multidimensional arrays. T he simplest form of the multidimensional array is the two-dimensional array. C# supports multidimensional arrays, which are arrays of arrays. You can pass to the function a pointer to an array by specifying the array's name without an index. T his is used for passing unknown number of parameters to a function. Defined in System namespace, it is the base class to all arrays, and provides