Solved

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

Posted on 2010-09-16
6
673 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now