开发者

C# - How is this technique called?

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 = ne开发者_如何转开发w 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

  • http://martinfowler.com/dslwip/MethodChaining.html
  • http://en.wikipedia.org/wiki/Method_chaining


Method Chaining


Fluent API.

A lot of frameworks now supports it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜