tags:

views:

38

answers:

3

I am calling a function which takes the same form as string.format where the first parameter is a string and the remainder are the replacement values. I have the string in a variable and the replacement values in an array, how can I call this function given any number of objects in the array? Simply passing in the array as the last argument does not work.

+3  A: 

you need to use the params keyword

Adeel
+5  A: 

Use the params keyword:

public void MyMethod(string value, params object[] args)
{
     // as an example
     return string.Format(value, args);
}

Then you can call it either with individual values

MyMethod("Test", "value1", "value2");

Or with an array

MyMethod("Test", new [] { "value1", "value2" });
Dave
A: 

The function I was calling had the signature

public static IQueryable Where(this IQueryable source, string predicate, params object[] values)

Thhe issue was that I was passing in an array of ints as the last parameter. When I createed a new array of objects poplulated from that initial array and passed it in it worked. Thanks for the answers

Macros