c# - C# 中的引用问题

标签 c# .net pass-by-reference

简单地说,我正在使用一个 while 循环来重复一个方法,每次运行该方法时,int "i"都会增加 1。尽管我在调用 "NumberUp"方法时遇到了麻烦。错误输出如下。

主要方法:

while (true)
{
    NumberUp(0);
}

NumberUp 方法:

public static void NumberUp(ref int i)
{
    i++;
    System.Console.WriteLine(i);
}

我不断收到以下错误:

The best overloaded method match for 'ConsoleApplication2.Program.NumberUp(ref int)' has some invalid arguments

最佳答案

要调用带有ref参数的方法,您需要传递一个变量,并使用ref关键字:

int x = 0;
NumberUp(ref x);
//x is now 1

这会将一个引用传递给x变量,允许NumberUp方法将一个新值放入变量中。

关于c# - C# 中的引用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011926/

相关文章:

c# - wcf单向非阻塞操作

c# - 不可变对象(immutable对象)是好的做法吗?

c++ - 候选函数不可行 : expects an l-value for 3rd argument

c++ - 默认情况下,c++ 中的所有内容都是按值传递的

c# - 如何使用类似插件的架构来扩展 WPF 应用程序?

c# - Win7 上的 %TEMP% 中未找到文件

c# - Console.WriteLine 在 VS2012 中不再工作

c# - 当前不支持 LastResultOperator 结果运算符

.net - 访问 .NET 服务时,WebORB 会增加什么值(value)?

java - 为什么 Java 不允许 C# "out"特性用于方法参数