Creating and Using an Array in C

Team-Fly    

 
.NET and COM Interoperability Handbook, The
By Alan Gordon
Table of Contents
Chapter Four.  A Quick Introduction to C#

Creating and Using an Array in C#

Working with Arrays in C# is simpler and less error-prone than C and C++. Arrays in C# are objects that support the properties shown in Table 4-8.

Table 4-8. A sample of the properties in System.Array

Property Name

Description

Length

Returns the number of elements in the array. If the array is multidimensional, it returns the total number of elements in all dimensions.

Rank

Returns the number of dimensions of the array, that is, the rank will be 2 for a 2D array.

The C# array class contains the methods shown in Table 4-9.

Table 4-9. A sample of the methods in System.Array

Method Name

Description

Clear

Sets a range of elements in the array to zero or a null reference.

Copy

Copies a portion of one array to another (deep copy).

Clone

Returns another reference to the array (a shallow copy).

GetLowerBound

Returns the lower bound of a specified dimension of the array.

GetUpperBound

Returns the upper bound of a specified dimension of the array.

GetValue

Returns the value at a specified index (or indexes in a multidimensional array) in an array. You can also use the index [] operator for this.

SetValue

Sets the value at a specified index (indices) you can also use the index "[]" operator for this

IndexOf

Returns the first occurrence of a specified value in a one-dimensional array.

LastIndexOf

Returns the last occurrence of a specified value in a one-dimensional array.

Reverse

Reverses the order of the elements in a one-dimensional array.

Sort

Sorts the elements in a one-dimensional array. (Each element must support the IComparable interface.)

As you can see from looking at the methods and properties supported by the array class, arrays are full-featured collections in C#. You can perform a shallow or deep copy, search for the first or last occurrence of a specified value, reverse the order of the elements in the array, or sort the elements in the array. Using an array is similar to the way that you would use an array in C++. The following code demonstrates how to use arrays in C#:

 namespace ArrayApp {   using System;   using System.Text;   internal class Employee   {     public Employee(string name)     {       this.name=name;     }     public string name;   }   public class TestArray   {     public static int Main(string[] args)     {       // An array of value types       int[] intArray=new int[20];       // An array of reference types       Employee[] empArray=new Employee[2];       // An initialized array       string[] strArray= {"A", ".NET","Primer"};       int i;       for (i=0;i<20;i++)         intArray[i]=i;       empArray[0]=new Employee("Alan Gordon");       empArray[1]=new Employee("Tamber Gordon");       Console.WriteLine(intArray[1]);       Console.WriteLine(strArray[1]);       Console.WriteLine(empArray[1].name);       return 0;     }   } } 

You can also create multidimensional arrays in C#. The following example program demonstrates how to declare and use multidimensional arrays:

 namespace ArrayApp {   using System;   using System.Text;   internal class Employee   {     public Employee(string name)     {       this.name=name;     }     public string name;     }   public class TestArray   {     public static int Main(string[] args)      {       float[,,] fltArray=new float[2,3,4];       Employee[,] empArray=new Employee[2,3];       string[,] strArray= {         {"A", ".NET","Primer"},         {"COM","COM+","Primer"}};       int i, j, k;       for (i=0;i<2;i++)         for (j=0;j<3;j++)           for (k=0;k<3;k++)             fltArray[i,j,k]=               i*1.5f+2.5f*j+3.5f*k;       empArray[0,1]=new Employee("Alan Gordon");       empArray[1,1]=new Employee("Tamber Gordon");       Console.WriteLine(fltArray[1,1,1]);       Console.WriteLine(strArray[1,2]);       Console.WriteLine(empArray[1,1].name);       return 0;     }   } } 

Team-Fly    
Top
 


. Net and COM Interoperability Handbook
The .NET and COM Interoperability Handbook (Integrated .Net)
ISBN: 013046130X
EAN: 2147483647
Year: 2002
Pages: 119
Authors: Alan Gordon

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