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);
Call
What 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
Parameter
Description
index / indices
Required. One index per dimension.
Return Value
Return Value
Description
object
The 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);
}
}
Output
20
20
How It Works
Index 1. Second element is 20.
object first. GetValue’s return type is object.
Cast to int. Then you can use it as a number.
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));
}
}
Output
C
B
How It Works
Rank 2. Two indexes select row then column.
(1,0) → C. Second row, first column.
(0,1) → B. First row, second column.
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));
}
}
}
Output
4
5
6
How It Works
Array variable. No int[] indexer without casting.
Length works. Still reports element count.
GetValue(i). Rank-1 read by index.
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.