In C#, List.ConvertAll(converter)maps every item of a list to something new. You give it a small function (a Converter<TInput, TOutput>) that takes one item and returns the converted item, usually as a lambda such as n => n.ToString().
The method returns a new list of the output type, with one converted item for each source item and in the same order. The original list is not changed. The new type can differ: List<int> in, List<string> out. It is the List version of LINQ's Select, and the instance version of Array.ConvertAll.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.ConvertAll(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass a lambda that returns the converted item. The compiler works out the output type from the lambda.
ConvertAll always returns a List<TOutput>, even for an empty source (an empty list). A null converter throws ArgumentNullException. If the converter throws (for example, int.Parse on bad text), the exception comes out of ConvertAll and no list is returned. LINQ Select(...).ToList() gives the same result and works on any collection.
Library declarations
C#
public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter);
Parameters
Parameter
Description
converter
Required. A Converter<T, TOutput>: a lambda or method that takes one item of type T and returns the converted item. It must not be null.
Return Value
Return Value
Description
List
A new list with the converted items, in the same order and with the same Count as the source. The source is unchanged.
C# Examples
Example 1 — Convert Numbers to Text
Make a list of labels from a list of numbers. The two lists have different item types.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3 };
List<string> labels = numbers.ConvertAll(n => "#" + n);
Console.WriteLine(string.Join(", ", labels));
Console.WriteLine(string.Join(", ", numbers));
Console.WriteLine(labels.Count);
}
}
Output
#1, #2, #3
1, 2, 3
3
How It Works
One call per item. The lambda n => "#" + n runs for 1, 2, and 3 and returns a string each time.
Type is worked out. The lambda returns a string, so the compiler knows the result is a List<string>. You did not have to write the type.
Source stays numbers. The second line shows the original List<int> was not modified.
Same size. Three items in, three items out. Order is kept.
Example 2 — Convert Text to Numbers
Pass a method name instead of a lambda. int.Parse takes a string and returns an int, so it fits.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> words = new List<string> { "10", "20", "30" };
List<int> numbers = words.ConvertAll<int>(int.Parse);
int total = 0;
foreach (int n in numbers) {
total += n;
}
Console.WriteLine(string.Join(" + ", numbers));
Console.WriteLine(total);
}
}
Output
10 + 20 + 30
60
How It Works
Method group.int.Parse has the right shape (string in, int out), so it can be passed as the converter, with no lambda.
Explicit type.ConvertAll<int> names the output type, which helps the compiler pick the right overload of Parse.
Now they are numbers. The loop can add the values, because they are int items and no longer text.
Bad text throws. A word like "abc" would make int.Parse throw FormatException. Use int.TryParse in your own loop when input may be bad.
Example 3 — Pull Names Out of Objects
Convert a list of objects into a list of one of their fields.
C#
using System;
using System.Collections.Generic;
class Person {
public string Name;
public int Age;
}
class Program {
static void Main() {
List<Person> people = new List<Person> {
new Person { Name = "Asha", Age = 30 },
new Person { Name = "Ben", Age = 17 }
};
List<string> names = people.ConvertAll(p => p.Name);
List<bool> adults = people.ConvertAll(p => p.Age >= 18);
Console.WriteLine(string.Join(", ", names));
Console.WriteLine(string.Join(", ", adults));
}
}
Output
Asha, Ben
True, False
How It Works
One field.p => p.Name returns just the name of each Person, so the result is a List<string>.
A computed value.p => p.Age >= 18 returns a bool for each person, so the result is a List<bool>.
Same order. Both result lists follow the order of people: Asha first, Ben second.
Strings, not objects. The new lists do not hold Person objects, so changing them cannot change the people.
Frequently Asked Questions
It creates a new list by running a converter function on every item of the list. The new list can have a different item type.
No. The source list stays as it is. You get a new List<TOutput>.
A Converter<TInput, TOutput> is a delegate: a function that takes one item and returns the converted item. In C# you usually write it as a lambda, such as n => n * 2.
ConvertAll is a List<T> method that returns a List<TOutput> right away. Select works on any collection and is lazy, so you need ToList() to get a list.
The exception comes out of ConvertAll and no new list is returned. For example, int.Parse on text that is not a number throws FormatException.