In .Net, you can chain methods returning a value or by using a void. Is one of them the "right way"?
So you could say
1)
Foo myFoo = new Foo();
myfoo.Bars =
myBars.DoSomethingCool(x)
.DoSomethingElse(y)
.AndSomethingElse(z);
public static IList<IBar> DoSomethingCool(this IList<IBar> source, object x)
{
IList<IBar> result = //some fn(source)
return result;
}
In this case, all 3 extension methods need to return IList (the type for myFoo.Bars)
or it could also be written as
2)
myBars.DoSomethingCool(x)
.DoSomethingElse(y)
.AndSomethingElse(z);
public static void DoSomethingCool(this IList<IBar> source, object x)
{
//Modify source
source = //some fn(source)
//Don't return anything
}
in which case, the extension methods return a void, but do the work on the source object that's coming in?
UPDATE Simon was correct in his answer that 2) won't compile. Here is how that could be re-written:
DoSomethingCool(myBars)
.DoSomethingElse(myBars)
.AndSomethingElse(myBars);
myBars would then be changing inside the call of each method and the methods would be returning void.