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/*from   www.  j a va  2  s  .c om*/
    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);