System.Array.GetValue Method

Gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.

Syntax

public object GetValue (int index)

Parameters

index
A 32-bit integer that represents the position of the Array element to get.

Returns

The value at the specified position in the one-dimensional Array.

Exceptions

TypeReason
ArgumentExceptionThe current instance has more than one dimension.
IndexOutOfRangeException index is outside the range of valid indices for the current instance.

Remarks

The Array.GetLowerBound(int) and Array.GetUpperBound(int) methods can determine whether the value of index is out of bounds.

This method is an O(1) operation.

Example

This example demonstrates the Array.GetValue(Int32[]) method.

C# Example

using System;
public class ArrayGetValueExample {
   public static void Main() {
      String[] strAry = { "one", "two", "three", "four", "five" };
      Console.Write( "The elements of the array are: " );
      for( int i = 0; i < strAry.Length; i++ )
         Console.Write( " '{0}' ", strAry.GetValue( i ) );
   }
}
   

The output is

The elements of the array are: 'one' 'two' 'three' 'four' 'five'

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0