C# Array - ConvertAll() Method



The C# Array ConvertAll() method is used to convert an array of one type into array of another type using a specified converter function.

Let's see an article related to ConvertAll method: C# Program to convert integer array to string array.

Exception

This method will throw an ArgumentNullException if the array is null or converter is null.

Syntax

Following is the syntax of the C# Array ConvertAll() method −

public static TOutput[] ConvertAll<TInput,TOutput> (TInput[] array, Converter<TInput,TOutput> converter);

Here in the above syntax, TInput and TOutput is the source and destination array respectively.

Parameters

This method accepts the following parameters −

  • array: A zero-based one-dimensional array to convert to a target type.
  • converter: A converter that converts each element from one type to another type.

Return value

This method returns an array of the target type, which contains the converted element from the source array.

Example 1: Convert Integer to String

Let us see a basic example of the CovertAll() method to convert integer to string −

using System;
class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };

      string[] strings = Array.ConvertAll(numbers, num => num.ToString());

      Console.WriteLine("Converted to strings:");
      foreach (string str in strings) {
         Console.WriteLine(str);
      }
   }
}

Output

Following is the output −

Converted to strings:
1
2
3
4
5

Example 2: Convert String to Integer

Let us see another example of theConvertAll()method to convert a string array into a integer array −

using System;
class Program {
   static void Main() {
      string[] stringNumbers = { "10", "20", "30", "40" };
      int[] integers = Array.ConvertAll(stringNumbers, str => int.Parse(str));

      Console.WriteLine("Converted to integers:");
      foreach (int number in integers) {
         Console.WriteLine(number);
      }
   }
}

Output

Following is the output −

one, two, three
changed, two, three

Example 3: Convert Custom Object to Another Type

Here, in this example we use the ConvertAll()method to convert the custom object into another type −

using System;
class Person {
   public string Name { get; set; }
}

class Program {
   static void Main() {
      Person[] people = {
         new Person { Name = "Aman" },
         new Person { Name = "Kumar" },
         new Person { Name = "Gupta" }
      };

      string[] names = Array.ConvertAll(people, person => person.Name);
      Console.WriteLine("Converted to names:");
      foreach (string name in names) {
         Console.WriteLine(name);
      }
   }
}

Output

Following is the output −

Converted to names:
Aman
Kumar
Gupta

Example 4: Convert Double to Integer

As we have converted string to integer and vice-versa, now in this example, we convert double to integer using theConvertAll()method −

using System;
class Program {
   static void Main() {
      double[] doubleArray = { 1.5, 2.6, 3.7, 4.8 };
      int[] intArray = Array.ConvertAll(doubleArray, dbl => (int)Math.Round(dbl));
      Console.WriteLine("Converted to integers (rounded):");
      foreach (int number in intArray) {
         Console.WriteLine(number);
      }
   }
}

Output

Following is the output −

Converted to integers (rounded):
2
3
4
5
csharp_array_class.htm
Advertisements