Converts an array of one type to an array of another type. - CSharp System

CSharp examples for System:Array Convert

Description

Converts an array of one type to an array of another type.

Demo Code

// Permission is hereby granted, free of charge, to any person
using System;/*from ww  w .j a  v a  2  s .c o m*/

public class Main{
        /// <summary>
        /// Converts an array of one type to an array of another type.
        /// </summary>
        /// <typeparam name="TInput">The type of the elements of the source array.</typeparam>
        /// <typeparam name="TOutput">The type of the elements of the target array.</typeparam>
        /// <param name="array">The one-dimensional, zero-based Array to convert to a target type.</param>
        /// <param name="converter">A Converter that converts each element from one type to another type.</param>
        /// <returns>An array of the target type containing the converted elements from the source array.</returns>
        public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter)
        {
#if PORTABLE
            if (array == null)
                throw new ArgumentException();

            return (from item in array select converter(item)).ToArray();
#else
            return Array.ConvertAll(array, converter);
#endif
        }
}

Related Tutorials