2013-02-26 25 views
2

任何人是否有一個想法,爲什麼我得到這些錯誤:修飾符「XXXX」是不適用於這個項目

The modifier 'static' is not valid for this item 

The modifier 'readonly' is not valid for this item 

以下代碼的第7行:

using System; 

namespace XXX 
{ 
    class YYY 
    { 
     private static readonly struct ZZZ 
     { 
      private int x = 0; 
      private int y = 0; 
      private int z = 0; 
     } 
    } 
} 

當我研究此事,我只找到了我不太瞭解的接口的答案,但我只想在我的類中創建一個靜態只讀結構字段。

回答

1

靜態和只讀是mmodifiers只用於實現一個對象,而不是在定義中。當你聲明你將要使用的ZZZ結構體對象時,你可以添加靜態和只讀的修飾符。

using System; 

namespace XXX 
{ 
    class YYY 
    { 
     private struct ZZZ 
     { 
      private int x = 0; 
      private int y = 0; 
      private int z = 0; 
     } 

     private static readonly ZZZ myZZZ = new ZZZ(); //The declaration of a ZZZ instance. 
    } 
} 
相關問題