Introduction
Arrays in C# look like they do in C/C++ but basically, a C# Array is derived from the base class System. Array. An Array is an un-ordered sequence of elements. All the elements in an Array have the same type unlike the fields in a structure or class which can have different types. The elements of an Array live in a contiguous block of memory and are accessed using an integer index, unlike fields in a structure or class which are accessed by name. Normally in C# the Array index starts with 0 but it is possible to have an Array with an arbitrary lower bound using the static method CreateInstance() of System. Array.
Arrays can be single or multidimensional and the declaration would be as follows for a single-dimensional.
Declaration of Array
int[] x = new int[12];
In the above statement, I have declared an integer type Array named x having 12 cells. Now to assign a value to them
x[0] = 5;
x[1] = 10;
// ... (omitting lines in between)
x[11] = 7;
Note. that index values start from 0 (are zero-based) and (for that Array) end at 11 for the 12 integer type cells. Let's have one more example and some discussion.
int[] b = {5, 7, 18, 15};
In the above example, the new keyword is missing but the statement is still valid. Why it is still valid is that for new just remember an Array can be used with or without having its instance. Keep on reading, you will find more on this.
class Program
{
static void Main()
{
int[] b = { 5, 7, 18, 15 };
for (int i = 0; i < b.Length; i++)
{
Console.WriteLine(b[i]);
}
Console.ReadKey();
}
}
Actually, Arrays are reference types regardless of the type of their elements. That means that an Array variable refers to a contiguous block of memory holding Array elements on the heap just like a class variable that refers to an object on the heap. This contiguous block of memory does not hold its Array elements directly on the stack as any structure (struct) does. Recall that when we declare a class variable, memory is not allocated for the object until we create its instance by using the new keyword. Here Array follows the same rules. When we declare an Array variable we do not declare its size at that time. We specify its size only when creating its instance and to create an instance we use a new keyword followed by element type, followed by the size of the Array between square brackets. One more thing we should note here is that Array initializes its default values automatically depending upon the type of value like 0 for numeric, null for reference (like string), and true/false for Boolean. Let's see some examples here.
int size = int.Parse(Console.ReadLine()); // assume size=4
int[] x = new int[size];
Or
int[] x = new int[4];
Let's see one example on strings.
int size = int.Parse(Console.ReadLine()); // assume size=4
string[] str = new string[size];
Or
string[] str = new string[4];
Initialization of Array
As we have discussed above, when we create an instance of Array, all the elements are automatically initialized to their default value depending upon their type. Actually, we always modify Array values. We can modify behavior and initialize the elements of an Array to a specific value if we prefer. We do this by providing a comma-separated list of values between a pair of braces. For example.
int[] x = new int[4] { 5, 7, 18, 15 };
One more way we have to assign values implicitly:
int[] x = new int[4];
x[0] = 5;
x[1] = 7;
x[2] = 18;
x[3] = 15;
Values of Array can be calculated at run-time also and stored in it. For example.
Random arVal = new Random();
int[] x = new int[4] { arVal.Next(), arVal.Next(), arVal.Next(), arVal.Next() };
Remember to match the size of an Array with the number of values, otherwise we will get compile time errors.



paul walkerPosted Jul 27, 2011, 4:14 AM
Good stuff, nice and clear - looking forward to part 2
Angelina ErinPosted Jul 26, 2011, 12:24 AM
Useful Article
Sivaraman DhamodaranPosted Jul 25, 2011, 7:55 AM
Real Good one for beginners. Some good stuff to learn. And, var myFrndz = new[]{ 23, 23.5, "Rahul", "Manish" }; var num = new[]{ 5, 7, 12.5, 12.6 }; Is var stands for varriant? In the article, I saw num is combination of float and integers. myFrndz is combination of above said in addition with strings.