編譯器錯誤 CS0459
更新: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;
}