我正在考虑使用结构在方法中存储几个状态位,但是我希望能够使用refkeword将其传递给辅助方法以避免传递值.但是,如果我这样做,那么将存储在堆上,而不是堆栈中?
示例代码:
var link = new Geoff("Bergen");
Perambulate(ref link);
Console.WriteLine(link.Name);
void Perambulate(ref Geoff man)
{
Console.WriteLine("Perambulating {0}",man.Name);
}
struct Geoff
{
public readonly string Name;
public Geoff(string name)
{
Name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我想我真的在问这个ref关键字是否强制将引用的值存储在堆上.
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |