Arrays


Arrays are discussed in Chapter 5, “Arrays.” The Array class is always behind the scenes of .NET arrays. When C# was designed, C# learned the bracket syntax for arrays from C++ and extended it with array initializers.

  // C# int[] arr1 = new int[3] {1, 2, 3}; int[] arr2 = {1, 2, 3}; 

If you use brackets with C++/CLI, you create a native C++ array but not an array that is based on the Array class. To create .NET arrays, C++/CLI introduced the array keyword. This keyword uses a generic-like syntax with angle brackets. Within the angle brackets the type of the elements is defined. C++/CLI supports array initializers with the same syntax as C#.

  // C++/CLI array<int>^ arr1 = gcnew array<int>(3) {1, 2, 3}; array<int>^ arr2 = {1, 2, 3}; 

Visual Basic uses braces for arrays. Visual Basic requires the last element number instead of the number of elements with the array declaration. With every .NET language, arrays begin with element number 0. This is also the same for Visual Basic. To make that clearer, Visual Basic 9 introduced the 0 To number expression with the array declaration. It always starts with 0, 0 To just makes this more readable.

Visual Basic also supports array initalizers if the array is initialized with the New operator.

  ' Visual Basic Dim arr1(0 To 2) As Integer() Dim arr2 As Integer() = New Integer(0 To 2) {1, 2, 3}; 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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