array of string

F

Francois

Hi,

In c#, is it possible to have an array of string declared as being constant:

something like:

protected const string[] OptionsGameFilterValues = new String[] {"Tom",
"Jon", "Henry"};

but this code does not compile.

However the MSDN documentation says that const modifier keyword can apply to
any reference type. An array is a reference type. And it is an array of
string then the values can be defined at compile time... Then what is wrong?
How to declare a constant array of string?

Thanks in advance.

Best regards,

Francois
 
J

Jako Menkveld

Francois

I think the problem lies in that the value you are trying to assign to the
array is not constant (you use the 'new' keyword).

Try making it readonly instead.

protected static readonly string[].... (the static is obviously optional)

Hope this helps.

Cheers
Jako
 
B

Bruce Wood

I'm not sure what the documentation was getting at, but you cannot make
complex constants in C#.

You can declare the array as readonly, but not even this guarantees
that someone can't change the array. (It only guarantees that nobody
can change the reference to point to a different array. That is, the
_array reference_ becomes read-only; the array itself is still mutable.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top