开发者

Find bounds of a array? C#

开发者 https://www.devze.com 2023-03-14 02:59 出处:网络
I was somewhat sur开发者_Go百科prised to see the below work coming from a C++ background. It obviously holds the bounds information at runtime, how can i get the first and 2nd bounds?

I was somewhat sur开发者_Go百科prised to see the below work coming from a C++ background. It obviously holds the bounds information at runtime, how can i get the first and 2nd bounds?

Reflection is welcome but not recommended.

    ff(new int[3, 4]);

    static void ff(int[,] a)
    {
        var aa = a[1, 2];
    }


int bound0 = a.GetUpperBound(0);
int bound1 = a.GetUpperBound(1);


You get the largest accessible index for dimension i with GetUpperBound(i).

You get the number of elements for dimension i with GetLength(i) or GetLongLength(i).

Dimensions start at 0;


Arrays in .net have a number of methods attached to them, amongst these are GetUpperBound and GetLowerBound:

var three = a.GetUpperBound(0); // contents is: 2
var four = a.GetUpperBound(1); // contents is: 3


Array.GetUpperBound Method gets the upper bound of the specified dimension in the Array.:

int bound0 = array.GetUpperBound(0);
int bound1 = array.GetUpperBound(1);


The usual way is GetLength(int dim) :

for (int row = 0; row < a.GetLength(0); row++)
   ...

For 1-dimensional arrays the Length property is used mostly.

0

精彩评论

暂无评论...
验证码 换一张
取 消