C# Array GetLowerBound

Description

Array GetLowerBound gets the index of the first element of the specified dimension in the array.

Syntax

Array.GetLowerBound has the following syntax.


public int GetLowerBound(
  int dimension
)

Parameters

Array.GetLowerBound has the following parameters.

  • dimension - A zero-based dimension of the array whose starting index needs to be determined.

Returns

Array.GetLowerBound method returns The index of the first element of the specified dimension in the array.

Example

The following example uses the GetLowerBound and GetUpperBound methods to display the bounds of a one-dimensional and two-dimensional array and to display the values of their array elements.


/*w  w w .jav a 2 s.com*/
using System;

public class Example
{
   public static void Main()  
   {
      int[] integers = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
      int upper = integers.GetUpperBound(0);
      int lower = integers.GetLowerBound(0);
      Console.WriteLine(lower);
      Console.WriteLine(upper);
      for (int ctr = lower; ctr <= upper; ctr++)
        Console.WriteLine(integers[ctr]);

      int[,] integers2d= { {2, 4}, {3, 9}, {4, 16}, {5, 25}, 
                           {6, 36}, {7, 49}, {8, 64}, {9, 81} }; 
      int rank = integers2d.Rank;  
      Console.WriteLine("Number of dimensions: {0}", rank);      
      for (int ctr = 0; ctr < integers2d.Rank - 1; ctr++)
        Console.WriteLine("   Dimension {0}: from {1} to {2}",
                          ctr, integers2d.GetLowerBound(ctr),
                          integers2d.GetUpperBound(ctr));
      Console.WriteLine("   Values of array elements:");
      for (int outer = integers2d.GetLowerBound(0); outer <= integers2d.GetUpperBound(0);outer++)
        for (int inner = integers2d.GetLowerBound(1); inner <= integers2d.GetUpperBound(1);inner++){
           Console.WriteLine("      {3}{0}, {1}{4} = {2}", outer, inner,integers2d.GetValue(outer, inner), "{", "}");
        }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version