Return to Snippet

Revision: 5009
at February 4, 2008 17:39 by Soft


Initial Code
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();
			}
		}

	}
}

Initial URL


Initial Description
In Ruby you can write 5.times { print "Hello World" } . With a simple extension method I was able to do something similar with C#.

Initial Title
Playing With Extension Methods

Initial Tags
c, Net, extension

Initial Language
C#