Skip to content

Instantly share code, notes, and snippets.

@davecowart
Created June 22, 2012 18:02
Show Gist options
  • Save davecowart/2974266 to your computer and use it in GitHub Desktop.
Save davecowart/2974266 to your computer and use it in GitHub Desktop.
C# implementation of Ruby's tap method
public static class TapExtension {
public static T Tap<T>(this T obj, Action<T> block) {
block.Invoke(obj);
return obj;
}
}
var stringBuilderSmall = new StringBuilder().Tap(sb => sb.AppendLine("Inside the small tap"));
Console.WriteLine(stringBuilderSmall.ToString());
var stringBuilderBig = new StringBuilder().Tap(sb => {
sb.AppendLine("Inside the big tap");
sb.AppendLine("Still inside the big tap");
sb.AppendLine("Yep, still inside the big tap");
});
Console.WriteLine(stringBuilderBig.ToString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment