unknown parameters

  • Thread starter Thread starter Howard
  • Start date Start date
H

Howard

Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}
 
Howard said:
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}

You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry
 
You can simulate this *for a single parameter type* by using "params":

public static string MyMethod(params string[] values) {
return string.Join(",", values);
}
public static void Examples() {
Debug.WriteLine(MyMethod("a", "b", "c"));
string[] data = new string[] { "d", "e", "f" };
Debug.WriteLine(MyMethod(data));
}

Internally (to the method) "values" acts just like a string-array; the
difference is to the caller, who can now pass in *either* an array, *or* an
inline series of (correctly-typed) values.

If you can't predict the types, then "params object[]" is an option; note
that for value-types this will incur boxing; to get around this you could
use generics (in 2.0):
public static string MyMethod<T>(params T[] values) {
// do something interesting
}

Note that this use of generics won't allow you to do anything exciting (as
all we know is that T : object), but it avoids the box. If the types change
per-parameter, then object[] is the only route.

Marc
 
is there going to be any performance or security problems with this method?


Barry Kelly said:
Howard said:
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}

You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry
 
When sending value types as objects they will be boxed. Boxing should
generally be avoided as it is slower and uses more memory resources than
using value types directly, but on the other hand it's a feature that is
very useful in some cases.

There is no security problems with this. The type information follows
the object, so there is no risk of accidentally casting a value to the
wrong type.
is there going to be any performance or security problems with this method?


Barry Kelly said:
Howard said:
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}
You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry
 
Say I have something like this
string a = "123";

int number = (int)a;
int number = int.parse(a);

is the unboxing method int number = (int)a faster in this case?
thanks

Göran Andersson said:
When sending value types as objects they will be boxed. Boxing should
generally be avoided as it is slower and uses more memory resources than
using value types directly, but on the other hand it's a feature that is
very useful in some cases.

There is no security problems with this. The type information follows the
object, so there is no risk of accidentally casting a value to the wrong
type.
is there going to be any performance or security problems with this
method?


Barry Kelly said:
Can c# pass a unknown number of parameters like in javascript?
the parameters could be different types

eg.
myMethod(string a, param)
{
int a = param[0];
string b = param[1];

}
You do it like this:

---8<---
public void MyMethod(params object[] args)
{
int a = (int) args[0];
// etc.
}
--->8---

-- Barry
 
Howard said:
Say I have something like this
string a = "123";

int number = (int)a;
int number = int.parse(a);

is the unboxing method int number = (int)a faster in this case?
thanks

This will not compile (try it). If the type of "a" was object (or if was
passed to the method I provided above), the cast would fail at runtime
with an InvalidCastException.

You can discover the type with a.GetType() or by testing "a is string"
etc.

The unboxing overhead relates to this:

object a = 4; // boxes an int

4 is a value type, native to the CPU. It isn't stored on the garbage
collected heap. All reference types are stored on the heap, however. In
order to make a "4" value fit into a reference type such as the most
general "System.Object" (object is an alias for System.Object), space
has to be made available on the heap, and the value needs to be copied
into this space. This process is called boxing. The reverse:

int i = (int) a; // where a is the object above

.... is called unboxing, and is the process of copying the value from the
heap back into a value type, where it can be optimized to (say) a CPU
register, for more efficient operations.

The overhead of this copying is the downside to the params object[]
approach. This overhead is the cost of allocating and initializing a
handful of bytes (around 8-12). In other words, it's relatively
insignificant, unless you're doing it in the middle of a very tight
loop.

-- Barry
 
Back
Top