Saturday, May 19, 2012
Forum Minimize
 
Web Development & Design ForumWeb Development & Design ForumC#C#GeneralGeneralArraysArrays
Previous Previous
 
Next
 Disabled
New Post
 9/7/2010 4:15 PM
 
Arrays 
Arrays are a way to manage groups of similarly typed values or objects. With arrays, you can group a series of variables and refer to them with an index. You can loop trough all or part of the variables and examine or affect each in turn. You also can create arrays with multiple dimensions. Arrays in .NET Framework have built-in functionality to facilitate many tasks.

Declaring and Initialising Arrays

Arrays can be declared and initialized in the some statement. When declaring an array in this manner, you must specify the type and number of the array elements. All arrays are zero-based – meaning the index of the first element is zero – and numbered sequentially. When declaring an array, you must indicate the number of array elements by specifying the number of elements in the array. Thus, the upper bound of an array is always one less than the number used in the declaration statement.
Example
//This line declares and initialise an array of 32 integers
//with indexes ranging from 0 to 31
int[] myIntegers = new int[32];
Arrays can be declared and initialised in separate steps. You can declare an array with one line and dynamically allocate it in another line.
Example
//This line delares
int[] myIntegers;
//This line initialises the array with 32 memebers
myIntegers = new int[32];
You can redefine your array to change its size at runtime. Redefinition is as simple as reinitialising the array as follows:
//This line delares and initialises th array
int[] myIntegers = new int[32];
//This line reinitialises the array
myIntegers = new int[11];
In the previous example, any data contained within an array is lost when the array is reinitialised. There is no way to preserve data when reinitializing arrays.
When creating an array of reference types, declaring and initialising an array does not create an array filled with member of that type. Rather, it creates an array of null references that can point to that type. To fill the array with members, you must assign each variable in the array to an object, which can be either a new object or an existing object.
Example
//This example create an array of Widgets then assigns each variable
// to a Widget object
Widget[] Widget = new Wiidget[11];
//Assigns Widget [0] to a new Widget object
Widgets[0] = new Widget();
//Assigns Widget[1] to an existing Widget object
Widget aWidget = new Widget();
Widget[1] = aWidget;
//Loops through widgets and assigns 2 through 10 to a new object
for (int Counter = 2; Counter < 11; Counter++)
{
Widgets[Counter] = new Widget();
}
Previous Previous
 
Next
 Disabled
Web Development & Design ForumWeb Development & Design ForumC#C#GeneralGeneralArraysArrays

spacer