Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How is this technique called?

Tags:

c#

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;
    }
}
like image 990
Svetlozar Angelov Avatar asked Apr 21 '10 10:04

Svetlozar Angelov


1 Answers

Method Chaining

  • http://martinfowler.com/dslwip/MethodChaining.html
  • http://en.wikipedia.org/wiki/Method_chaining
like image 105
Tim Ridgely Avatar answered Nov 15 '22 08:11

Tim Ridgely