Array

Array CF 1.0, ECMA 1.0, serializable

System (mscorlib.dll) abstract class

Unlike other environments (such as C++), .NET has arrays of first-class type, in that all array types are derivatives of the base type Array . All methods are available on any array type, regardless of its declaration. In fact, the CLR is required to synthesize a pseudotype that matches the declaration. Thus, when you declare a variable of type string[ ] , the CLR creates an anonymous type, deriving from Array specifically for storing Strings in a one-dimensional array.

The Array class has a number of useful array- related methods, such as checking for bounds violations (attempting to access an element of the array that isn't in the array's declared size) and retrieval of array length. In addition, because Array also implements the ICloneable , System.Collections.IList , System.Collections.ICollection , and System.Collections.IEnumerable interfaces, arrays can be used anywhere these interface types are expected.

Starting with .NET 1.1, Array now has support for 64-bit indexer values, meaning that an array can stretch to include 2 64 possible elements. As a result, several methods, including Copy( ) , GetValue( ) , and SetValue( ) , among others, take Int64 parameters as well as Int32 .

Arrays are reference types. This means that the statement ArrayB = ArrayA results in two objects that reference the same array. Use ArrayB = ArrayA.Clone( ) to create a duplicate copy of an array. This will be a shallow copy with identical references to subobjects. To create a deep copy in which each array has its own copy of subobjects, you must loop through the array and assign values manually.

The Array class also contains useful static methods. These include IndexOf( ) , which returns the offset of the first matching occurrence of an object in an array. For one-dimensional arrays, you can also use Reverse( ) to reverse a subset of rows, and Sort ( ) to sort a subset of rows (provided the objects in the array implement the IComparable interface). If the objects in the array do not implement that interface, you can implement System.Collections.IComparer in a custom class and pass an instance of it to Sort( ) .

The static Copy( ) method works like the C function memmove : it copies a portion of an array to a different position in the current array (or to a different array). When copying between multidimensional arrays, the array is treated like a long one-dimensional array in which each element occupies a separate row. (For example, if an array has three columns , copying five elements from the beginning of the array copies all three elements in the first row and the first two elements from the second row.) The source and destination ranges can overlap without causing a problem.

Note that you can create both multidimensional arrays and ragged arrays (arrays of arrays). Arrays are fixed in size and zero-based by default, although you can use the CreateInstance( ) method to create an array with a different lower bound. This is not recommended, as the array won't be CLS (Common Language Specification)-compliant. Lastly, if you need a dynamically resizable array, consider the collection System.Collections.ArrayList , which provides Add( ) and Remove( ) methods.

 public abstract class  Array  : ICloneable, IList, ICollection, IEnumerable {  // Public Instance Properties  public virtual bool  IsFixedSize  {get; }  // implements IList  public virtual bool  IsReadOnly  {get; }  // implements IList  public virtual bool  IsSynchronized  {get; }  // implements ICollection  public int  Length  {get; }    public long  LongLength  {get; }    public int  Rank  {get; }    public virtual object  SyncRoot  {get; }  // implements ICollection   // Public Static Methods  public static int  BinarySearch  (Array   array   , int   index   , int   length   , object   value   );    public static int  BinarySearch  (Array   array   , int   index   , int   length   , object   value   ,        System.Collections.IComparer   comparer   );    public static int  BinarySearch  (Array   array   , object   value   );    public static int  BinarySearch  (Array   array   , object   value   , System.Collections.IComparer   comparer   );    public static void  Clear  (Array   array   , int   index   , int   length   );    public static void  Copy  (Array   sourceArray   , Array   destinationArray   , int   length   );    public static void  Copy  (Array   sourceArray   , Array   destinationArray   , long   length   );    public static void  Copy  (Array   sourceArray   , int   sourceIndex   , Array   destinationArray   , int   destinationIndex   ,        int   length   );    public static void  Copy  (Array   sourceArray   , long   sourceIndex   , Array   destinationArray   , long   destinationIndex   ,        long   length   );    public static Array  CreateInstance  (Type   elementType   , int   length   );    public static Array  CreateInstance  (Type   elementType   , params int[ ]   lengths   );    public static Array  CreateInstance  (Type   elementType   , int[ ]   lengths   , int[ ]   lowerBounds   );    public static Array  CreateInstance  (Type   elementType   , int   length1   , int   length2   );    public static Array  CreateInstance  (Type   elementType   , int   length1   , int   length2   , int   length3   );    public static Array  CreateInstance  (Type   elementType   , params long[ ]   lengths   );    public static int  IndexOf  (Array   array   , object   value   );    public static int  IndexOf  (Array   array   , object   value   , int   startIndex   );    public static int  IndexOf  (Array   array   , object   value   , int   startIndex   , int   count   );    public static int  LastIndexOf  (Array   array   , object   value   );    public static int  LastIndexOf  (Array   array   , object   value   , int   startIndex   );    public static int  LastIndexOf  (Array   array   , object   value   , int   startIndex   , int   count   );    public static void  Reverse  (Array   array   );    public static void  Reverse  (Array   array   , int   index   , int   length   );    public static void  Sort  (Array   array   );    public static void  Sort  (Array   keys   , Array   items   );    public static void  Sort  (Array   keys   , Array   items   , System.Collections.IComparer   comparer   );    public static void  Sort  (Array   keys   , Array   items   , int   index   , int   length   );    public static void  Sort  (Array   keys   , Array   items   , int   index   , int   length   , System.Collections.IComparer   comparer   );    public static void  Sort  (Array   array   ,        System.Collections.IComparer   comparer   );    public static void  Sort  (Array   array   , int   index   , int   length   );    public static void  Sort  (Array   array   , int   index   , int   length   , System.Collections.IComparer   comparer   );  // Public Instance Methods  public virtual object  Clone  ( );  // implements ICloneable  public virtual void  CopyTo  (Array   array   , int   index   );  // implements ICollection  public virtual void  CopyTo  (Array   array   , long   index   );    public virtual IEnumerator  GetEnumerator  ( );  // implements IEnumerable  public int  GetLength  (int   dimension   );    public long  GetLongLength  (int   dimension   );    public int  GetLowerBound  (int   dimension   );    public int  GetUpperBound  (int   dimension   );    public object  GetValue  (int   index   );    public object  GetValue  (params int[ ]   indices   );    public object  GetValue  (int   index1   , int   index2   );    public object  GetValue  (int   index1   , int   index2   , int   index3   );    public object  GetValue  (long   index   );    public object  GetValue  (params long[ ]   indices   );    public object  GetValue  (long   index1   , long   index2   );    public object  GetValue  (long   index1   , long   index2   , long   index3   );    public void  Initialize  ( );    public void  SetValue  (object   value   , int   index   );    public void  SetValue  (object   value   , params int[ ]   indices   );    public void  SetValue  (object   value   , int   index1   , int   index2   );    public void  SetValue  (object   value   , int   index1   , int   index2   , int   index3   );    public void  SetValue  (object   value   , long   index   );    public void  SetValue  (object   value   , params long[ ]   indices   );    public void  SetValue  (object   value   , long   index1   , long   index2   );    public void  SetValue  (object   value   , long   index1   , long   index2   , long   index3   ); } 

Returned By

System.Collections.ArrayList.ToArray( ) , Enum.GetValues( )

Passed To

Multiple types



C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

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