C# | Converting an array of one type to an array of another type Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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: CSHARP // 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(); } } Output: Initial Array: 10 20 30 40 Converted Array: 10 20 30 40 Example 2: CSHARP // 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(); } } Output: Exception Thrown: System.ArgumentNullException Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.convertall?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Converting an array of one type to an array of another type R rohitprasad3 Follow Improve Article Tags : C# CSharp-Arrays CSharp-method Similar Reads C# | How to convert an ArrayList to Array In C#, an array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float etc. and the elements are stored in a contiguous location.ArrayList represen 4 min read C# | Copying the elements of ArrayList to a new array ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra 2 min read C# | Copy the Stack to an Array Stack<T>.CopyTo(T[], Int32) Method is used to copy the Stack<T> to an existing 1-D Array which starts from the specified array index. Properties: The capacity of a Stack<T>is the number of elements the Stack<T> can hold. As elements are added to a Stack<T> , the capacit 2 min read C# Multidimensional Arrays Multidimensional arrays can be termed as arrays of arrays, extending the capabilities of one-dimensional arrays to store data in a structured format. Syntaxdata_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]â¦.[sizeN];Parameters:data_type: Type of data 4 min read C# Jagged Arrays A jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows 4 min read Like