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?
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.
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#.
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.
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; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With