2012-12-28 13 views
20

Possible Duplicate:
Declare a Const Array文字列のコンス配列

クラスにconst文字列の配列が必要です。

public class some_class_t 
{ 
    public const string[] names = new string[4] 
    { 
     "alpha", "beta", "gamma", "delta" 
    }; 
} 

のようなもの。しかし、このコードは、エラーが発生します。

A constant 'names' of reference type 'string[]' can only be initialized with null.

私は何をすべき?

+1

配列は変更可能です。ヌルでない限り、定数にすることはできません。 –

+0

'readonly'として宣言する –

答えて

57

ではなくconstreadonlyとしてそれを宣言します。

public readonly string[] names = { "alpha", "beta", "gamma", "delta" }; 
+0

詳細は[http://stackoverflow.com/a/5142378/1080355](http://stackoverflow.com/a/5142378/1080355) – VSB

+2

で入手できます。 'public readonly string [] names = {" alpha "、" beta "、" gamma "、" delta "};'を使用します。 –