Is it possible to disable "params"

  • Thread starter Thread starter Stan Huff
  • Start date Start date
S

Stan Huff

Is there any way to disable the "params" on a particular invocation so that
one can pass an array containing the arguments and not have receiver get an
array having you argument array stuffed into the first index?

I have worked around this by getting the MethodInfo via reflection and
invoking it there. Is there a simpler way?

Thanks,

Stan
 
Stan,
| Is there any way to disable the "params" on a particular invocation so
that
| one can pass an array containing the arguments
This is the standard behavior for "params"!

| and not have receiver get an
| array having you argument array stuffed into the first index?

Consider the following function:

static int Sum(params int[] values)
{
int total = 0;
foreach(int value in values)
{
total += value;
}
return total;
}

Given:
int[] values = {1,2,3,4,5};
int total;

You can call it with either of:

total = Sum(1,2,3,4,5);
total = Sum(values);

NOTE: That the type of the array (int[] values) matches the type of the
params parameter (int[]). It will not work if you attempt to pass a int[]
variable to a object[] parameter, as the "shape" of the parameters don't
match.

Hope this helps
Jay



| Is there any way to disable the "params" on a particular invocation so
that
| one can pass an array containing the arguments and not have receiver get
an
| array having you argument array stuffed into the first index?
|
| I have worked around this by getting the MethodInfo via reflection and
| invoking it there. Is there a simpler way?
|
| Thanks,
|
| Stan
|
|
 
Thanks Jay. I went back tried the obvious method and it worked. At some
point in the past I recalled having some issue here, but apparently I was
mistaken. Good thing to know...

Stan
 
Stan,
| point in the past I recalled having some issue here,
Its easy to have an "issue" with it. Consider:

void Write(string format, params object[] values) { ... }

int[] values = {1, 2, 3, 4, 5};

Write(theFormat, values);

Because Write expects object params & I am attempting to send it an int
array, the int array will be put into an object array...

Hope this helps
Jay

| Thanks Jay. I went back tried the obvious method and it worked. At some
| point in the past I recalled having some issue here, but apparently I was
| mistaken. Good thing to know...
|
| Stan
|
| | > Stan,
| > | Is there any way to disable the "params" on a particular invocation so
| > that
| > | one can pass an array containing the arguments
| > This is the standard behavior for "params"!
| >
| > | and not have receiver get an
| > | array having you argument array stuffed into the first index?
| >
| > Consider the following function:
| >
| > static int Sum(params int[] values)
| > {
| > int total = 0;
| > foreach(int value in values)
| > {
| > total += value;
| > }
| > return total;
| > }
| >
| > Given:
| > int[] values = {1,2,3,4,5};
| > int total;
| >
| > You can call it with either of:
| >
| > total = Sum(1,2,3,4,5);
| > total = Sum(values);
| >
| > NOTE: That the type of the array (int[] values) matches the type of the
| > params parameter (int[]). It will not work if you attempt to pass a
int[]
| > variable to a object[] parameter, as the "shape" of the parameters don't
| > match.
| >
| > Hope this helps
| > Jay
| >
| >
| >
| > | > | Is there any way to disable the "params" on a particular invocation so
| > that
| > | one can pass an array containing the arguments and not have receiver
get
| > an
| > | array having you argument array stuffed into the first index?
| > |
| > | I have worked around this by getting the MethodInfo via reflection and
| > | invoking it there. Is there a simpler way?
| > |
| > | Thanks,
| > |
| > | Stan
| > |
| > |
| >
| >
|
|
 
Back
Top