我正在学习使用 ref,但不明白为什么会出现错误?
class A
{
public void ret(ref int variable)
{
variable = 7;
}
static int Main()
{
int z = 5;
ret(ref z); // Error: Need a reference on object
Console.WriteLine(z); // it will be 7 as I understand
return 0;
}
}
最佳答案
问题不在于 ref
参数。就是 ret
是一个实例方法,如果不引用该类型的实例,就不能调用实例方法。
尝试使 ret
静态化:
public static void ret(ref int variable)
{
variable = 7;
}
关于c# - 如何使用引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514890/