Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Modify Array Values: Direct Assignment and Alternatives

Learn how to modify array values in C# using direct indexing, Array.Copy, Span<T>, and List<T>, including performance tradeoffs and common pitfalls.

~3 min readC#
#C# arrays#array indexing#Span<T>#List<T>
Diagram showing direct assignment to an array element in C# with alternatives like Span<T> and List<T>.

Modifying array values in C# is a routine task when you're working with collections of data. The simplest method is direct assignment through the indexer, but other approaches better suit specific scenarios such as bulk updates, high-performance code, or dynamic resizing.

Direct Assignment with the Indexer

The most straightforward way to change an element is to assign a new value to a specific index. Arrays in C# are zero-based, so the first element is at index 0.

csharp
int[] numbers = { 1, 2, 3, 4, 5 }; numbers[2] = 30; // numbers becomes { 1, 2, 30, 4, 5 }

This works for both value types and reference types. For reference types, the assignment replaces the reference stored in the array slot, not the object itself. To modify a property of an object stored in an array, access the object through the array element expression and set the property.

csharp
class Point { public int X; public int Y; } Point[] points = { new Point { X = 1, Y = 2 } }; points[0].X = 10; // modifies the object's property

The indexer assignment is O(1) and does not allocate additional memory, making it the fastest way to modify a single element.

Modifying Multiple Elements with Array.Copy

When you need to copy a range of values from one array to another, Array.Copy handles the bulk copy efficiently. It can copy elements between two arrays, or within the same array; overlapping source and destination ranges are handled as if the original source values were copied to a temporary location first.

csharp
int[] source = { 10, 20, 30, 40, 50 }; int[] destination = new int[source.Length]; Array.Copy(source, 1, destination, 0, 3); // copies 20, 30, 40

This is useful when you are reshaping data or shifting elements. Array.Copy cannot set every element to a specific value; for that, use a loop or Array.Fill.

Using Span<T> for Efficient In-Place Modification

Span<T> provides a type-safe and memory-safe view over a contiguous region of memory, including arrays. It is useful when you want to modify a slice of an array without allocating a new array or copying data.

csharp
int[] data = { 1, 2, 3, 4, 5, 6 }; Span<int> slice = data.AsSpan(1, 4); // elements 2,3,4,5 slice[0] = 20; // modifies data[1] directly

Span<T> is a ref struct, so it cannot be used in async methods or as a field in a class. For synchronous, performance-critical code, it provides zero-copy access to array slices.

Converting Arrays to Lists for Flexible Modification

If you need to insert or remove elements, an array is not the right structure because its size is fixed. Converting the array to a List<T> lets you add, remove, and insert elements, and convert back to an array when needed.

csharp
int[] original = { 1, 2, 3 }; List<int> list = original.ToList(); list.Add(4); list.RemoveAt(0); int[] modified = list.ToArray(); // { 2, 3, 4 }

This approach allocates a new list and then a new array, so it is less efficient than in-place modification. Use it only when the collection size must change dynamically.

Performance Considerations for Array Mutation

Direct index assignment is the fastest way to modify a single element because it does not allocate and has constant time complexity. Array.Copy is optimized at the runtime level and can be faster than a manual loop for large ranges. Span<T> avoids creating intermediate arrays for slicing operations. Converting to List<T> involves allocation and copying, so reserve it for scenarios where dynamic resizing is required.

When modifying arrays in a hot path, measure the actual impact. Differences are often negligible for small arrays, but avoiding unnecessary copies can matter for large arrays.

Common Pitfalls When Modifying Arrays

One common mistake is accessing an index outside the array's bounds, which throws an IndexOutOfRangeException. Always validate the index when it comes from user input or a computed value.

Another issue arises when you store value types in an array and expect modifications to persist. For example, modifying a struct's field through an array element expression works because the expression addresses the actual storage slot. However, if you copy the element to a local variable, changes to that variable do not affect the array.

csharp
struct Point { public int X; } Point[] pts = { new Point { X = 1 } }; Point p = pts[0]; p.X = 5; // pts[0].X remains 1

For reference types, the array stores references, so modifying an object's properties through the reference works as expected. Assigning a new object to an index changes the reference, not the original object.

Understanding these behaviors helps you avoid subtle bugs when working with arrays in C#.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.