gpt4 book ai didi

c# - C# 中的这种链接称为什么?

转载 作者:太空狗 更新时间:2023-10-29 21:00:07 24 4
gpt4 key购买 nike

你能告诉我这是 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是具有非常流畅的界面的开源库的两个示例。

关于c# - C# 中的这种链接称为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2634891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com