C# Array GetValue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, GetValue reads one element from an array and returns it as object. You pass one index for 1D arrays, or several indexes for multi-dimensional arrays.

Typed arrays usually use arr[i] instead. GetValue matters when you hold a plain Array reference or need a rank-agnostic read.

This is an instance member of System.Array. You call it on an array variable with a dot, such as arr.GetValue(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.

Syntax

Call GetValue on the array instance. Cast the result to the element type.

C#
object o = nums.GetValue(2);
int x = (int)nums.GetValue(2);
string s = (string)grid.GetValue(1, 0);
CallWhat it does
nums.GetValue(2)Reads index 2 as object.
grid.GetValue(1, 2)Reads row 1, column 2 of a 2D array.

Pair with SetValue to write. Prefer arr[i] when the compile-time type is already T[].

Library declarations
C#
public object GetValue(int index);
public object GetValue(int index1, int index2);
public object GetValue(params int[] indices);

Parameters

ParameterDescription
index / indicesRequired. One index per dimension.

Return Value

Return ValueDescription
objectThe element boxed/cast as object. Cast back to the real type.

C# Examples

Example 1 — Read From a 1D Array

Fetch an int through GetValue and cast it.

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 10, 20, 30 };
        object raw = nums.GetValue(1);
        int value = (int)raw;

        Console.WriteLine(raw);
        Console.WriteLine(value);
    }
}

How It Works

  1. Index 1. Second element is 20.
  2. object first. GetValue’s return type is object.
  3. Cast to int. Then you can use it as a number.
  4. Same as nums[1]. Prefer indexer when type is known.

Example 2 — Read a Matrix Cell

Pass two indexes for a rectangular array.

C#
using System;

class Program {
    static void Main() {
        string[,] grid = {
            { "A", "B" },
            { "C", "D" }
        };

        Console.WriteLine(grid.GetValue(1, 0));
        Console.WriteLine(grid.GetValue(0, 1));
    }
}

How It Works

  1. Rank 2. Two indexes select row then column.
  2. (1,0) → C. Second row, first column.
  3. (0,1) → B. First row, second column.
  4. Works on Array. Useful when the variable is typed as Array.

Example 3 — Loop With GetValue

Iterate using Length and GetValue on a non-generic Array.

C#
using System;

class Program {
    static void Main() {
        Array raw = new int[] { 4, 5, 6 };
        for (int i = 0; i < raw.Length; i++) {
            Console.WriteLine(raw.GetValue(i));
        }
    }
}

How It Works

  1. Array variable. No int[] indexer without casting.
  2. Length works. Still reports element count.
  3. GetValue(i). Rank-1 read by index.
  4. Print object. Console writes the boxed ints.

Frequently Asked Questions

The element as object. Cast it to the real element type.
When you have a System.Array reference or need multi-index access in a uniform way.
Yes. Pass one index per dimension: GetValue(row, col).
An IndexOutOfRangeException (or ArgumentOutOfRangeException depending on API) is thrown.
Use SetValue.

Next: IndexOf()

Return the first index of a value, or -1 if it is missing with System.Array.

IndexOf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful