Summary

   


In this chapter, you learned about the fundamentals of declaring, creating, and working with one-dimensional arrays.

The essential points discussed in this chapter are reviewed in this section.

Arrays can be useful for representing and accessing a small or a large number of similarly typed data items. Regardless of the number of items an array represents, it only needs one or two lines to be declared, and a loop construct can easily access different array element by referencing a single array name with a varying index.

Loop constructs a highly suited to traverse arrays.

When an array is created, its elements are initially and automatically assigned a default value. This value depends on the array element type.

After an array has been created, its length cannot be altered; this is one of the reasons why arrays sometimes are not the preferred means to represent a small or a large number of data items. In those cases, collection types with the ability to alter their length dynamically during runtime are called needed.

A reference is referencing the null object if it does not reference any particular other object. Any reference type can reference the null object.

The array has a zero-based index, meaning that the first array element has the index zero. It is possible to make your program pretend that the first index is 1 (or any other index you might find convenient).

If you attempt to access an array element with an index smaller than zero or larger than the array's length minus one, the runtime will generate an IndexOutOfRangeException.

It is possible to specify the initial values of an array's elements directly in the source code; the values must be enclosed by curly braces.

The traditional loop constructs (especially the for loop) provide tremendous power to the hands of the programmer, because the programmer has direct control over loop counters and the indexes used to access individual array elements. However, sometimes this detailed control is not necessary. In those cases, the foreach statement provides a handy and simple alternative to the traditional loop constructs (for, while, and do-while).

The array is a class type (System.Array) and therefore a reference type. This means that arrays follow the rules of any reference type. One array object can be referenced by several array references, and array references can be passed to methods as arguments and returned back to the caller from a method. The equals operator (==) support reference equality between array variables.

An array element, like any other variable, can be used as an argument in a method call.

If you assign an array reference variable to another array reference variable, you are merely assigning it a copy of the reference. After this assignment, the two array variables will reference the same array object. If you want to create a totally new array instead, but with identical array elements, you can use the array's built-in Clone method.

Via an array (conventionally called args) of base type string, you can provide values to the program you are running from the command line by using command-line arguments.

Array elements can contain references to objects of any class type.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net