
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - GetUpperBound() Method
The C# Array GetUpperBound() method is used to return a 32-bit integer representing index of the last element of the specified dimension in the array.
This method throws an IndexOutOfRangeException if the dimension is zero or if it is greater than or equal to the rank.
Syntax
Following is the syntax of the C# Array GetUpperBound() method −
public int GetUpperBound (int dimension);
Parameters
This method accepts a zero based dimension of the array whose upper bound needs to be determined −
Return value
This method returns the index of the last element of the specified dimension in the array, or -1 if the specified dimension is empty.
Example 1: Display Index of the Last Element
This is the basic example of the GetUpperBound() method to display the number of element present in the array −
using System; class Program { static void Main() { int[] Number = new int[] {1, 2, 3, 4, 5}; // Get the length int indx = Number.GetUpperBound(0); Console.WriteLine("Index of the last Element " + indx); } }
Output
Following is the output −
Index of the last Element 4
Example 2: Index of Last Element of String Array
Let's create an example that displays the index of the last element of the string array using theGetUpperBound() method −
using System; class Program { static void Main() { string[] Number = new string[] {"Aman", "Gupta", "tutorialspoint", "India"}; // Get the length int indx = Number.GetUpperBound(0); Console.WriteLine("Index of the last Element " + indx); } }
Output
Following is the output −
Index of the last Element 3
Example 3:Get Last Index of 2D-Array
Let's see another example of theGetUpperBound()method to display the last index of the both dimension [dimension-0 and dimension-1] −
using System; class Program { static void Main() { int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Get last index of dimension 0 int indx_r = matrix.GetUpperBound(0); // Get last index of dimension 1 int indx_c = matrix.GetUpperBound(1); Console.WriteLine($"Index - 0D: {indx_r}, Index - 1D: {indx_c}"); } }
Output
Following is the output −
Index - 0D: 2, Index - 1D: 2
Example 4: Get Last Index of Object
Let's see another version of the GetUpperBound() method. In this version, we display the last index of the array object.
using System; class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main() { Person[] people = { new Person { Name = "Aman Kumar", Age = 25 }, new Person { Name = "Rahul Kumar", Age = 26 }, new Person { Name = "Akash Gupta", Age = 26 } }; int indx = people.GetUpperBound(0); Console.WriteLine($"Last index of object: {indx}"); } }
Output
Following is the output −
Last index of object: 2