C # in-depth analysis of the Rank attribute and GetUpperBound Method

Source: Internet
Author: User

Rank attribute of Array:
Syntax: public int Rank {get;} to obtain the Rank (dimension) of the Array ).
The GetUpperBound Method for Array:
Syntax: public int GetUpperBound (int dimension) is used to obtain the upper limit of the specified dimension of Array.
--------------------------------------------------------------------------------
Example:
Copy codeThe Code is as follows: using System;
Public class SamplesArray {
Public static void Main (){
// Creates a new one-dimen1_array of type Int32.
Array my1DIntArray = Array. CreateInstance (typeof (Int32), 5 );
// Uses GetLowerBound and GetUpperBound in the for loop.
For (int I = my1DIntArray. GetLowerBound (0); I <= my1DIntArray. GetUpperBound (0); I ++)
My1DIntArray. SetValue (I + 1, I );
// Displays the bounds and values of the one-dimen=array.
Console. WriteLine ("One-dimen1_array :");
Console. WriteLine ("Rank/tLower/tUpper ");
Console. WriteLine ("{0}/t {1}/t {2}", 0, my1DIntArray. GetLowerBound (0), my1DIntArray. GetUpperBound (0 ));
Console. WriteLine ("Values :");
PrintValues (my1DIntArray );
Console. WriteLine ();
// Creates a new three-dimen1_array of type Int32.
Array my3DIntArray = Array. CreateInstance (typeof (Int32), 2, 3, 4 );
// Uses GetLowerBound and GetUpperBound in the for loop.
For (int I = my3DIntArray. GetLowerBound (0); I <= my3DIntArray. GetUpperBound (0); I ++)
For (int j = my3DIntArray. GetLowerBound (1); j <= my3DIntArray. GetUpperBound (1); j ++)
For (int k = my3DIntArray. GetLowerBound (2); k <= my3DIntArray. GetUpperBound (2); k ++ ){
My3DIntArray. SetValue (I * 100) + (j * 10) + k, I, j, k );
}
// Displays the bounds and values of the multidimen=array.
Console. WriteLine ("multidimen1_array :");
Console. WriteLine ("Rank/tLower/tUpper ");
For (int I = 0; I <my3DIntArray. Rank; I ++)
Console. WriteLine ("{0}/t {1}/t {2}", I, my3DIntArray. GetLowerBound (I), my3DIntArray. GetUpperBound (I ));
Console. WriteLine ("Values :");
PrintValues (my3DIntArray );
}
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 {
Console. WriteLine ();
I = 1;
}
Console. Write ("/t {0}", myEnumerator. Current );
}
Console. WriteLine ();
}
}
/*
This code produces the following output.
One-dimen1_array:
Rank Lower Upper
0 0 4
Values:
1 2 3 4 5
Multidimen1_array:
Rank Lower Upper
0 0 1
1 0 2
2 0 3
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
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.