Initialize array With function - CSharp System

CSharp examples for System:Array Element

Description

Initialize array With function

Demo Code


using System;/*from  w  w  w  .j  av a2 s  . c  o  m*/

public class Main{
    public static T[] InitializeWith<T>(this T[] a, Func<int, T> fn)
      {
         for (var i = 0; i < a.Length; i++)
         {
            a[i] = fn(i);
         }
         return a;
      }
    public static T[] InitializeWith<T>(this T[] a, Func<T> fn)
      {
         for (var i = 0; i < a.Length; i++)
         {
            a[i] = fn();
         }
         return a;
      }
}

Related Tutorials