projects each element of an array into a new form - CSharp System

CSharp examples for System:Array Enumerate

Description

projects each element of an array into a new form

Demo Code


using System.Text;
using System.Runtime.InteropServices;
using System.Linq;
using System.Collections.Generic;
using System;//from ww  w.j ava  2 s . co m

public class Main{
        /// <summary>
      /// projects each element of an array into a new form
      /// </summary>
      /// <typeparam name="TSrc">the type of the elements of source array</typeparam>
      /// <typeparam name="TRes">the type of the value returned by selector</typeparam>
      /// <param name="array">an array of values to invoke a transform function on</param>
      /// <param name="selector">a transform function to apply to each element</param>
      /// <returns>
      /// an array whose elements are the result of invoking the transform function on each element of source array
      /// </returns>
      public static TRes[] Copy<TSrc, TRes>(this TSrc[] array, Func<TSrc, TRes> selector) {
         if(array == null){
            return null;
         }
         var cnt = array.Length;
         var mappedArray = new TRes[cnt];
         for (int i = 0; i < cnt; ++i) {
            mappedArray[i] = selector(array[i]);
         }
         return mappedArray;
      }
}

Related Tutorials