CSharp - Array Array Converting


Array.ConvertAll creates and returns a new array of element type TOutput, calling the supplied Converter delegate to copy over the elements.

Converter is defined as follows:

public delegate TOutput Converter<TInput,TOutput> (TInput input)

The following converts an array of floats to an array of integers:


using System;
class MainClass/*w w  w.  j  a v a 2  s. co m*/
    public static void Main(string[] args)
        float[] reals = { 1.3f, 1.5f, 1.8f };
        int[] wholes = Array.ConvertAll(reals, r => Convert.ToInt32(r));

        Array.ForEach(wholes, Console.WriteLine);