tags:

views:

84

answers:

2

Something like:

public static void Estimate ( params string [ ] names, params int[] numbers )
{ }

where you could use it like:

Estimate ( "Eric", "Simpson", "Jon", 1, 2, 3, 4, 5 )
+1  A: 

As already discussed in the comments - this would be a nightmare for compiler writers and for the authors of the specification as well :-). (overload resolution in C# is already complicated enough)

One possible solution when you need something like this in practice is to just give up and use a single params parameter of type object[]. This may not be very elegant, but it allows you to do what you need. The compiler will not check whether all uses are correct, but it may still be reasonable...

For example, constructors of LINQ to XML types (e.g. XElement) usually do this, so that you can create XElement containin other elements, attribtues, collections of elements, etc..

 new XElement( new XElement("elem"),
               new XAttribute("attr", "value"),
               GetListOfElements() );
Tomas Petricek
Thanks but having to figure out the right return type overload for linq was already complicated for someone like Eric Lippert, yet they did it. Is it because this feature is seen as not very important?
Joan Venge
The thing that makes C# often very complicated is that it needs to preserve the behavior from previous versions and add new features on top of it without breaking anything. This is why overloading in LINQ is difficult. And yes - I think that they did it for LINQ because otherwise, LINQ wouldn't be as nicely useable (which was the main point of the project)...
Tomas Petricek
+2  A: 

Tomas and Kirk are right, this would be difficult to resolve and, even more so, it would potentially produce unexpected behavior from the point of the developer using it. For example, take the following class:

public class Overload<T, U>
{
    public void SomeMethod(params T[] arrayOfT, params U[] arrayOfU) { }
}

Allowing multiple parameters using the 'params' keyword in a method would make this compile just fine, and would work fine for quite a few cases until someone tried to use the class as Overload<string, string> (or something similar) and then a compilation error would happen. Another difficulty is that a parameter marked with the 'params' keyword allows the omission of that parameter when calling the method. For example, the following is also impossible to resolve adequately if the method is called with no parameters or even with several int values.

void SomeOtherMethod(params string[] arrayOfString, params int[] arrayOfInt);
void SomeOtherMethod(params int[] arrayOfInt);

Language features are generally added if there is a distinct benefit to them being there. Will they make things easier or just impede progress? This is a clear impediment, and one that can be gotten around by doing like Tomas suggested and using a params object[] parameter. You can also pass individual arrays (i.e. SomeOtherMethod(string[] arrayOfString, int[] arrayOfInt)), if separating by type works better for you.

Chris Hannon
Wow...it's your first post and you've already figured out how to articulate and format your answer. Not bad.
Brian Gideon