Use a list of objects.
In the case the number of parameters are unknown at planning time, just use a list of objects. Something like:
void g(params object[] args) {
foreach (object arg in args) {
if ((arg is int) && (foo((int)arg))) baz((int)arg) else
if ((arg is string) && (foo((string)arg))) baz((string)arg)
}
}
(Assuming you have bool foo(int)
, bool foo(string)
...)
So you can call:
g(p1, p2);
g(p1);
g(p1, p2, p3)
with any combination of the types, since every reference derives from object (which it could be many more types than required, int and string, but could be handy in future to support more other types).
This is possible since you could use Reflection to recognize the type at runtime.
Another way to execute a sequence of operation is the use of interfaces, defining the action to execute at certain conditions, on certain objects.
interface IUpdatable {
void Update(object[] data);
}
class object1 : IUpdatable { public void Update(object data) { baz(data); } }
class object2 : IUpdatable { public void Update(object data) { baz(data); } }
void g(params IUpdatable[] args) {
foreach (IUpdatable arg in args) {
arg.Update(args);
}
}
But this way you have to model p1 and p2 (but also p3, as objects implementing an interface, which could be not possible.