C#中的这种链接叫什么?
发布时间:2020-12-15 18:30:11 所属栏目:百科 来源:网络整理
导读:你能告诉我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#团队以来
你能告诉我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是具有非常好的流畅接口的开源库的两个示例. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |