Solved

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

Posted on 2010-09-16
6
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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