Tutorials
Desktop Programming
C Sharp
ARRAYS IN C#
Written by Christopher Sunday, 15 June 2008 18:39
INTRODUCTION
When defining variables you can choose between scalar number and arrays. Arrays in C# are declared by adding a set of square brackets to the end of the variable declaration. All members of an array must be of the same type. You cannot have different types in an array.
USING ARRAYS
An example of an array declaration is the following:
Â
int[] integers;
This array is not initialized and we don’t know its dimensions. In order to declare and initialize the array you have to use the following statement:
int[] integers = new int[18];
The individual elements in this array are value types. However, the array itself is a reference type. It follows reference semantics and usage. If you assign one array variable to another it will just copy the reference to it and not the array. All C# arrays are zero-based. The first element has zero index value. To access the second element of the array you reference it with the index value 1:
               int a = integers[1];
Since the declaration of an array does not instantiate it we can use this to our advantage. We can declare an array and initialize it later in another portion of our code. This allows for dynamically-sized arrays. This technique assigns the null reference value to the array and later, when initialized, this reference is assigned the address of a new memory block of data requested with new keyword.
The array class contains several members for working with its contained data. Common methods include:
·        Clear() : Sets the specified elements of the array to zero, false or null depending the data type.
·        Copy(): Copies a range of elements from a source array to a destination array starting at specified indexes for both arrays.
·        ConstrainedCopy():Copies a range of elements from a source array to a destination array starting at specified indexes for both arrays. Guarantees that all changes will be undone if the copy is not successful.
·        Exists() : Returns true or false. It searches to see if the array contains elements that match the conditions defined in the arguments of the method.
·        FindIndex(): searches the array for elements matching the conditions defined in the arguments of the method. It returns the zero-based index of the first occurrence.
·        Reverse(): Reverses the sequence of elements in the specified array.
·        Sorrt(): Sorts the elements of the array according to the keys of the elements.
·        TrueforAll(): Returns a Boolean value. True if all elements of the array match the conditions specified in the method’s arguments.

| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |