Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 常量列表的内联实例化_C#_.net - Fatal编程技术网

C# 常量列表的内联实例化

C# 常量列表的内联实例化,c#,.net,C#,.net,我尝试这样做: public const List<String> METRICS = new List<String>() { SourceFile.LOC, SourceFile.MCCABE, SourceFile.NOM, SourceFile.NOA, SourceFile.FANOUT, SourceF

我尝试这样做:

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
        };
    List<string> tagList = new List<string>(new[]
    {
         "A"
        ,"B"
        ,"C"
        ,"D"
        ,"E"
    });
public const List METRICS=new List()
{
SourceFile.LOC,
SourceFile.MCCABE,
SourceFile.NOM,
SourceFile.NOA,
SourceFile.FANOUT,
SourceFile.FANIN,
SourceFile.NOPAR,
SourceFile.NDC,
SourceFile.CALLS
};
但不幸的是,这不起作用:

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.
FileStorer.METRICS”的类型为“System.Collections.Generic.List”。引用类型不是字符串的常量字段只能用null初始化。

如何解决此问题?

您将需要使用
静态
只读
列表。如果您希望列表是不可变的,那么您可能需要考虑使用<代码> RealDyLyCys<代码>而不是<代码>列表< /C> > < /P>
private static readonly readonly collection\u度量=
新建只读集合(新建[]
{
SourceFile.LOC,
SourceFile.MCCABE,
SourceFile.NOM,
SourceFile.NOA,
SourceFile.FANOUT,
SourceFile.FANIN,
SourceFile.NOPAR,
SourceFile.NDC,
SourceFile.CALLS
});
公共静态只读收集度量
{
获取{return\u metrics;}
}

常量
用于编译时常量。您可以只将其设置为
静态只读
,但这只适用于
度量
变量本身(通常情况下,它应该是度量)。它不会使列表不可变-因此有人可以调用
METRICS.Add(“不应该在这里”)

您可能需要使用
ReadOnlyCollection
来包装它。例如:

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 });
public static readonly IList Metrics=new readonly集合
(新名单{
SourceFile.LoC、SourceFile.McCabe、SourceFile.NoM、,
SourceFile.NoA、SourceFile.FanOut、SourceFile.FanIn、,
源文件.PAR,源文件.NDC,SooSeCuff.Calp};
ReadOnlyCollection
只是包装了一个可能可变的集合,但由于此后没有其他对象可以访问
列表,因此您可以将整个集合视为不可变的

(这里的大写大多是猜测——在我看来,使用更完整的名称会更清楚。)


您是否将其声明为
IList
IEnumerable
ReadOnlyCollection
或其他内容取决于您。。。如果您希望它只被视为一个序列,那么
IEnumerable
可能是最合适的。如果顺序很重要,并且您希望人们能够通过索引访问它,
IList
可能是合适的。如果您想使不变性变得明显,那么将其声明为
ReadOnlyCollection
可能很方便,但不灵活。

您正在寻找一个简单的代码,如下所示:

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
        };
    List<string> tagList = new List<string>(new[]
    {
         "A"
        ,"B"
        ,"C"
        ,"D"
        ,"E"
    });
List tagList=新列表(新[]
{
“A”
,“B”
,“C”
,“D”
,“E”
});

注意:将此问题作为重复问题结束是一个错误。链接的问题是关于常量数组的,这个问题是关于常量列表的。尽管这两种方法提供的功能大致相同,但它们不同,答案也不同。只有此问题的答案提到了
只读集合
。问题是相关的,但两者都不是重复的。我来这里寻找
新列表{…}
部分。谢谢Jon:)我想这是有原因的,但对我来说,
ReadOnlyCollection
实现了
IList
,这暴露了
。Add
这并不能回答问题,因为提问者需要实例化属性。