Introduction to Arrays

C# arrays, as well as arrays in the CLR, are highly evolved from C/C++ arrays. In C/C++, you typically access an array by offsetting a pointer that points to the beginning of a contiguous range of items in a memory block somewhere. C/C++ arrays have no built-in range checking, which is the root of more bugs than you can shake a stick at. C# and the CLR solve this problem elegantly by making the array type a built-in, implicit type to the runtime.

When you declare a type-whether it’s a class or struct-the runtime reserves the right to silently generate an array type based upon that new type. The array type that it generates is a reference type- thus, all array instances are of class type. The reference type that it generates is derived from
System.Array, and ultimately from System.Object. Therefore, you can treat all C# arrays polymorphically through a reference to System.Array. Of course, that means that each array, no matter what concrete type of array it is, implements all of the methods and properties of System.Array. The way that you declare an array within C# is similar to C/C++, except the designers of the language took the liberty to make the syntax a tad more intuitive in their minds, in that the square brackets in the declaration follow the type and not the array variable name. The following example shows three ways to create an array:

using System;
public class EntryPoint
static void Main() {
int[] array1 = new int[ 10 ];
for( int i = 0; i < array1.Length; ++i ) {
array1[i] = i*2;
int[] array2 = new int[] { 2, 4, 6, 8 };
int[] array3 = { 1, 3, 5, 7 };

The longhand way to create an array instance and fill it with initial values is shown where array1 is initialized. Items are indexed using an indexer that is typically greater than or equal to 0. You may know that arrays in the CLR can have a user-defined lower bound. However, in C#, the lower bound is always 0 in order to meet the CLS restriction that arrays have a 0 lower bound. The initialization techniques used for array2 and array3 show a shorter notation for doing the same thing. Notice that in most cases, you must first allocate the array instances on the heap using the new operator. The same thing happens with the array3 instance, but the compiler does it for you in order to facilitate the notational shorthand. It’s interesting to note that an array of type object-thus, System.Object[]-is itself of type System.Object. One of the conveniences of .NET arrays is that theye are range-checked. Therefore, if you step off the end of one of them, thus going out of bounds, the runtime will throw an IndexOutOfRangeException instead of changing random memory, as in native C/C++. So you can say goodbye to those lurking, hardto-find range bugs, because the CLR won’t allow them to lurk too long, and they definitely won’t go unnoticed for long periods of time anymore.

Lastly, notice that you can conveniently iterate through the items in the array using the C# foreach statement. This works because System.Array implements IEnumerable.

Source Of Information : Apress Accelerated C Sharp 2010


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner