navindren navindren - 3 months ago 9
C# Question

How to get the dimension of an array without using the built in function Rank in c#?

I am really curious to know, the method to get the dimension of the array below, without using the built in function Rank in c#.

string [,] random_array= new string[2, 2];

Answer

Are we allowed to cheat (and use really HORRIBLE code)?

public static int Rank(Array a)
{
    int rank = 0;

    while (true)
    {
        try
        {
            a.GetLength(rank++);
        }

        catch
        {
            return rank - 1;
        }
    }
}

Utterly pointless, of course.