Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline instantiation of a constant List

Tags:

c#

.net

I try to do something like this:

public const List<String> METRICS = new List<String>()         {             SourceFile.LOC,             SourceFile.MCCABE,             SourceFile.NOM,             SourceFile.NOA,             SourceFile.FANOUT,             SourceFile.FANIN,             SourceFile.NOPAR,             SourceFile.NDC,             SourceFile.CALLS         }; 

But unfortunately this doesn't work:

FileStorer.METRICS' is of type 'System.Collections.Generic.List<string>'. A const field of a reference type other than string can only be initialized with null. 

How can I solve this problem?

like image 470
RoflcoptrException Avatar asked Jan 12 '11 11:01

RoflcoptrException


People also ask

How do you check if a string is present in a list of strings in C#?

CSharp Online TrainingContains() is a string method. This method is used to check whether the substring occurs within a given string or not. It returns the boolean value. If substring exists in string or value is the empty string (“”), then it returns True, otherwise returns False.

What is constant and read only C#?

readonly keyword is used to define a variable which can be assigned once after declaration either during declaration or in constructor. const keyword is used to define a constant to be used in the program. Following is the valid usage of a readonly and const keywords in C#.


2 Answers

const is for compile-time constants. You could just make it static readonly, but that would only apply to the METRICS variable itself (which should typically be Metrics instead, by .NET naming conventions). It wouldn't make the list immutable - so someone could call METRICS.Add("shouldn't be here");

You may want to use a ReadOnlyCollection<T> to wrap it. For example:

public static readonly IList<String> Metrics = new ReadOnlyCollection<string>     (new List<String> {           SourceFile.LoC, SourceFile.McCabe, SourceFile.NoM,          SourceFile.NoA, SourceFile.FanOut, SourceFile.FanIn,           SourceFile.Par, SourceFile.Ndc, SourceFile.Calls }); 

ReadOnlyCollection<T> just wraps a potentially-mutable collection, but as nothing else will have access to the List<T> afterwards, you can regard the overall collection as immutable.

(The capitalization here is mostly guesswork - using fuller names would make them clearer, IMO.)

Whether you declare it as IList<string>, IEnumerable<string>, ReadOnlyCollection<string> or something else is up to you... if you expect that it should only be treated as a sequence, then IEnumerable<string> would probably be most appropriate. If the order matters and you want people to be able to access it by index, IList<T> may be appropriate. If you want to make the immutability apparent, declaring it as ReadOnlyCollection<T> could be handy - but inflexible.

like image 103
Jon Skeet Avatar answered Sep 18 '22 07:09

Jon Skeet


You'll need to use a static readonly list instead. And if you want the list to be immutable then you might want to consider using ReadOnlyCollection<T> rather than List<T>.

private static readonly ReadOnlyCollection<string> _metrics =     new ReadOnlyCollection<string>(new[]         {             SourceFile.LOC,             SourceFile.MCCABE,             SourceFile.NOM,             SourceFile.NOA,             SourceFile.FANOUT,             SourceFile.FANIN,             SourceFile.NOPAR,             SourceFile.NDC,             SourceFile.CALLS         });  public static ReadOnlyCollection<string> Metrics {     get { return _metrics; } } 
like image 42
LukeH Avatar answered Sep 19 '22 07:09

LukeH