Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several overloads for method with "params" keyword

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.

like image 480
Adi Lester Avatar asked Dec 26 '12 10:12

Adi Lester


2 Answers

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?

like image 195
phoog Avatar answered Oct 20 '22 09:10

phoog


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

  • Path.Combine 2.0
  • Path.Combine 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.

like image 20
codingbiz Avatar answered Oct 20 '22 08:10

codingbiz