放弃重载,转而使用扩展方法
既然我们在 C# 中有了扩展方法,那么在任何类的实现中保留重载以传递默认值还有什么意义吗? 当可以进行扩展方法重载时,为什么会污染类实现呢? 重载有什么好处(用于传递默认值)?
我正在计算默认参数的选项,因为它强制参数的特定顺序(即默认参数可以出现在最后),并且默认值在客户端代码中进行编译,并且服务包可能因此而中断。
Now that we have extension methods in C#, Is there any point left in keeping overloads in implementation of any class for passing default values?
Why pollute the class implementation when overloads can be made extension methods?
Any advantage of overloads (for passing default values)?
I'm counting out the option of default parameters because it forces specific ordering of parameters (i.e. default ones can come in end) and the fact that default value gets compiled in client code and a service pack could possibly break because of that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将使用可选参数而不是扩展方法来删除重载。
扩展方法有很多缺点 - 它们对私有成员的访问较少,它们存在可发现性问题(因为它们是在单独的类上定义的)等。
但是,有时使用重载方法和构造函数比使用重载方法和构造函数更好添加可选参数 - 与您已经提到的问题分开。这包括:
I would use optional arguments instead of extension methods to remove overloads.
Extension methods have quite a few disadvantages - they have less access to the private members, they have a discoverability issue (as they're defined on a separate class), etc.
However, there are times when using overloaded methods and constructors are better than adding optional arguments - separate from the issues you already mentioned. This include:
问题不应该是“重载有优势吗”,而应该是“扩展方法有哪些优势使其优于重载?”在我看来,扩展方法的缺点远远超过任何可察觉的优点。事实上,当您设计类时,我无法想出扩展方法提供的任何优势。
例如,假设您的类具有此方法:
但最常见的预期用例是客户端使用
count
值为 1 来调用它。因此您想要创建一个Frob()
不需要该参数的方法。当然,在 .NET 4.0 中,您可以使用默认参数来执行此操作,但正如您所说,默认参数并不总是一个选项,因为它们必须放在最后。因此,如果没有默认参数,您可以选择 1) 创建重载;或 2) 创建扩展方法。
创建过载很简单。创建扩展方法需要一个静态类和一个静态方法,并且引入了无意隐藏的可能性——所有的复杂性都没有任何好处。当您只需编写 dang 重载并完成它时,为什么要使用扩展方法来模拟重载呢?
如果您无法修改类,那么扩展方法是显而易见的选择。但如果可以选择,请使用旨在提供您想要的功能的功能。
The question shouldn't be "are there advantages to overloads," but rather, "what are the advantages of extension methods that make them superior to overloads?" In my opinion, the disadvantages of extension methods far outweigh any perceived advantages. In fact, I'm unable to come up with any advantage that extension methods provide when you're designing a class.
Suppose, for example, your class has this method:
But the most common expected use case is for clients to call it with a
count
value of 1. So you want to create aFrob()
method that doesn't require that parameter.Granted, in .NET 4.0 you could do this with default parameters, but as you say default parameters aren't always an option because they have to come last. So without default parameters, you have the option of 1) creating an overload; or 2) creating an extension method.
Creating an overload is straightforward. Creating an extension method requires a static class and a static method, and introduces the possibility of unintentional hiding--all complications that come with no benefit. Why use extension methods to emulate overloads when you can just write the dang overload and be done with it?
If you're unable to modify the class, then extension methods are the obvious choice. But given the choice, use the feature that's designed to provide the functionality that you want.
除了 Reed Copsey 的答案:扩展方法根据定义是静态的,而静态通常被认为是邪恶的单元测试和模拟。这就是为什么我个人尝试尽可能避免扩展方法。
In addition to Reed Copsey's answer: extension methods are by definition static, and statics are often considered evil in terms of unit testing and mocking. This is why I personally try to avoid extension methods as much as possible.