编译器错误 CS1657
更新:2007 年 11 月
错误消息
无法将“parameter”作为 ref 或 out 参数传递,因为“reason”
当一个变量在该变量为只读的上下文中作为 ref 或 out 参数传递时,将出现此错误。只读上下文包括 foreach 迭代变量、using 变量和 fixed 变量。若要解决此错误,请不要调用在 using 块、foreach 语句和 fixed 语句中接受 foreach、using 或 fixed 变量作为 ref 或 out 参数的函数。
示例
下面的示例生成 CS1657:
// CS1657.cs
using System;
class C : IDisposable
{
public int i;
public void Dispose() {}
}
class CMain
{
static void f(ref C c)
{
}
static void Main()
{
using (C c = new C())
{
f(ref c); // CS1657
}
}
}
下面的代码说明 fixed 语句中的同样的问题:
// CS1657b.cs
// compile with: /unsafe
unsafe class C
{
static void F(ref int* p)
{
}
static void Main()
{
int[] a = new int[5];
fixed(int* p = a) F(ref p); // CS1657
}
}