更新:2007 年 11 月
错误消息
不能采用只读局部变量的地址
在 C# 语言中有三种生成只读局部变量的常见情况:foreach、using 和 fixed。在每一种情况中,您都不能写入只读局部变量或者采用它的地址。当编译器意识到您正在试图采用只读局部变量的地址时,将产生此错误。
示例
当您试图在 foreach 循环和 fixed 语句块中采用只读局部变量的地址时,下面的示例将生成 CS0459。
// CS0459.cs
// compile with: /unsafe
class A
{
public unsafe void M1()
{
int[] ints = new int[] { 1, 2, 3 };
foreach (int i in ints)
{
int *j = &i; // CS0459
}
fixed (int *i = &_i)
{
int **j = &i; // CS0459
}
}
private int _i = 0;
}