Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A Get the length of a slice from a multi-dimensional array

You can use Array.GetUpperBound method Gets the index of the last element of the specified dimension in the array. and Array.GetLowerBound one accordingly. var arr = new int[,]{ { 1, 2 ...

posted 2y ago by FoggyFinder‭

Answer
#1: Initial revision by user avatar FoggyFinder‭ · 2021-09-26T08:32:38Z (over 2 years ago)
You can use [Array.GetUpperBound](https://docs.microsoft.com/en-us/dotnet/api/system.array.getupperbound?view=net-5.0) method 

> Gets the index of the last element of the specified dimension in the array.

and [Array.GetLowerBound](https://docs.microsoft.com/en-us/dotnet/api/system.array.getlowerbound?view=net-5.0) one accordingly.

```csharp
var arr = new int[,]{
    { 1, 2 },
    { 23, 42 }
};

for (int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0); i++)
{
    for (int j = arr.GetLowerBound(0); j <= arr.GetUpperBound(1); j++)
    {
        Console.Write($"{arr[i, j],3} ");
    }
    Console.WriteLine();
}
```

The key difference between this method and `Array.GetLength` one is that `GetUpperBound` can be used in case when bounds of an array are arbitrary:

```csharp
var arrWithNonStandardBounds =
    Array.CreateInstance(
        typeof(ValueTuple<int, int>),
        new[] { 2, 2, },
        new int[] { -1, -1 }
    );

var n = arrWithNonStandardBounds.GetUpperBound(0);
var m = arrWithNonStandardBounds.GetUpperBound(1);

for (int i = arrWithNonStandardBounds.GetLowerBound(0); i <= n; i++)
{
    for (int j = arrWithNonStandardBounds.GetLowerBound(1); j <= m; j++)
    {
        arrWithNonStandardBounds.SetValue((i, j), i, j);
        Console.Write($"{arrWithNonStandardBounds.GetValue(i, j),5} ");
    }
    Console.WriteLine();
}
```

It's a rare case especially lately though.