C# | Finding the index of last element in the array Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: CSharp // C# program to illustrate the GetUpperBound(Int32) // method in 1-D array using System; public class GFG { // Main method static public void Main() { // 1-D Array int[] value = {1, 2, 3, 4, 5, 6, 7}; // Get the index of the last element // in the given Array by using // GetUpperBound(Int32) method int myvalue = value.GetUpperBound(0); Console.WriteLine("Index: {0}", myvalue); } } Output: Index: 6 Example 2: CSharp // C# program to find last index // value and rank of 2-D array using System; public class GFG { // Main method static public void Main() { // 2-D char Array char[, ] value = { { 'a', 'b' }, { 'c', 'd' }, { 'e', 'f' }, { 'g', 'h' }, { 'i', 'j' } }; // Get the index of the last element // and the rank of the given Array 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 Comment More infoAdvertise with us Next Article C# | Finding the index of last element in the array K Kirti_Mangal Follow Improve Article Tags : C# CSharp-Arrays CSharp-method Similar Reads C# | Array.LastIndexOf<T>(T[], T) Method Array.LastIndexOf<T>(T[], T) Method is used to search for the specified object. It returns the index of the last occurrence within the entire Array. Syntax: public static int LastIndexOf<T>(T[] array, T value); Parameters: array: It is a one-dimensional, zero-indexed array to search. val 2 min read C# | Array.FindLast() Method This method is used to search for an element that matches the conditions defined by the specified predicate and returns the last occurrence within the entire Array. Syntax: public static T FindLast<T> (T[] array, Predicate<T> match); Parameters: array: It is the one-dimensional, zero-bas 3 min read Array.LastIndexOf Method in C# | Set - 1 Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexO 8 min read Array.LastIndexOf Method in C# | Set â 2 Array.LastIndexOf method is used to find the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the over 4 min read C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe 8 min read Get first and last elements from Array and Vector in CPP Given an array, find first and last elements of it. Input: {4, 5, 7, 13, 25, 65, 98} Output: First element: 4 Last element: 98 In C++, we can use sizeof operator to find number of elements in an array. CPP // C++ Program to print first and last element in an array #include <iostream> using nam 2 min read Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu 6 min read C++ Program for Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu 2 min read C++ Program to Find Index of First Occurrence of a Value in Array In C++, an array is a data structure that stores elements of the same type in contiguous memory locations. In this article, we will learn how to find the index of the first occurrence of a specific value in an array in C++. Example: Input: int arr[] = {5, 7, 1, 2, 3, 7, 1} Target = 1Output: The firs 2 min read Maximum Size of an Array in C Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the dat 4 min read Like