Set every element in an array to the return value of a delegation - CSharp System

CSharp examples for System:Array Element

Description

Set every element in an array to the return value of a delegation

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//w w w  . j a v a2  s.  c  o m

public class Main{
        /// <summary>
        /// Set every element in an array to the return value of a delegation
        /// </summary>
        /// <typeparam name="T">Type of the Array</typeparam>
        /// <param name="arr">The array</param>
        /// <param name="DO">The delegate to set the values taking input (X,Y,Z,Value)</param>
        public static void SetForEach<T>(T[,,] arr, Func<int, int, int, T, T> DO)
        {
            for (int x = 0; x < arr.GetLength(0); x++)
                for (int y = 0; y < arr.GetLength(1); y++)
                    for(int z = 0; z < arr.GetLength(2); z++)
                        arr[x, y,z] = DO(x, y,z, arr[x, y,z]);
        }
        /// <summary>
        /// Set every element in an array to the return value of a delegation
        /// </summary>
        /// <typeparam name="T">Type of the Array</typeparam>
        /// <param name="arr">The array</param>
        /// <param name="DO">The delegate to set the values taking input (X,Y,Value)</param>
        public static void SetForEach<T>(T[,] arr, Func<int, int, T, T> DO)
        {
            for (int x = 0; x < arr.GetLength(0); x++)
                for(int y = 0; y < arr.GetLength(1); y++)
                    arr[x,y] = DO(x,y, arr[x,y]);
        }
        /// <summary>
        /// Set every element in an array to the return value of a delegation
        /// </summary>
        /// <typeparam name="T">Type of the Array</typeparam>
        /// <param name="arr">The array</param>
        /// <param name="DO">The delegate to set the values taking input (Key->Value)</param>
        public static void SetForEach<T>(T[] arr, Func<int, T, T> DO)
        {
            for (int x = 0; x < arr.Length; x++)
                arr[x] = DO(x, arr[x]);
        }
}

Related Tutorials