
Void main ()
{
int intArray __gc[]= new int __gc[5];
for (int i=0; i<6; i++) //over the top!!!
{
try
{
intArray[i] = i;
}
catch (IndexOutOfRangeException *piore)
{
//should do something more useful to recover here
Console::WriteLine("Oooops!");
Console::WriteLine(piore->get_Message());
}
}
}
Рис. 6. Программа IndexOutOfRangeException.cpp
//Array1.cpp
#using <mscorlib.dll>
using namespace System;
Void main ()
{
//managed 1D array of int (using __gc)
Console::WriteLine("managed 1D array of int");
int intArray __gc[]= new int __gc[5];
for (int i=0; i<intArray->Length; i++)
{
intArray[i] = i;
Console::Write(intArray[i]);
Console::Write("\t");
}
Console::WriteLine();
//managed 2D array of Strings (using managed type)
Console::WriteLine("managed 2D array of Strings");
String *str2DArray[,] = new String *[2,3];
for(int row=0; row<str2DArray->GetLength(0); row++)
{
for(int col=0; col<str2DArray->GetLength(1); col++)
{
str2DArray[row,col] = (row*10 + col).ToString();
Console::Write(str2DArray[row,col]);
Console::Write("\t");
}
Console::WriteLine();
}
//unmanaged 2D array of int (for comparison)
Console::WriteLine("unmanaged 2D array of int");
int int2DArray[2][3];
for(int row=0; row<2; row++)
{
for(int col=0; col<3; col++)
{
int2DArray[row][col] = row*10 + col;
Console::Write(int2DArray[row][col]);
Console::Write("\t");
}
Console::WriteLine();
}
}
Рис. 7. Программа Array1.cpp
//Array2.cpp
#using <mscorlib.dll>
using namespace System;
Void main(void)
{
Console::WriteLine("Rectangular array using [,]");
int rect2DArray [,] = new int __gc [3,4]; //managed
for(int row=0; row< rect2DArray ->GetLength(0); row++)
{
for(int col=0; col< rect2DArray->GetLength(1); col++)
{
rect2DArray [row,col] = row*10 + col;
Console::Write(rect2DArray[row,col]);
Console::Write("\t");
}
Console::WriteLine();
}
Console::WriteLine("Array of arrays using [][]");
int arrayOfArray[3][4]; //unmanaged
for(int row=0; row<3 ; row++)
{
for(int col=0; col<4; col++)
{
arrayOfArray[row][col] = row*10 + col;
Console::Write(arrayOfArray[row][col]);
Console::Write("\t");
}
Console::WriteLine();
}
}
Рис. 8. Программа Array2.cpp
// Test1.cpp
// compile with: /clr
using namespace System;
#define ARRAY_SIZE 10
// Returns a managed array of Int32.
array<Int32>^ Test1() {
int i;
array< Int32 >^ local = gcnew array< Int32 >(ARRAY_SIZE);
for (i = 0 ; i < ARRAY_SIZE ; i++)
local[i] = i + 10;
return local;
}
int main() {
int i;
// Declares an array of value types and initializes with a function.
array< Int32 >^ IntArray;
IntArray = Test1();
for (i = 0 ; i < ARRAY_SIZE ; i++)
Console::WriteLine("IntArray[{0}] = {1}", i, IntArray[i]);
Console::WriteLine();
}
Рис. 9. Программа Test1.cpp
//Array1_05.cpp
#using <mscorlib.dll>
using namespace System;