Solved

How do you const-ify a string array literal in C# ?

Posted on 2010-09-16
6
681 Views
Last Modified: 2013-12-16
In C# I'd like to specify that an array with it's literal values cannot be modified in any way.  Is this possible in C#?

Using readonly keyword doesn't seem to be complete...  the seal keyword doesn't compile (only for classes)... and the const keyword doesn't compile because arrays are reference types.
public const string[] BANDS = new string[] {"Rush", "Banco De Gaia"};

Open in new window

0
Comment
Question by:ZuZuPetals
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33693515
try this
public class Class
    {
        private IList Bands;

        public Class()
        {
            Bands = ArrayList.ReadOnly(new string[] { "Rush", "Banco De Gaia" });
        }
    }Tr

Open in new window

0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 500 total points
ID: 33693541
Sorry forgot mention that string[] can be assigned to IList reference and hence the above code will not introduce any unwanted changes to your existing code.

IList Bands = new string[] { "Rush", "Banco De Gaia" };  // No compile error

And hence

IList Bands = ArrayList.ReadOnly(new string[] { "Rush", "Banco De Gaia" });  // Should work the same way as above
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33693946
yes can use ArrayList.ReadOnly(IList) method as suggested by novice_novice

as it is internally creates ReadOnlyList

   public static IList ReadOnly(IList list)
{    
if (list == null)
 {
 throw new ArgumentNullException("list");
    }    
return new ReadOnlyList(list);
}


and better than that you can use ReadOnlyCollection<T> Class

ReadOnlyCollection<string> BANDS = new ReadOnlyCollection<string>( new string[] {"Rush", "Banco De Gaia"});



0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Author Comment

by:ZuZuPetals
ID: 33694457
Golly.

I appreciate the solution but it's syntactic molasses.

I wish .Net inventors had just let us add the keyword const or perhaps a new keyword like fixed (to get around the underlying technical reasons--reference, etc.) to make the string values immutable.
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 33694592
Yes, even I will be happy to see this type of implementation but for now  

ReadOnlyCollection is the best way to achieve this.

http://msdn.microsoft.com/en-us/library/ms132474.aspx
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33694639
C# does not support Constant Array (Contents are constants), You will have to live with it.

The choice whether List over Collection should fit your needs not fancy.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question