开发者

Abandoning overloads in favor of Extension methods

开发者 https://www.devze.com 2023-02-22 16:36 出处:网络
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?

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.


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?

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:

  • Working with languages which don't understand optional arguments. C# and VB.Net are not the only .NET languages out there.
  • Sometimes optional arguments just don't cut it - for example, you often need a true default constructor, and optional arguments, while they look like it when hand writing code, behave very differently in the face of reflection/serialization/etc.


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:

public int Frob(int count)

But the most common expected use case is for clients to call it with a count value of 1. So you want to create a Frob() 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消