Perform a delegation with each element in the array - CSharp System

CSharp examples for System:Array Element

Description

Perform a delegation with each element in the array

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from ww  w . j  a  v  a 2s.  co  m

public class Main{
        /// <summary>
        /// Perform a delegation with each element in the array
        /// </summary>
        /// <typeparam name="T">Type of the Array</typeparam>
        /// <param name="arr">The array</param>
        /// <param name="DO">The delegate to run taking input (X,Y,Z,Value)</param>
        public static void DoForEach<T>(T[,,] arr, Action<int, int, int, 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++)
                        DO(x, y, z, arr[x, y, z]);
        }
        /// <summary>
        /// Perform a delegation with each element in the array
        /// </summary>
        /// <typeparam name="T">Type of the Array</typeparam>
        /// <param name="arr">The array</param>
        /// <param name="DO">The delegate to run taking input (X,Y,Value)</param>
        public static void DoForEach<T>(T[,] arr, Action<int, int, T> DO)
        {
            for (int x = 0; x < arr.GetLength(0); x++)
                for (int y = 0; y < arr.GetLength(1); y++)
                    DO(x, y, arr[x, y]);
        }
        /// <summary>
        /// Perform a delegation with each element in the array
        /// </summary>
        /// <typeparam name="T">Type of the Array</typeparam>
        /// <param name="arr">The array</param>
        /// <param name="DO">The delegate to run taking input (Key->Value)</param>
        public static void DoForEach<T>(T[] arr, Action<int, T> DO)
        {
            for (int x = 0; x < arr.Length; x++)
                DO(x, arr[x]);
        }
}

Related Tutorials