Jump to content
  • Advertisement
Sign in to follow this  
UnshavenBastard

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

This topic is 2574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!