你能告诉我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是一个更复杂的例子.最后,Autofac和CuttingEdge.Conditons是具有非常好的流畅接口的开源库的两个示例.