c# - C#方法如何改变参数的值
问题描述
我对下面的代码如何工作感到困惑。为什么输出不是 10 而是 5?
public class Program
{
public void MyFunc(int x){
x = 10;
}
public void Main()
{
int x = 5;
MyFunc(x);
Console.WriteLine(x);
}
}
非常感谢。
解决方案
阅读“ref 关键字” 现在结果是 10
public static void MyFunc(ref int x)
{
x = 10;
}
public static void Main()
{
int x = 5;
MyFunc(ref x);
Console.WriteLine(x);
}
推荐阅读
- asp.net-mvc - 仅为更新的文件创建 nuget 包
- maven - 无法使用 Gradle 插件在 Jenkins 中获取类型为 DefaultGroovyMavenDeployer 的对象的未知属性
- elasticsearch - Elasticsearch 查询 - 组合查询
- vue.js - Vuex mapState 设置状态
- sqlite - SQLite 返回的最小值大于最大值
- c++ - 无法编译 C++ 代码
- javascript - 跟踪html5视频控制区域中的点击播放按钮
- android - 移动应用安全
- excel - Excel VBA:列充满不可见的内容,直到结束(第 16,384 列)
- reactjs - React/Redux/Router 客户端登录凭据 - 受限页面