WorldCoding WorldCoding -4 years ago 47
C# Question

How to find how many indices are in the array and length in C#?

I was working on this code find from SOURCE

float[, ] tempsGrid = new float[4, 3];

for (int x = 0; x < tempsGrid.GetLength(0); x++)
{
for (int y = 0; y < tempsGrid.GetLength(1); y++)
{
tempsGrid[x, y] = x + 10 * y;

}
}

for (int x = 0; x < tempsGrid.GetLength(0); x++)
{
for (int y = 0; y < tempsGrid.GetLength(1); y++)
{
Console.Write(tempsGrid[x, y] + ", ");
}
Console.WriteLine();
}


Above code is very simple just looking some extra feature to find length and indices in this example?

Answer Source

It's quite simple just use Length and Rank Properties to achieve that functionality.

    float[,] tempsGrid = new float[4, 3];

        //To find what is Length of this array?
        Console.WriteLine($"Length is {tempsGrid.Length}");

        //To find how many indices are in the array?
        Console.WriteLine($"Rank is {tempsGrid.Rank}");


        for (int x = 0; x < tempsGrid.GetLength(0); x++)
        {
            for (int y = 0; y < tempsGrid.GetLength(1); y++)
            {
                tempsGrid[x, y] = x + 10 * y;

            }
        }

        for (int x = 0; x < tempsGrid.GetLength(0); x++)
        {
            for (int y = 0; y < tempsGrid.GetLength(1); y++)
            {
                Console.Write(tempsGrid[x, y] + ", ");
            }
            Console.WriteLine();
        }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download