Apply a callback to an array: Difference between revisions

no edit summary
No edit summary
Line 98:
# Alternatively:
[1,2,3,4,5].each { |i| puts i**2 }
 
==[[C#]]==
using System;
static class Program
{
/*
* Purpose: Apply a callback to an Array.
* Output: Prints 1, 4, 9, 16, 25 to the console.
* Compiler: Visual Studio 2005
* Framework: .net 2
*/
[STAThread]
public static void Main() {
int[] intArray = { 1, 2, 3, 4, 5 };
// Using a callback,
Console.WriteLine("Printing squares using a callback:");
Array.ForEach<int>(intArray, PrintSquare);
// or using an anonymous method:
Console.WriteLine("Printing squares using an anonymous method:");
Array.ForEach<int>(
intArray,
delegate(int value) {
Console.WriteLine(value * value);
});
}
public static void PrintSquare(int value) {
Console.WriteLine(value * value);
}
}
Anonymous user