Home C# Arrays Methods ConvertAll() C# Array ConvertAll() Method Definition In C#, Array.ConvertAll builds a new array by running a converter function on each element. The output type can differ from the input type—for example int[] to string[].
The converter is often a lambda: x => x.ToString(). The source array is not modified.
This is a static member of System.Array. You call it with the class name, such as Array.ConvertAll(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax Pass the source array and a converter. Store the new result array.
string[] text = Array.ConvertAll(nums, x => x.ToString());
int[] lens = Array.ConvertAll(names, n => n.Length); Call What it does Array.ConvertAll(nums, x => x.ToString())Each int becomes its string form. Array.ConvertAll(names, n => n.ToUpper())Each name becomes uppercase.
FindAll filters (same type, fewer items). ConvertAll maps (same count, possibly new type).
Library declarations public static TOutput[] ConvertAll<TInput, TOutput>(
TInput[] array, Converter<TInput, TOutput> converter); Parameters Parameter Description array Required. Source TInput[]. converter Required. Function from TInput to TOutput.
Return Value Return Value Description TOutput[] New array with one converted element per source element.
C# Examples 1. ToString 2. Lengths 3. Scale Example 1 — Ints to Strings Convert each number to text for joining.
using System;
class Program {
static void Main() {
int[] nums = { 1, 2, 3 };
string[] text = Array.ConvertAll(nums, x => x.ToString());
Console.WriteLine(string.Join("-", text));
}
} How It Works New type. Result is string[].Same length. Three inputs → three outputs.Lambda map. ToString on each int.Join works. string.Join needs strings.Example 2 — Strings to Lengths Map each name to its character count.
using System;
class Program {
static void Main() {
string[] names = { "Ann", "Bob", "Carla" };
int[] lens = Array.ConvertAll(names, n => n.Length);
Console.WriteLine(string.Join(", ", lens));
}
} How It Works n.Length. Converter returns an int.Ann/Bob → 3. Carla → 5.Source intact. names still holds strings.Parallel array. lens[i] describes names[i].Example 3 — Scale Numbers Multiply every value to build a new int array.
using System;
class Program {
static void Main() {
int[] nums = { 2, 4, 5 };
int[] doubled = Array.ConvertAll(nums, x => x * 2);
Console.WriteLine("src: " + string.Join(", ", nums));
Console.WriteLine("dest: " + string.Join(", ", doubled));
}
} src: 2, 4, 5
dest: 4, 8, 10 How It Works Pure map. Each output is input × 2.Independent arrays. Changing dest later would not alter src.Same element type. TInput and TOutput can both be int.Vs ForEach. ForEach would not return doubled values.Frequently Asked Questions What does ConvertAll do? It creates a new array by converting each element with a converter function.
Does ConvertAll change the source array? No. It returns a new array.
How is ConvertAll different from FindAll? FindAll filters elements (same type). ConvertAll transforms each element (possibly new type) and keeps the same count.
Can input and output types differ? Yes. That is a main use case, such as int[] to string[].
Is the result Length the same? Yes. One output element per input element.
Next: Copy() Copy a range of elements from one array to another with System.Array.
Copy() tutorial → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion