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?
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.
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.
Declare it as readonly
instead of const
:
public readonly string[] names = { "alpha", "beta", "gamma", "delta" };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With