Arrays in C#
Array Declaration :
An Array is a collection of values of a similar data type. Technically, C# arrays are a reference type. Each array in C# is an object and is inherited from the System.Array class. Arrays are declared as:
[]
Let's define an array of type int to hold 10 integers.
int [] integers = new int[10];
The size of an array is fixed and must be defined before using it. However, you can use variables to define the size of the array:
int size = 10;
int [] integers = new int[size];
You can optionally do declaration and initialization in separate steps:
int [] integers;
integers = new int[10]
It is also possible to define arrays using the values it will hold by enclosing values in curly brackets and separating individual values with a comma:
int [] integers = {1, 2, 3, 4, 5}
This will create an array of size 5, whose successive values will be 1, 2, 3, 4 and 5.
Accessing the values stored in an array
To access the values in an Array, we use the indexing operator [int index]. We do this by passing an int to indicate which particular index value we wish to access. It's important to note that index values in C# start from 0. So if an array contains 5 elements, the first element would be at index 0, the second at index 1 and the last (fifth) at index 4. The following lines demonstrate how to access the 3rd element of an array:
int [] intArray = {5, 10, 15, 20};
int j = intArray[2];
PROGRAM :
// demonstrates the use of arrays in C#
static void Main()
{
// declaring and initializing an array of type integer
int [] integers = {3, 7, 2, 14, 65};
// iterating through the array and printing each element
for(int i=0; i<5; i++)
{
Console.WriteLine(integers[i]);
}
}
Here we used the for loop to iterate through the array and the Console.WriteLine() method to print each individual element of the array. Note how the indexing operator [] is used.
The above program is quite simple and efficient, but we had to hard-code the size of the array in the for loop. As we mentioned earlier, arrays in C# are reference type and are a sub-class of the System.Array Class. This class has lot of useful properties and methods that can be applied to any instance of an array that we define. Properties are very much like the combination of getter and setter methods in common Object Oriented languages.
Properties are context sensitive, which means that the compiler can un-ambiguously identify whether it should call the getter or setter in any given context. We will discuss properties in detail in the coming lessons. System.Array has a very useful read-only property named Length that can be used to find the length, or size, of an array programatically.
Using the Length property, the for loop in the above program can be written as:
for(int i=0; i
{
Console.WriteLine(integers[i]);
}
This version of looping is much more flexible and can be applied to an array of any size and of any data-type. Now we can understand the usual description of Main(). Main is usually declared as:
static void Main(string [] args)
The command line arguments that we pass when executing our program are available in our programs through an array of type string identified by the args string array.
int[] numbers = new int[20];
Array Initialization :
int[] numbers = {0, 1, 2, 3, 5};
This is identical to the complete initialisation statement:
int[] numbers = new int[] {0, 1, 2, 3, 5};
array types. The class Array contains methods for sorting and searching.
using System;
class myArray
public static void Main()
Console.Write("\n");
int pos1 = Array.BinarySearch(slist,"carl");
Array.Sort(slist);
Console.Write("pos2 = {0}",pos2); Console.WriteLine();
for(int j=0;j
public class TwoDim
public static void Main()
double[,] myarray;
No comments:
Post a Comment