5

I've had a look at Path.Combine and noticed it has four overloads:

  1. string, string
  2. string, string, string
  3. string, string, string, string
  4. params string[]

How are the first three overloads useful?
The way I see it, the fourth overload makes the others pretty pointless. I looked at the source and I did see that the fourth overload's implementation is a bit different, but even in this case I would expect to have just the one params overload which decides which implementation to use based on the array's length.

Blachshma
  • 17,097
  • 4
  • 58
  • 72
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • Well, `Combine(String, String)` is there for backwards compatibility (versions before 4 did not have any other overloads). – Oded Dec 26 '12 at 10:18
  • As I understand it, the non-params overloads allow you to skip the overhead of creating the parameter array and the argument iterator. I don't remember where I read that, though, so I'm commenting rather than answering. – phoog Dec 26 '12 at 10:26
  • @phoog - that makes sense. Most uses will need to combine up to 4 path sections. – Oded Dec 26 '12 at 10:28
  • 1
    @Oded In fact, I was speaking generally of params methods that have seemingly unnecessary overloads with 1, 2, and 3 parameters. Here's the same question, but asked about string.Format: http://stackoverflow.com/questions/2796731/why-do-the-overloads-of-string-format-exist – phoog Dec 26 '12 at 10:35

3 Answers3

5

According to this answer, https://stackoverflow.com/a/2796763/385844, it's to avoid the overhead of creating the parameter array, and because the non-params overloads are convenient for users of languages that do not support variable-length parameter lists.

See also

Why does string.Format come in several flavors?

Community
  • 1
  • 1
phoog
  • 42,068
  • 6
  • 79
  • 117
1

Just like Oded said, I found out that it must have been there for backward compatibility as I couldn't found it in 2.0, 3.5

I think the overloaded started in 4.0

  • Path.Combine 4.0 - If you look at the right navigation, you'll see the overloads

As for the other many overloads, I wouldn't speak for .net team, but I feel they feel is pointless increasing the overloads every time so they stopped at 4 and provided an Array of string for more than 4 string combinations - which I think is wise

I based my explanation on Lambda expression where the team stopped at 16 arguments

Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16)

Path.Combine could have been like that but is pointless.

codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • 1
    Yes, but it doesn't explain the 3 and 4 string overloads (in particular since the params array overload was added at the same time). – Oded Dec 26 '12 at 10:24
  • 2
    The `Action` thing is different, currently there is no way for a `params` generic definition. – SWeko Dec 26 '12 at 10:35
0

I can only speak from my experience with other C# developers.

Not all developers are familiar or comfortable with the params syntax (and the fact that the technical name is variadic function parameters doesn't help).
I know I've had to explain it over and over again, so it is not unusual to see calls

instance.ParamsMethod(new int[]{1});
//or even
instance.ParamsMethod(new List<int>{1}.ToArray());

for a method writen as:

public void ParamsMethod(params int[] source) {}

negating all the sweet syntactic sugar of params (and then some).

So, my personal preference is to provide the 1 and 2 parameter case as overloads, because that somewhat makes it harder to clutter the code unnecessarily. The call is marginally slower because of the overload chaining, but it helps make clearer code.

SWeko
  • 30,434
  • 10
  • 71
  • 106