Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Array.IndexOf() / Array.Copy()

Array.IndexOf() / Array.Copy()

Since: C# 1.0(2002)

The Array.IndexOf() method searches an array for a specified value, and the Array.Copy() method copies elements from one array to another.

Syntax

Array.IndexOf(T[] array, T value)

// Copies length elements from sourceArray starting at sourceIndex to destinationArray starting at destinationIndex.
Array.Copy(T[] sourceArray, int sourceIndex, T[] destinationArray, int destinationIndex, int length)

// Copies length elements from the beginning of sourceArray to the beginning of destinationArray.
Array.Copy(T[] sourceArray, T[] destinationArray, int length)

Method List

MethodDescription
Array.IndexOf(T[] array, T value)Returns the index of the first element in the array that matches value. Returns -1 if not found.
Array.IndexOf(T[] array, T value, int startIndex)Starts the search at startIndex and returns the index of the first matching element.
Array.Copy(Array src, Array dst, int length)Copies length elements from the beginning of src to the beginning of dst.
Array.Copy(Array src, int srcIdx, Array dst, int dstIdx, int length)Copies length elements from src starting at srcIdx to dst starting at dstIdx.

Sample Code

Use Array.IndexOf() to search for the position of an element. Returns -1 if the value is not found.

Program.cs
using System;

string[] languages = { "C#", "Java", "Python", "Go", "Rust" };
int index = Array.IndexOf(languages, "Python");
Console.WriteLine(index); // 2

int notFound = Array.IndexOf(languages, "Swift");
Console.WriteLine(notFound); // -1

string target = "Java";
if (Array.IndexOf(languages, target) != -1)
{
    Console.WriteLine($"{target} exists in the array.");
}

Run the following command:

dotnet run
2
-1
Java exists in the array.

Copying Arrays with Array.Copy()

Array.Copy() copies the contents of one array to another. Modifying the copied array does not affect the original (for value types).

CopySample.cs
using System;

int[] source = { 10, 20, 30, 40, 50 };
int[] dest = new int[source.Length];
Array.Copy(source, dest, source.Length);
dest[0] = 999;
Console.WriteLine(source[0]); // 10 (the original array is unchanged)
Console.WriteLine(dest[0]); // 999

// Partial copy (copy 3 elements starting at index 1 into dest starting at index 0)
int[] partial = new int[3];
Array.Copy(source, 1, partial, 0, 3);
Console.WriteLine(string.Join(", ", partial)); // 20, 30, 40

The following example demonstrates this:

dotnet run
10
999
20, 30, 40

Searching with a Start Index

The third argument startIndex of Array.IndexOf() lets you start the search from a specific position. This is useful when the same value appears multiple times and you want to find the next occurrence.

SearchSample.cs
using System;

string[] members = { "Kiryu Kazuma", "Majima Goro", "Kiryu Kazuma", "Akiyama Shun" };

int first = Array.IndexOf(members, "Kiryu Kazuma");
Console.WriteLine(first); // 0

int second = Array.IndexOf(members, "Kiryu Kazuma", first + 1);
Console.WriteLine(second); // 2

// List all occurrences
int pos = 0;
while ((pos = Array.IndexOf(members, "Kiryu Kazuma", pos)) != -1)
{
    Console.WriteLine($"Found at index {pos}.");
    pos++;
}

Run the following command:

dotnet run
0
2
Found at index 0.
Found at index 2.

Common Mistakes

Common Mistake: Array.Copy() Performs a Shallow Copy of Reference Types

Array.Copy() performs a shallow copy of reference-type elements. When copying an array that contains class instances, the copied elements reference the same objects as the original array. Modifying one will affect the other.

using System;

class Character
{
    public string Name;
    public int Level;
}

Character a = new Character { Name = "Kiryu Kazuma", Level = 50 };
Character[] original = { a };
Character[] copy = new Character[1];
Array.Copy(original, copy, 1);

copy[0].Level = 99; // modify the copy
Console.WriteLine(original[0].Level); // 99 (the original is also changed)

The following example demonstrates this:

dotnet run
99

This issue does not occur with value-type arrays (int, double, struct, etc.). To deeply copy reference types, instantiate each element individually with new, or use a serialize/deserialize approach.

Notes

Array.Copy() performs a shallow copy. If the array contains reference-type elements (such as class instances), the copied elements will reference the same objects as the original array. This is not an issue for value-type arrays (such as int or double).

To inspect the contents of an array, first check the element count using Length / Array.Resize(), then access elements within the valid index range.

If you find any errors or copyright issues, please .