System Namespace
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.
Visibility |
Name |
Parameters |
Return Type |
public static |
AsReadOnly |
(
T
array
)
|
ReadOnlyCollection`1
|
public static |
BinarySearch |
(
T
array
,
T
value
,
IComparer`1
comparer
)
|
Int32
|
public static |
BinarySearch |
(
T
array
,
Int32
index
,
Int32
length
,
T
value
)
|
Int32
|
public static |
BinarySearch |
(
Array
array
,
Int32
index
,
Int32
length
,
Object
value
,
IComparer
comparer
)
|
Int32
|
public static |
BinarySearch |
(
T
array
,
T
value
)
|
Int32
|
public static |
BinarySearch |
(
Array
array
,
Int32
index
,
Int32
length
,
Object
value
)
|
Int32
|
public static |
BinarySearch |
(
Array
array
,
Object
value
)
|
Int32
|
public static |
BinarySearch |
(
T
array
,
Int32
index
,
Int32
length
,
T
value
,
IComparer`1
comparer
)
|
Int32
|
public static |
BinarySearch |
(
Array
array
,
Object
value
,
IComparer
comparer
)
|
Int32
|
public static |
Clear |
(
Array
array
,
Int32
index
,
Int32
length
)
|
Void
|
public |
Clone |
( )
|
Object
|
public static |
ConstrainedCopy |
(
Array
sourceArray
,
Int32
sourceIndex
,
Array
destinationArray
,
Int32
destinationIndex
,
Int32
length
)
|
Void
|
public static |
ConvertAll |
(
TInput
array
,
Converter`2
converter
)
|
TOutput
|
public static |
Copy |
(
Array
sourceArray
,
Int32
sourceIndex
,
Array
destinationArray
,
Int32
destinationIndex
,
Int32
length
)
|
Void
|
public static |
Copy |
(
Array
sourceArray
,
Array
destinationArray
,
Int32
length
)
|
Void
|
public static |
Copy |
(
Array
sourceArray
,
Array
destinationArray
,
Int64
length
)
|
Void
|
public static |
Copy |
(
Array
sourceArray
,
Int64
sourceIndex
,
Array
destinationArray
,
Int64
destinationIndex
,
Int64
length
)
|
Void
|
public |
CopyTo |
(
Array
array
,
Int32
index
)
|
Void
|
public |
CopyTo |
(
Array
array
,
Int64
index
)
|
Void
|
public static |
CreateInstance |
(
Type
elementType
,
Int32
lengths
,
Int32
lowerBounds
)
|
Array
|
public static |
CreateInstance |
(
Type
elementType
,
Int32
length1
,
Int32
length2
)
|
Array
|
public static |
CreateInstance |
(
Type
elementType
,
Int32
length
)
|
Array
|
public static |
CreateInstance |
(
Type
elementType
,
Int32
length1
,
Int32
length2
,
Int32
length3
)
|
Array
|
public static |
CreateInstance |
(
Type
elementType
,
Int32
lengths
)
|
Array
|
public static |
CreateInstance |
(
Type
elementType
,
Int64
lengths
)
|
Array
|
public static |
Empty |
( )
|
T
|
public static |
Exists |
(
T
array
,
Predicate`1
match
)
|
Boolean
|
public static |
Find |
(
T
array
,
Predicate`1
match
)
|
T
|
public static |
FindAll |
(
T
array
,
Predicate`1
match
)
|
T
|
public static |
FindIndex |
(
T
array
,
Int32
startIndex
,
Int32
count
,
Predicate`1
match
)
|
Int32
|
public static |
FindIndex |
(
T
array
,
Predicate`1
match
)
|
Int32
|
public static |
FindIndex |
(
T
array
,
Int32
startIndex
,
Predicate`1
match
)
|
Int32
|
public static |
FindLast |
(
T
array
,
Predicate`1
match
)
|
T
|
public static |
FindLastIndex |
(
T
array
,
Int32
startIndex
,
Int32
count
,
Predicate`1
match
)
|
Int32
|
public static |
FindLastIndex |
(
T
array
,
Int32
startIndex
,
Predicate`1
match
)
|
Int32
|
public static |
FindLastIndex |
(
T
array
,
Predicate`1
match
)
|
Int32
|
public static |
ForEach |
(
T
array
,
Action`1
action
)
|
Void
|
public |
GetEnumerator |
( )
|
IEnumerator
|
public |
GetLength |
(
Int32
dimension
)
|
Int32
|
public |
GetLongLength |
(
Int32
dimension
)
|
Int64
|
public |
GetLowerBound |
(
Int32
dimension
)
|
Int32
|
public |
GetUpperBound |
(
Int32
dimension
)
|
Int32
|
public |
GetValue |
(
Int64
index1
,
Int64
index2
)
|
Object
|
public |
GetValue |
(
Int64
index
)
|
Object
|
public |
GetValue |
(
Int32
index1
,
Int32
index2
,
Int32
index3
)
|
Object
|
public |
GetValue |
(
Int64
index1
,
Int64
index2
,
Int64
index3
)
|
Object
|
public |
GetValue |
(
Int64
indices
)
|
Object
|
public |
GetValue |
(
Int32
index
)
|
Object
|
public |
GetValue |
(
Int32
indices
)
|
Object
|
public |
GetValue |
(
Int32
index1
,
Int32
index2
)
|
Object
|
public static |
IndexOf |
(
Array
array
,
Object
value
,
Int32
startIndex
)
|
Int32
|
public static |
IndexOf |
(
Array
array
,
Object
value
)
|
Int32
|
public static |
IndexOf |
(
T
array
,
T
value
,
Int32
startIndex
,
Int32
count
)
|
Int32
|
public static |
IndexOf |
(
T
array
,
T
value
,
Int32
startIndex
)
|
Int32
|
public static |
IndexOf |
(
T
array
,
T
value
)
|
Int32
|
public static |
IndexOf |
(
Array
array
,
Object
value
,
Int32
startIndex
,
Int32
count
)
|
Int32
|
public |
Initialize |
( )
|
Void
|
public static |
LastIndexOf |
(
T
array
,
T
value
)
|
Int32
|
public static |
LastIndexOf |
(
T
array
,
T
value
,
Int32
startIndex
)
|
Int32
|
public static |
LastIndexOf |
(
Array
array
,
Object
value
,
Int32
startIndex
)
|
Int32
|
public static |
LastIndexOf |
(
Array
array
,
Object
value
)
|
Int32
|
public static |
LastIndexOf |
(
T
array
,
T
value
,
Int32
startIndex
,
Int32
count
)
|
Int32
|
public static |
LastIndexOf |
(
Array
array
,
Object
value
,
Int32
startIndex
,
Int32
count
)
|
Int32
|
public static |
Resize |
(
T[]&
array
,
Int32
newSize
)
|
Void
|
public static |
Reverse |
(
Array
array
)
|
Void
|
public static |
Reverse |
(
Array
array
,
Int32
index
,
Int32
length
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int32
index
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int32
indices
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int32
index1
,
Int32
index2
,
Int32
index3
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int32
index1
,
Int32
index2
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int64
index
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int64
indices
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int64
index1
,
Int64
index2
,
Int64
index3
)
|
Void
|
public |
SetValue |
(
Object
value
,
Int64
index1
,
Int64
index2
)
|
Void
|
public static |
Sort |
(
T
array
,
IComparer`1
comparer
)
|
Void
|
public static |
Sort |
(
TKey
keys
,
TValue
items
,
IComparer`1
comparer
)
|
Void
|
public static |
Sort |
(
T
array
,
Int32
index
,
Int32
length
)
|
Void
|
public static |
Sort |
(
TKey
keys
,
TValue
items
,
Int32
index
,
Int32
length
)
|
Void
|
public static |
Sort |
(
T
array
,
Comparison`1
comparison
)
|
Void
|
public static |
Sort |
(
Array
keys
,
Array
items
)
|
Void
|
public static |
Sort |
(
T
array
,
Int32
index
,
Int32
length
,
IComparer`1
comparer
)
|
Void
|
public static |
Sort |
(
TKey
keys
,
TValue
items
,
Int32
index
,
Int32
length
,
IComparer`1
comparer
)
|
Void
|
public static |
Sort |
(
TKey
keys
,
TValue
items
)
|
Void
|
public static |
Sort |
(
Array
keys
,
Array
items
,
Int32
index
,
Int32
length
)
|
Void
|
public static |
Sort |
(
Array
array
,
IComparer
comparer
)
|
Void
|
public static |
Sort |
(
Array
array
)
|
Void
|
public static |
Sort |
(
Array
array
,
Int32
index
,
Int32
length
)
|
Void
|
public static |
Sort |
(
Array
keys
,
Array
items
,
Int32
index
,
Int32
length
,
IComparer
comparer
)
|
Void
|
public static |
Sort |
(
T
array
)
|
Void
|
public static |
Sort |
(
Array
keys
,
Array
items
,
IComparer
comparer
)
|
Void
|
public static |
Sort |
(
Array
array
,
Int32
index
,
Int32
length
,
IComparer
comparer
)
|
Void
|
public static |
TrueForAll |
(
T
array
,
Predicate`1
match
)
|
Boolean
|
|
The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly from the Array class. Users should use the array constructs provided by the language.
An element is a value in an Array. The length of an Array is the total number of elements it can contain. The rank of an Array is the number of dimensions in the Array. The lower bound of a dimension of an Array is the starting index of that dimension of the Array; a multidimensional Array can have different bounds for each dimension.
Type objects provide information about array type declarations. Array objects with the same array type share the same Type object.
Type.IsArray and Type.GetElementType might not return the expected results with Array because if an array is cast to the type Array, the result is an object, not an array. That is, typeof ( System.Array ).IsArray returns false, and typeof ( System.Array ).GetElementType returns a null reference ( Nothing in Visual Basic ).
Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.
The Array.Copy method copies elements not only between arrays of the same type but also between standard arrays of different types; it handles type casting automatically.
The following example creates and initializes an Array and displays its properties and its elements.
public static void createArray ( ) {
// creates and initializes a new three-dimensional Array of type Int32.
Array myArr = Array.CreateInstance ( typeof ( Int32 ), 2, 3, 4 );
for ( int i = myArr.GetLowerBound ( 0 ); i <= myArr.GetUpperBound ( 0 ); i++ ) {
for ( int j = myArr.GetLowerBound ( 1 ); j <= myArr.GetUpperBound ( 1 ); j++ ) {
for ( int k = myArr.GetLowerBound ( 2 ); k <= myArr.GetUpperBound ( 2 ); k++ ) {
myArr.SetValue ( ( i*100 ) + ( j*10 ) +k, i, j, k );
}
}
}
// Displays the properties of the Array.
Response.Write ( "The Array has " + myArr.Rank +
dimension (s) and a total of " + myArr.Length + " elements.",
Response.Write ( " Length Lower Upper" );
for ( int i = 0; i < myArr.Rank; i++ ) {
Response.Write ( l + ": " + myArr.GetLength ( l ) );
Response.Write ( myArr.GetLowerBound ( l ) + " " + myArr.GetUpperBound ( l ) );
}
// displays the contents of the Array.
Response.Write ( "The Array contains the following values:" );
printValues ( myArr );
}
public static void printValues ( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator ( );
int i = 0;
int cols = myArr.GetLength ( myArr.Rank - 1 );
while ( myEnumerator.MoveNext ( ) ) {
if ( i < cols ) {
i++;
} else {
i = 1;
}
Response.Write ( myEnumerator.Current );
}
}
Public Shared Sub createArray ( )
' creates and initializes a new three-dimensional Array of type Int32.
Dim myArr As Array = Array.CreateInstance ( GetType ( Int32 ), 2, 3, 4 )
Dim i As Integer
For i = myArr.GetLowerBound ( 0 ) To myArr.GetUpperBound ( 0 )
Dim j As Integer
For j = myArr.GetLowerBound ( 1 ) To myArr.GetUpperBound ( 1 )
Dim k As Integer
For k = myArr.GetLowerBound ( 2 ) To myArr.GetUpperBound ( 2 )
myArr.SetValue ( i * 100 + j * 10 + k, i, j, k )
Next k
Next j
Next i
' displays the properties of the Array.
Response.Write ( "The Array has " + myArr.Rank + _
dimension (s) and a total of " + myArr.Length + " elements.",
Response.Write ( " Length Lower Upper" )
For i = 0 To myArr.Rank - 1
Response.Write ( l + ": " + myArr.GetLength ( l ) )
Response.Write ( myArr.GetLowerBound ( l ) + " " + myArr.GetUpperBound ( l ) )
Next i
' displays the contents of the Array.
Response.Write ( "The Array contains the following values:" )
printValues ( myArr )
End Sub
Public Shared Sub printValues ( myArr As Array )
Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator ( )
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength ( myArr.Rank - 1 )
While myEnumerator.MoveNext ( )
If i < cols Then
i += 1
Else
i = 1
End If
Response.Write ( myEnumerator.Current )
End While
End Sub
public static function createArray ( ) {
// creates and initializes a new three-dimensional Array of type Int32.
var myArr : System.Array = System.Array.CreateInstance ( Int32, 2, 3, 4 );
for ( var i : int = myArr.GetLowerBound ( 0 ); i <= myArr.GetUpperBound ( 0 ); i++ ) {
for ( var j : int = myArr.GetLowerBound ( 1 ); j <= myArr.GetUpperBound ( 1 ); j++ ) {
for ( var k : int = myArr.GetLowerBound ( 2 ); k <= myArr.GetUpperBound ( 2 ); k++ ) {
myArr.SetValue ( Int32 ( ( i*100 ) + ( j*10 ) +k ), i, j, k );
}
}
}
// displays the properties of the Array.
Response.Write ( "The Array has " + myArr.Rank +
dimension (s) and a total of " + myArr.Length + " elements.",
Response.Write ( " Length Lower Upper" );
for ( var l : int = 0; l < myArr.Rank; l++ ) {
Response.Write ( l + ": " + myArr.GetLength ( l ) );
Response.Write ( myArr.GetLowerBound ( l ) + " " + myArr.GetUpperBound ( l ) );
}
// displays the contents of the Array.
Response.Write ( "The Array contains the following values:" );
printValues ( myArr );
}
public static function printValues ( myArr : System.Array ) {
var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator ( );
var i : int = 0;
var cols : int = myArr.GetLength ( myArr.Rank - 1 );
while ( myEnumerator.MoveNext ( ) ) {
if ( i < cols ) {
i++;
} else {
i = 1;
}
Response.Write ( myEnumerator.Current );
}
} |
|
C# |
VB |
JScript |
The obove code produces the following output.
The Array has 3 dimension ( s ) and a total of 24 elements.
Length Lower Upper
0: 2 0 1
1: 3 0 2
2: 4 0 3
The Array contains the following values:
0 1 2 3
10 11 12 13
20 21 22 23
100 101 102 103
110 111 112 113
120 121 122 123
|
|