-
-
Save dterracino/259223a7f9e7d273f4ad53befff8276a to your computer and use it in GitHub Desktop.
C# implementation of Ruby's tap method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class TapExtension { | |
| public static T Tap<T>(this T obj, Action<T> block) { | |
| block.Invoke(obj); | |
| return obj; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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