Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method with ref object parameter

Tags:

c#

asp.net

Hi i have to call a method that has this signature:

int MethodName(ref object vIndexKey)

If i try to call it with

String c = "690";

MethodName(ref (object) c);

It doesn't work.

How can i do?

thanks

like image 393
Luca Romagnoli Avatar asked Dec 22 '22 05:12

Luca Romagnoli


1 Answers

You need to do it like this:

String c = "690"; 
object o = (object) c;
MethodName(ref o);

The reason is that the parameter must be assignable by the function. The function could do something like this:

o = new List<int>();

Which is not possible if the underlying type is a string that has been casted to an object during the method call, because the target of the assignment would still be a string and not an object.

like image 79
Klaus Byskov Pedersen Avatar answered Jan 02 '23 08:01

Klaus Byskov Pedersen