public static void Main(string[] args) { int[] A = new int[] { 1, 2, 3 }; PrintArray(A); int[] B = Prog2.SquareArray(A); PrintArray(B); //1, 4, 9 int[] C = Prog2.IncrementArray(A); PrintArray(C); //2, 3, 4 int[] D = ApplyOperation(A, Square); PrintArray(D); //1, 4, 9 int[] E = ApplyOperation(A, TimesTen); PrintArray(E); //10, 20, 30 int[] F = ApplyOperation(A, Sqrt); PrintArray(F); //1, 2, 2 MickyMouse times100 = delegate(int x) { return(x * 100); }; //annoymous method. a method that doesn't belong to a class int[] G = ApplyOperation(A, times100); PrintArray(G); //100, 200, 300 MickyMouse times100b = delegate(int x) { return(TimesN(x, 100)); }; int[] H = ApplyOperation(A, times100b); PrintArray(H); //100, 200, 300 int[] I = ApplyOperation(A, delegate(int x) { return(x * 100); }); PrintArray(I); int[] J = ApplyOperation(A, (int x) => { return(x * 100); }); //lambda espression. G,H,I and J are the same. PrintArray(J); }
delegate int MickyMouse(int b); //delegate; this one takes in integer and returns int static int[] ApplyOperation(int[] A, MickyMouse ops) { int[] newArray = new int[A.Length]; for (int i = 0; i < A.Length; i++) { newArray[i] = ops(A[i]); //applying ops to each element } return(newArray); }