是否可以写出类似于下面的内容?

public const string[] Titles = { "German", "Spanish", "Corrects", "Wrongs" };

当前回答

从c# 6开始,你可以这样写:

public static string[] Titles => new string[] { "German", "Spanish", "Corrects", "Wrongs" };

参见:c#:新的和改进的c# 6.0(特别是“表达式体函数和属性”一章)

这将使一个只读的静态属性,但它仍然允许您更改返回的数组的内容,但当您再次调用该属性时,您将再次获得原始的、未更改的数组。

为了澄清,这段代码与(或实际上是缩写)相同:

public static string[] Titles
{
    get { return new string[] { "German", "Spanish", "Corrects", "Wrongs" }; }
}

请注意,这种方法有一个缺点:每个引用实际上都会实例化一个新数组,因此如果您使用的是一个非常大的数组,这可能不是最有效的解决方案。 但是如果你重复使用相同的数组(例如把它放在一个私有属性中),它将再次打开改变数组内容的可能性。

如果你想要一个不可变数组(或列表),你也可以使用:

public static IReadOnlyList<string> Titles { get; } = new string[] { "German", "Spanish", "Corrects", "Wrongs" };

但是,这仍然有更改的风险,因为你仍然可以将其转换回字符串[]并更改内容,如下所示:

((string[]) Titles)[1] = "French";

其他回答

为了我的需要,我定义了静态数组,而不是不可能的const,它工作: 公共静态字符串[]title ={"德语","西班牙语","纠正","错误"};

快速的解决方法,说不定对别人有帮助。我需要一个字符串[]作为Attribute的参数(在某些理论中为测试用例传递内联数据)。Readonly在这里没用。但是,我最后做的是:

const string foo = "a|b|c";

[InlineData(foo)]
public void Test(string fooString)
{
    var foo = fooString.Split("|"); // foo == {"a","b","c"}
    ...
}

你可以采用一种不同的方法:定义一个常量字符串来表示你的数组,然后在你需要它的时候把字符串分割成一个数组。

const string DefaultDistances = "5,10,15,20,25,30,40,50";
public static readonly string[] distances = DefaultDistances.Split(',');

这种方法为您提供了一个可以存储在配置中并在需要时转换为数组的常量。

如果你在IReadOnlyList接口后面声明了一个数组,你会得到一个常量数组,在运行时声明常量值:

public readonly IReadOnlyList<string> Titles = new [] {"German", "Spanish", "Corrects", "Wrongs" };

在. net 4.5及更高版本中可用。

.NET Framework v4.5+解决方案,改进了tdbeckett的答案:

using System.Collections.ObjectModel;

// ...

public ReadOnlyCollection<string> Titles { get; } = new ReadOnlyCollection<string>(
  new string[] { "German", "Spanish", "Corrects", "Wrongs" }
);

注意:假定集合在概念上是常量,在类级别声明它时将其设置为静态可能是有意义的。

上面的:

Initializes the property's implicit backing field once with the array. Note that { get; } - i.e., declaring only a property getter - is what makes the property itself implicitly read-only (trying to combine readonly with { get; } is actually a syntax error). Alternatively, you could just omit the { get; } and add readonly to create a field instead of a property, as in the question, but exposing public data members as properties rather than fields is a good habit to form. Creates an array-like structure (allowing indexed access) that is truly and robustly read-only (conceptually constant, once created), both with respect to: preventing modification of the collection as a whole (such as by removing or adding elements, or by assigning a new collection to the variable). preventing modification of individual elements. (Even indirect modification isn't possible - unlike with an IReadOnlyList<T> solution, where a (string[]) cast can be used to gain write access to the elements, as shown in mjepsen's helpful answer. The same vulnerability applies to the IReadOnlyCollection<T> interface, which, despite the similarity in name to class ReadOnlyCollection, does not even support indexed access, making it fundamentally unsuitable for providing array-like access.)