c# Rank屬性與GetUpperBound方法的深入分析
更新時(shí)間:2013年06月08日 15:41:41 作者:
本篇文章是對(duì)c#中的Rank屬性與GetUpperBound方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
Array的Rank 屬性:
語法:public int Rank { get; } 得到Array的秩(維數(shù))。
Array而GetUpperBound 方法:
語法:public int GetUpperBound(int dimension) 用于獲取 Array 的指定維度的上限。
--------------------------------------------------------------------------------
示例:
using System;
public class SamplesArray {
public static void Main() {
// Creates a new one-dimensional 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-dimensional Array.
Console.WriteLine( "One-dimensional 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-dimensional 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 multidimensional Array.
Console.WriteLine( "Multidimensional 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-dimensional Array:
Rank Lower Upper
0 0 4
Values:
1 2 3 4 5
Multidimensional 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
*/
語法:public int Rank { get; } 得到Array的秩(維數(shù))。
Array而GetUpperBound 方法:
語法:public int GetUpperBound(int dimension) 用于獲取 Array 的指定維度的上限。
--------------------------------------------------------------------------------
示例:
復(fù)制代碼 代碼如下:
using System;
public class SamplesArray {
public static void Main() {
// Creates a new one-dimensional 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-dimensional Array.
Console.WriteLine( "One-dimensional 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-dimensional 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 multidimensional Array.
Console.WriteLine( "Multidimensional 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-dimensional Array:
Rank Lower Upper
0 0 4
Values:
1 2 3 4 5
Multidimensional 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
*/
相關(guān)文章
C#使用RabbitMQ發(fā)送和接收消息工具類的實(shí)現(xiàn)
RabbitMQ是一個(gè)消息的代理器,用于接收和發(fā)送消息,本文主要介紹了C#使用RabbitMQ發(fā)送和接收消息工具類的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12C#實(shí)現(xiàn)ProperTyGrid自定義屬性的方法
這篇文章主要介紹了C#實(shí)現(xiàn)ProperTyGrid自定義屬性的方法,主要通過接口ICustomTypeDescriptor實(shí)現(xiàn),需要的朋友可以參考下2014-09-09Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng) ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02基于Unity實(shí)現(xiàn)3D版2048游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Unity實(shí)現(xiàn)簡易的3D版2048游戲,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-02-02C#實(shí)現(xiàn)WebSocket協(xié)議客戶端和服務(wù)器websocket sharp組件實(shí)例解析
這篇文章主要介紹了C#實(shí)現(xiàn)WebSocket協(xié)議客戶端和服務(wù)器websocket sharp組件實(shí)例解析,包括websocket sharp組件的概念及使用方法,需要的朋友可以參考下2017-04-04c# 如何實(shí)現(xiàn)不同進(jìn)程之間的通信
這篇文章主要介紹了c# 如何實(shí)現(xiàn)不同進(jìn)程之間的通信,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-11-11