I don’t like to overuse if/else statements. I really dislike seeing code like this:
if(somethingIsTrue)
{
DoSomethingWhenTrue( ) ;
}
else
{
DoSomethingElse( ) ;
}I just had an idea about using extension methods so I can write this instead:
somethingIsTrue.Branch(
() => DoSomethingWhenTrue(),
() => DoSomethingElse()) ;and here’s the extension method:
public static void Branch(this bool @bool, Action left, Action right)
{
if (@bool)
{
left();
}
else
{
right();
}
}Nice or not? I think it reads a bit better (for single line expressions anyway).
.net c# tips🙏🙏🙏
Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please 🦋 ping me on Bluesky! 🦋
Leave a comment
Comments are moderated, so there may be a short delays before you see it.
Published