C# Array GetUpperBound

Description

Array GetUpperBound gets the index of the last element of the specified dimension in the array.

Syntax

Array.GetUpperBound has the following syntax.


public int GetUpperBound(
  int dimension
)

Parameters

Array.GetUpperBound has the following parameters.

  • dimension - A zero-based dimension of the array whose upper bound needs to be determined.

Returns

Array.GetUpperBound method returns The index of the last element of the specified dimension in the array, or -1 if the specified dimension is empty.

Example


/*w  ww .j a  v  a  2s  .  co m*/
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