6

Here on this page, Scott Hanselman shows two examples from Micro-ORMs Dapper and Massive, and the Massive-example caught my interested, because I don't see how they could implement that syntax.

The example is as follows, where I'm going to break it over several lines instead of just one long one:

var tbl = new Products();
var products = tbl.All(where: "CategoryID = @0 AND UnitPrice > @1",
    orderBy: "ProductName", limit: 20, args: 5,20);
                                       ^----+---^
                                            |
                                            +-- this

How did they implement this syntax, allowing args to have multiple values? I'm assuming params-based arguments, because that's the only thing that allows for that, but I don't understand how they constructed the method to allow for that since it seems to me that all I try ends up complaining about named arguments and fixed position arguments are in the wrong order.

I tried a test-method like this:

public static void Test(string name, int age, params object[] args)
{
}

and then using named arguments:

Test(age: 40, name: "Lasse", args: 10, 25);

But all I get is this:

Named argument specifications must appear after all fixed arguments have been specified

so obviously that is wrong. Also I can't see in the source anything that would allow for this but maybe I'm looking in the wrong place.

What am I missing here?

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825

2 Answers2

8

Actually I think that Mr. Hanselman showed some code that doesn't compile (oops, did I really dare to say that?). I can only get it working like this:

 Test(age: 40, name: "Lasse", args: new object[] { 10, 25 });
Richard
  • 627
  • 7
  • 14
  • 2
    That makes sense, perhaps I should take a trip back to his blog and ask there... perhaps that would've been the right approach to begin with, oh well. I'll hold off on accepting this answer to see if there is a way (though I doubt it). – Lasse V. Karlsen May 27 '11 at 13:00
  • Hm, I copied it from Rob's blog. Sorry about that. – Scott Hanselman Jun 04 '11 at 07:11
2

This is just named arguments in C# 4.0. You can specify your arguments by using the name of the parameter as you see in your call above.

To accept an array (as you see with the multiple "args") - you simply use the "params" keyword:

public void MyMethod(string arg1, params object[] args){ //.. }

Now, to call this method in C# 4.0, you could use "MyMethod(arg1: "Lasse", args:1,2,4,5)"

  • 2
    Are you *sure* I can call it using `args:1,2,4,5` ? I can't get that to compile. I can only get this to compile: `args: new[] { 1,2,4,5 }`. Just to be clear, I am *not* asking about the `args:` part, I'm fully aware of how named arguments in C# 4.0 work, specifically I'm asking about the `1,2,4,5` part. – Lasse V. Karlsen Jul 01 '11 at 07:14
  • 2
    Hmm - well I'm sure, and then I'm not sure. This is strangeness - if you only pass in ONE argument to args:, then it will work. If you pass in multiple, then I get the same error as you. SKEEEET! –  Jul 05 '11 at 23:20