I was wondering if this technique has a name - changing state methods to return this, to be able to write them in the linq way method().method().method()
.
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.Add(1).Write().Add(2).Write().Add(3).Write().Add(4).Write().Subtract(2).Write();
}
}
internal class Test
{
private int i = 0;
public Test Add(int j)
{
i += j;
return this;
}
public Test Subtract(int j)
{
i -= j;
return this;
}
public Test Write()
{
Console.WriteLine("i = {0}",i.ToString());
return this;
}
}
Method Chaining
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With