CSharp examples for System:Array Convert
Convert all elements of a rectangular array to a vector, using a function.
using System;//from ww w.j av a 2s . c o m public class Main{ /// <summary> /// Convert all elements of a rectangular array to a vector, using a function to cast/transform each element. /// The dimension reduction is column-first, appending each line of the input array into the result vector. /// </summary> /// <typeparam name="T">The type of the input array elements</typeparam> /// <typeparam name="U">The type of the output array elements</typeparam> /// <param name="array">Input array</param> /// <param name="fun">A conversion function taking in an object of type T and returning one of type U</param> /// <returns></returns> public static U[] ArrayConvertAllOneDim<T, U>(T[,] array, Func<T, U> fun) { int rows = array.GetLength(0); int cols = array.GetLength(1); U[] res = new U[rows * cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) res[rows * j + i] = fun(array[i, j]); return res; } }