C#中的这种链接叫什么?

前端之家收集整理的这篇文章主要介绍了C#中的这种链接叫什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你能告诉我C#中有哪种构造吗?

Code Golf: Numeric equivalent of an Excel column name

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

虽然我是C#的新手(到目前为止只有两个月),但自从我加入C#团队以来,我从未见过这种链接.它真的吸引了我,我想了解更多.

请对此有所了解.

解决方法

像这样链接方法通常称为 fluent interface.

您可以通过实现返回调用对象的函数来创建自己的流畅界面.

对于一个简单的例子:

class Foo 
{
    private int bar;

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

哪个可以用作:

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

您还可以使用扩展方法实现流畅的界面.

例如:

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

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

等等

Here是一个更复杂的例子.最后,AutofacCuttingEdge.Conditons是具有非常好的流畅接口的开源库的两个示例.

原文链接:https://www.f2er.com/csharp/98700.html

猜你在找的C#相关文章