Tuesday, June 23, 2009

fluent helpers

Fluent C Sharp Language Extension Helpers - Part 1

So, I found myself doing a lot of for( int i = 0; i < n; i++ ){} stuff lately. So, I've decided to try something new. I've started a small collection of "Fluent Helpers" that alleviate a lot of the verbosity in C#.

About 28 characters (including spaces) for a simple for loop to do some constant iteration.

for( int i = 0; i < n, i++){

About 18 characters to do this (no pun intended):

Do.This( 5, () =>{

Here's an example:


Do.This( 5,() =>
{
Console.Write( "Hello " );
Console.WriteLine( "World!" );
}
);


Prints:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

Now that feels much better on my hands, and looks much cleaner too, IMHO. Here's the simple implementation for Do.This:


public static class Do {

public static void This(int times, Action what)
{
for( int i = 0; i < times; i++ )
what();
}
}


I've already started a little library of these small syntax helpers I've collected. If you have any suggestions, please share! cool0003.gif

-Brian Chavez

No comments: