开发者

What is this kind of chaining in C# called?

开发者 https://www.devze.com 2022-12-26 14:12 出处:网络
Can you please tell me what kind of construct in C# is this. Code Golf: Numeric equivalent of an Excel col开发者_运维问答umn name

Can you please tell me what kind of construct in C# is this.

Code Golf: Numeric equivalent of an Excel col开发者_运维问答umn name

C.WriteLine(C.ReadLine() 
            .Reverse() 
            .Select((c, i) => (c - 64) * System.Math.Pow(26, i)) 
            .Sum());

Though I am new to C# (only two months exp so far), but since the time I have joined a C# team, I have never seen this kind of chaining. It really attracted me and I want to learn more about it.

Please give some insight about this.


Method chaining like this is often called a fluent interface.

You can make your own fluent interface by implementing functions that return the object they were called on.

For a trivial example:

class Foo 
{
    private int bar;

    public Foo AddBar(int b) 
    {
        bar += b;
        return this;
    }
}

Which can be used like:

Foo f = new Foo().AddBar(1).AddBar(2);

You could also implement a fluent interface using extension methods.

For example:

class Foo 
{
    public int Bar { get; set; }
}

static class FooExtensions 
{
    public static Foo AddBar(this Foo foo, int b)
    {
        foo.Bar += b;
        return foo;
    }
}

etc.

Here is a more complex example. Finally, Autofac and CuttingEdge.Conditons are two examples of open-source libraries that have very nice fluent interfaces.


It's little more than a chain of function calls with some indentation, where C calls ReadLine(), whose result is used for Reverse, whose result is used for Select, etc. The functions themselves are part of LINQ, the ones that get used after translating the syntactic sugar. Here's a list of LINQ query functions, along with samples on their use, and here's a tutorial for LINQ.

(In case you're interested: Reverse() returns an IEnumerable that goes from back to front of the given IEnumerable, Select() returns an IEnumerable listing all elements, after applying the given lambda expression, and Sum() simply returns the sum of all the elements of the given IEnumerable.)


There's nothing special about most of the expression, but the select method makes use of lambda expressions, a key component of Language Integrated Query - LINQ, for short.

.NET Language-Integrated Query defines a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language.

LINQ, and the lambda expressions they use, are a way to write complicated query and manipulation expressions succinctly and readably. It was added to the .NET Framework in 3.5. Here's some more information from MSDN.


The chaining pattern can be called a Fluent Interface. It occurs when an interface's function (or extension method) returns the same interface. In this case, it is IEnumerable.

You also have some LINQ thrown in there with the Select and Sum functions

0

精彩评论

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

关注公众号