是否值得在 C# 中使用方法链?
在 C# 中使用集合初始值设定项 和允许定义类的属性而无需调用构造函数 ,在 C# 中使用方法链接有什么意义吗? 我什么也看不见。也许我在这里遗漏了一些东西?
谢谢
Having Collection initializers in C# and being allowed to define properties of a class without having to call the constructor, is there any point in using Method Chaining in C#?
I can't see any. Maybe I'm missing something here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ?
LINQ?
常见的用途是 流畅的接口
编辑:为了回答评论中的问题,属性/集合初始化器相当有限,因为您只能设置属性或调用集合上的 Add 方法,而方法调用则更灵活,因为它们可以采用多个参数。
流畅的接口只是方法链接的一种特定用途,用于生成更具可读性的 API,通常适用于对象构建器。
另外,顺便说一句,MSDN 文章非常具有误导性,因为对象初始化程序不允许您绕过构造函数,只是在示例中,
StudentName
类有一个默认构造函数,该构造函数不执行任何操作。A common use is fluent interfaces
EDIT: In response to the questions in the comments, property/collection initialisers are fairly limited in that you can only set propeties or call the Add method on a collection, whereas method calls are more flexible since they can take multple arguments.
A fluent interface is just one specific use of method chaining to produce a more readable API, often for object builders.
Also, as an aside that MSDN article is quite misleading since object initialisers don't allow you to bypass the constructor, it's just that in the example, the
StudentName
class has a default constructor which does nothing.CuttingEdge.Conditions 是说明为什么方法链接很方便的一个很好的例子?
CuttingEdge.Conditions is a good example of why method chaining is convenient?