Imagine I have a set of global methods that aren’t associated with any specific classes. Each method takes exactly one object and returns exactly one object of various types.
I want to be able to link these methods together so that the object returned by one method gets passed as a parameter to the next method and so on.
Extension methods are the easiest and most elegant way I can think of for accomplishing this:
var result = someObj.ExtensionMethod1().ExtensionMethod2().ExtensionMethod3();
However, I also have a log method which takes an object parameter and I want to pass the result of each method to this log method. For example:
var result1 = someObj.ExtensionMethod1();
log(result1);
var result2 = result1.ExtensionMethod2();
log(result2);
var result3 = result2.ExtensionMethod3();
log(result3);
Is there a more elegant of way of doing this so I won’t have to create temporary variables to pass every time I call a method?
You’re looking for
(The generic parameter is needed so that the return value can be used without casting)
LINQPad’s
Dump()method works the same way.