Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use the GetLowerBound method of array class in C#
The GetLowerBound() method of the Array class in C# returns the lower bound of the specified dimension in an array. For most arrays in C#, the lower bound is typically 0, but this method becomes useful when working with arrays that have custom bounds or multi-dimensional arrays.
Syntax
Following is the syntax for the GetLowerBound() method −
public int GetLowerBound(int dimension);
Parameters
- dimension − A zero-based dimension of the array whose lower bound needs to be found.
Return Value
Returns an integer representing the lower bound of the specified dimension.
Using GetLowerBound with Single-Dimensional Arrays
For standard single-dimensional arrays, the lower bound is always 0 −
using System;
class Program {
static void Main(string[] args) {
Array arr = Array.CreateInstance(typeof(string), 3);
arr.SetValue("Car", 0);
arr.SetValue("Truck", 1);
arr.SetValue("Motorbike", 2);
Console.WriteLine("Lower Bound: {0}", arr.GetLowerBound(0));
Console.WriteLine("Upper Bound: {0}", arr.GetUpperBound(0));
Console.WriteLine("Array Length: {0}", arr.Length);
}
}
The output of the above code is −
Lower Bound: 0 Upper Bound: 2 Array Length: 3
Using GetLowerBound with Multi-Dimensional Arrays
The method is more useful with multi-dimensional arrays where you need to check bounds for different dimensions −
using System;
class Program {
static void Main(string[] args) {
int[,] matrix = new int[3, 4] {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
Console.WriteLine("Dimension 0 - Lower Bound: {0}, Upper Bound: {1}",
matrix.GetLowerBound(0), matrix.GetUpperBound(0));
Console.WriteLine("Dimension 1 - Lower Bound: {0}, Upper Bound: {1}",
matrix.GetLowerBound(1), matrix.GetUpperBound(1));
Console.WriteLine("Array Rank (dimensions): {0}", matrix.Rank);
}
}
The output of the above code is −
Dimension 0 - Lower Bound: 0, Upper Bound: 2 Dimension 1 - Lower Bound: 0, Upper Bound: 3 Array Rank (dimensions): 2
Using GetLowerBound with Arrays Having Custom Bounds
You can create arrays with custom lower bounds using Array.CreateInstance() −
using System;
class Program {
static void Main(string[] args) {
// Create array with custom bounds: starts at index 5, length 3
Array customArray = Array.CreateInstance(typeof(int), new int[] {3}, new int[] {5});
customArray.SetValue(100, 5);
customArray.SetValue(200, 6);
customArray.SetValue(300, 7);
Console.WriteLine("Lower Bound: {0}", customArray.GetLowerBound(0));
Console.WriteLine("Upper Bound: {0}", customArray.GetUpperBound(0));
Console.WriteLine("Length: {0}", customArray.Length);
// Display values
for (int i = customArray.GetLowerBound(0); i <= customArray.GetUpperBound(0); i++) {
Console.WriteLine("Index {0}: {1}", i, customArray.GetValue(i));
}
}
}
The output of the above code is −
Lower Bound: 5 Upper Bound: 7 Length: 3 Index 5: 100 Index 6: 200 Index 7: 300
Comparison with Related Methods
| Method | Purpose | Return Type |
|---|---|---|
GetLowerBound(dimension) |
Gets the lower bound of specified dimension | int |
GetUpperBound(dimension) |
Gets the upper bound of specified dimension | int |
GetLength(dimension) |
Gets the length of specified dimension | int |
Rank |
Gets the number of dimensions | int |
Conclusion
The GetLowerBound() method is essential for determining the starting index of array dimensions, especially when working with multi-dimensional arrays or arrays with custom bounds. It helps write robust code that can handle arrays with varying index ranges safely.
