GetUpperBound() Method is used to find the index of the last element of the specified dimension in the array.
Syntax:
public int GetUpperBound (int dimension);
Here, dimension is a zero-based dimension of the array whose upper bound needs to be determined.
Return Value:The return type of this method is System.Int32. This method returns the index of the last element of the specified dimension in the array Or -1 if the specified dimension is empty.
Exception: This method will give IndexOutOfRangeException if the value of dimension is less than zero, or equal or greater than Rank.
Note: GetUpperBound(0) returns the last index in the first dimension of the array, and GetUpperBound(Rank – 1) returns the last index of the last dimension of the array. This method is an O(1) operation.
Below programs illustrate the use of GetUpperBound() Method:
Example 1:
using System;
public class GFG {
static public void Main()
{
int[] value = {1, 2, 3, 4, 5, 6, 7};
int myvalue = value.GetUpperBound(0);
Console.WriteLine("Index: {0}", myvalue);
}
}
|
Example 2:
using System;
public class GFG {
static public void Main()
{
char[, ] value = { { 'a', 'b' }, { 'c', 'd' },
{ 'e', 'f' }, { 'g', 'h' },
{ 'i', 'j' } };
int myvalue = value.GetUpperBound(0);
Console.WriteLine("Dimension: {0}", value.Rank);
Console.WriteLine("Index: {0}", myvalue);
}
}
|
Output:
Dimension: 2
Index: 4
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.getupperbound?view=netcore-2.1
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Feb, 2019
Like Article
Save Article