/ Published in: C#

In Ruby you can write 5.times { print "Hello World" } . With a simple extension method I was able to do something similar with C#.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; namespace ExtensionMethodTest { static class Program { public delegate void MyDelegate(); static void Main(string[] args) { 5.Times(delegate() { Console.WriteLine("Hello World"); }); Console.Read(); } public static void Times(this int count, MyDelegate del) { for (int i = 0; i < count; i++) { del(); } } } }
Comments
