【发布时间】:2017-09-01 12:34:49
【问题描述】:
在 c++ 中,可以通过引用 (&) 或指针 (*) 来实现。在 C# 中有“ref”。如何从表中获取值并通过引用对其进行更改?
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
int[] t=new int[3]{1,2,3};
int a=t[0]; //ref int a=t[0];
a+=10;
System.Console.WriteLine("a={0}", a); //11
System.Console.WriteLine("t[0]={0}", t[0]); //1
}
}
}
例如在 C++ 中
int &a=tab[0];
【问题讨论】:
-
这里非常重要的一点是,C# 不是 C++。没有规则说它存在于 C++ 中就必须存在于 C# 中。
标签: c# visual-studio reference ref