Skip to content

Instantly share code, notes, and snippets.

@dterracino
Forked from davecowart/TapExtension.cs
Created December 10, 2020 05:54
Show Gist options
  • Save dterracino/259223a7f9e7d273f4ad53befff8276a to your computer and use it in GitHub Desktop.
Save dterracino/259223a7f9e7d273f4ad53befff8276a 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