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 rohitprasad3 Follow Improve Article Tags : C# CSharp-method CSharp-Arrays 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# | Type.GetTypeArray() Method Type.GetTypeArray() Method is used to get the types of the objects in the specified array. Syntax: public static Type[] GetTypeArray (object[] args); Here, it takes an array of objects whose types to determine. Return Value: This method returns an array of Type objects representing the types of the 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 C# Arrays 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. Length of the array spe 8 min read Return an Array in C In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C 5 min read How to Pass Array of Structure to a Function in C? An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin 2 min read How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 2 min read Like