Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const array of strings [duplicate]

Possible Duplicate:
Declare a Const Array

I need an array of const strings in the class. Something like

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

But this code causes an error:

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

What should I do?

like image 487
Newbee Avatar asked Dec 28 '12 00:12

Newbee


People also ask

Can arrays be const?

Arrays are Not Constants It does NOT define a constant array. It defines a constant reference to an array. Because of this, we can still change the elements of a constant array.

How do you check if an array of objects has duplicate values in JavaScript?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.


1 Answers

Declare it as readonly instead of const:

public readonly string[] names = { "alpha", "beta", "gamma", "delta" }; 
like image 152
John Woo Avatar answered Oct 05 '22 17:10

John Woo