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:
51 Do.This( 5,
52 () =>
53 {
54 Console.Write( "Hello " );
55 Console.WriteLine( "World!" );
56 }
57 );
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:
42 public static class Do
43 {
44 public static void This(int times, Action what)
45 {
46 for( int i = 0; i < times; i++ )
47 {
48 what();
49 }
50 }
51 }
I’ve already started a little library of these small syntax helpers I’ve collected. If you have any suggestions, please share!
-Brian Chavez
Leave a comment
Your email address will not be published. Required fields are marked *