C# Array SetValue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, SetValue writes an element into an array at the index (or indexes) you provide. The value is passed as object and must be compatible with the array’s element type.

Prefer arr[i] = value for typed arrays. Use SetValue when working with Array or multi-dimensional indexes in a generic helper.

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

Syntax

Call SetValue with the value first, then the index or indexes.

C#
nums.SetValue(99, 1);
grid.SetValue("Z", 1, 2);
CallWhat it does
nums.SetValue(99, 1)Writes 99 at index 1.
grid.SetValue("Z", 0, 1)Writes Z at [0,1].

A wrong runtime type throws. Match the array’s element type.

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

Parameters

ParameterDescription
valueRequired. New element value (compatible type).
index / indicesRequired. One index per dimension.

Return Value

Return ValueDescription
voidThe element is updated in place.

C# Examples

Example 1 — Write Into a 1D Array

Replace one int and print the array.

C#
using System;

class Program {
    static void Main() {
        int[] nums = { 1, 2, 3 };
        nums.SetValue(99, 1);
        Console.WriteLine(string.Join(", ", nums));
    }
}

How It Works

  1. Value first. SetValue takes the new value, then the index.
  2. Index 1. Middle slot becomes 99.
  3. Others unchanged. 1 and 3 stay.
  4. Same as nums[1]=99. Indexer is fine for typed arrays.

Example 2 — Write a Matrix Cell

Set a string in a 2×2 grid.

C#
using System;

class Program {
    static void Main() {
        string[,] grid = {
            { "A", "B" },
            { "C", "D" }
        };
        grid.SetValue("Z", 1, 0);
        Console.WriteLine(grid[1, 0]);
        Console.WriteLine(grid.GetValue(1, 0));
    }
}

How It Works

  1. Two indexes. Row 1, column 0.
  2. Overwrite C. That cell becomes Z.
  3. Indexer agrees. grid[1,0] shows Z.
  4. GetValue agrees. Read API matches write API.

Example 3 — SetValue on System.Array

Mutate through a non-generic Array reference.

C#
using System;

class Program {
    static void Main() {
        Array raw = new string[] { "a", "b", "c" };
        raw.SetValue("X", 0);
        Console.WriteLine(raw.GetValue(0));
    }
}

How It Works

  1. No typed indexer. Variable type is Array.
  2. SetValue writes. Index 0 becomes X.
  3. GetValue reads. Confirms the change.
  4. Type must match. Setting an int into string[] would throw.

Frequently Asked Questions

It writes a value into the array at the given index or indexes.
System.Array is non-generic. The runtime checks that the value fits the element type.
Yes. Pass one index per dimension after the value.
You get an exception such as InvalidCastException or ArgumentException.
Yes when the compile-time type is already T[].

Next: Sort()

Sort the elements of an array in place (ascending by default) with System.Array.

Sort() 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.

9 people found this page helpful