Sign in to follow this  
UnshavenBastard

[.net] hhrmm, const array in C# ?

Recommended Posts

I want to have an array of const strings, but the compile complains "only variable arrays can be initialized" or something like that. private static string[] strings = { "hello", "there", "whatever" } works, but I don't want just static, I really want: private const string[] strings = { "hello", "there", "whatever" } So, why doesn't the damn compiler like that? I think it makes much sense to have an array of const (iaw. non-modifiable) strings.

Share this post


Link to post
Share on other sites
yeah, I figured, but I don't really neither 'cause the compiler won't let me do this.
and it doesn't work to put the "const" in either place, I should have questioned more general like "is it possible at all to make an array of const items in C#", I guess.

Share this post


Link to post
Share on other sites
Dean Harding    546
C# has the readonly modifier, so you can use the following:


private static readonly string[] strings =
{
"hello", "there", "whatever"
};



It works similar to const, except a readonly field can be initialized in the constructor (whereas a const must be initialized where it is declared).

Share this post


Link to post
Share on other sites
RenZimE    128
i belive you cant use the const keyword with an array because arrays in dot net cantnot be constant due to the way they are declared.

doing
<access modifier> const string[] string = {"yada",yadda","yaadda"};

is the same as doing
<access modifier> const string[] string = new string[] {"yada",yadda","yaadda"};

the only way your going to get any thing thats close to a constant array of some thing is to use the readonly keyword.

Share this post


Link to post
Share on other sites
TheBluMage    372
I've looked long and hard to find a way to create a const array in .Net too, but static readonly is the closest you'll be able to get. It's one of the few things I miss from C++. If what you want is just a readonly, immutable array, you could create a wrapper class (or struct) that would only allow you to get the elements from the array and not set them. It still won't have the small performance benefits of constants, but it will accomplish the same access restrictions.

Share this post


Link to post
Share on other sites
realJJ    163
Reference types cannot have the const modifiers. The only exception to it is the string type. And since Array is a reference type, you can only use the readonly modifier.

Share this post


Link to post
Share on other sites
HellspawnDev    100
You do have another option: Read-Only public properties.

I need to do it with a char array. You can check it out here:

[url="http://hellspawndev.blogspot.com/2011/07/c-const-char.html"]HellspawnDev: C# const char array[/url]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this