Reputation: 47
I would like to pass a lot of arguments to a method by ref but I don't know the number of arguments.
I have tried something like this but is it not working :D :
public void myMethod(ref params object args)
I will think to pass the pointers in params but is it little complicated in C# :/
Possible workaround ?
EDIT:
I want to encapsulate a part of code, basically like this :
....
var collectionA = new List<string>();
var myObject = // an object
Optimizer.Optimize(ref collectionA, ref myObject); // cache
//{
MaClass.Treatment(); // use collectionA stored in cache via Optimizer
// the collectionA is modified in MaClass.Treatment()
...
//}
Optimizer.EndOptimize();
...
The goal, cannot request my server all the time for the same treatment (HTTPRequest) if the call was encapsulate into my Optimizer
Upvotes: 1
Views: 974
Reputation: 1062640
No, that's not possible. However, you can get much of the same by mutating the array in the method, then simply reading the values back out of the array at the call-site:
var args = new[] {x, y, z}
obj.myMethod(args);
x = args[0];
y = args[1];
z = args[2];
(which can be trivially generalized to a number of arguments known only at runtime)
Upvotes: 3