C# | Converting an array of one type to an array of another type

Array.ConvertAll(TInput[], Converter<TInput, TOutput>) Method is used to convert an array of one type to an array of another type.

Syntax:

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

Here, TInput and TOutput is the source array and target array respectively.

Parameters:

array: It is the one-dimensional, zero-based Array to convert to a target type.
converter: It is a Converter that converts each element from one type to another type.



Return Value: This method returns an array of the target type containing the converted elements from the source array.

Exception: This method throws ArgumentNullException if the array is null or converter is null.

Below programs illustrate the use of Array.ConvertAll(TInput[], Converter<TInput, TOutput>) Method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Array.ConvertAll() Method
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and initializing
            // new the Array of int
            int[] myArr = {10, 20, 30, 40};
  
            // Display the values of the myArr.
            Console.WriteLine("Initial Array:");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(myArr);
  
            // converting int myArr to String conarr
            String[] conarr = Array.ConvertAll(myArr, 
              new Converter<int, String>(intToString));
  
            // Display the values of the myArr.
            Console.WriteLine("Converted Array:");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(conarr);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(String[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(int[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
  
    // Defining the method
    // intToString
    public static String intToString(int pf)
    {
        return pf.ToString();
    }
}

chevron_right


Output:

Initial Array:
10
20
30
40

Converted Array:
10
20
30
40

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Array.ConvertAll() Method
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and initializing new 
            // the Array of int with null value
            int[] myArr = null;
  
            // converting int myArr to String conarr
            String[] conarr = Array.ConvertAll(myArr, 
             new Converter<int, String>(intToString));
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(conarr);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(String[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
  
    // Defining the method
    // intToString
    public static String intToString(int pf)
    {
        return pf.ToString();
    }
}

chevron_right


Output:

Exception Thrown: System.ArgumentNullException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.