I won't go into detail about how you could use the maybe monad to save a few lines of code as Daniel Earwicker already blogged about it so you'd better read his post.
Basically using the IfNotNull extension method you'll be able to replace lines of code like these:
with the following:
If you like the idea you could go further and extend it and add an extension method which for example could check whether your method is throwing an ArgumentException:
or you could check for some condition before calling the next method:
And here's the code for the extension methods:
If you want to know more about monads you could read the following blog post or watch the brilliant Brian Beckman at channel9. If not for the monads, at least for the hat he's wearing during the video :)
Copyright © 2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.