在 C# 中声明 const double[]?

发布于 2024-07-26 04:34:53 字数 326 浏览 1 评论 0原文

我使用了几个常量,我的计划是将它们放入 const 双精度数组中,但是编译器不允许我这样做。

我尝试以这种方式声明它:

const double[] arr = {1, 2, 3, 4, 5, 6, 73, 8, 9 };

然后我决定将其声明为静态只读:

static readonly double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9};

但是问题仍然存在。 为什么编译器不允许我声明 const 值数组? 或者会,但我只是不知道如何?

I have several constants that I use, and my plan was to put them in a const array of doubles, however the compiler won't let me.

I have tried declaring it this way:

const double[] arr = {1, 2, 3, 4, 5, 6, 73, 8, 9 };

Then I settled on declaring it as static readonly:

static readonly double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9};

However the question remains. Why won't compiler let me declare an array of const values? Or will it, and I just don't know how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光无声 2024-08-02 04:34:53

这可能是因为

static const double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9};

实际上与说

static const double[] arr = new double[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9};

分配给 const 的值必须是... const 相同。 每个引用类型都不是常量,而数组是引用类型。

我的研究表明,解决方案是使用静态只读。 或者,在您的情况下,使用固定数量的双精度数,为所有内容提供单独的标识符。


编辑(2):
一个小sidenode,每个类型都可以使用const,但是分配给它的值必须是const。 对于引用类型,唯一可以分配的是 null:

static const double[] arr = null;

但这完全没有用。 字符串是例外,它们也是唯一可用于属性参数的引用类型。

This is probably because

static const double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9};

is in fact the same as saying

static const double[] arr = new double[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9};

A value assigned to a const has to be... const. Every reference type is not constant, and an array is a reference type.

The solution, my research showed, was using a static readonly. Or, in your case, with a fixed number of doubles, give everything an individual identifier.


Edit(2):
A little sidenode, every type can be used const, but the value assigned to it must be const. For reference types, the only thing you can assign is null:

static const double[] arr = null;

But this is completely useless. Strings are the exception, these are also the only reference type which can be used for attribute arguments.

红尘作伴 2024-08-02 04:34:53

来自 MSDN (http://msdn.microsoft.com/en-us/library/ ms228606.aspx)

常量表达式是一个表达式
可以在以下位置进行全面评估
编译时。 因为唯一的方法是
创建一个非空值a
引用类型[数组]是应用新的
运算符,并且因为新运算符
不允许在
常量表达式,唯一可能的
引用类型常量的值
除字符串之外的其他内容为空。

From MSDN (http://msdn.microsoft.com/en-us/library/ms228606.aspx)

A constant-expression is an expression
that can be fully evaluated at
compile-time. Because the only way to
create a non-null value of a
reference-type [an array] is to apply the new
operator, and because the new operator
is not permitted in a
constant-expression, the only possible
value for constants of reference-types
other than string is null.

肥爪爪 2024-08-02 04:34:53

C# 中无法使用 const 数组。 您需要使用索引器、属性等来确保数组的内容不被修改。 您可能需要重新评估班级的公共方面。

只是要指出...静态只读 - 不是常量 -

这是完全有效的,而不是您想要的:

class TestClass
{
    public static readonly string[] q = { "q", "w", "e" };
}

class Program
{
    static void Main( string[] args )
    {
        TestClass.q[ 0 ] = "I am not const";
        Console.WriteLine( TestClass.q[ 0 ] );
    }
}

您将需要找到其他方法来保护您的阵列。

There is no way to have a const array in C#. You need to use indexers, properties, etc to ensure the contents of the array are not modified. You may need to re-evaluate the public side of your class.

Just to point out though... Static readonly -IS NOT CONST-

This is perfectly valid and not what you were wanting:

class TestClass
{
    public static readonly string[] q = { "q", "w", "e" };
}

class Program
{
    static void Main( string[] args )
    {
        TestClass.q[ 0 ] = "I am not const";
        Console.WriteLine( TestClass.q[ 0 ] );
    }
}

You will need to find other ways to protect your array.

(り薆情海 2024-08-02 04:34:53

我不知道为什么你需要将其设置为常量或只读。 如果你真的想让整个数组不可变,那么简单的常量/只读关键字不会帮助你,更糟糕的是,它还可能将你引向错误的方向。

对于任何非不可变引用类型,将它们设置为只读意味着您永远无法重新分配变量本身,但内容仍然是可变的。 请参阅下面的示例:

readonly double[] a = new double[]{1, 2, 3};
...
a = new double[] {2,3}; // this won't compile;
a[1] = 4; // this will compile, run and result the array to {1, 4, 3}

根据您的上下文,可能有一些解决方案,其中之一是,如果您真正需要的是双精度列表,则 List a = new List() {1,2,3,4,5}。 AsReadOnly(); 将为您提供一个双内容只读列表。

I don't know why you needed to make it either constant or readonly. If you really want to make the whole array immutable, then a simple constant/readonly keyword will not help you, and what's worse is, it might also divert you to the wrong way.

For any non-immutable reference types, make them readonly only means you can never re-assign the variable itself, but the content is still changeable. See below example:

readonly double[] a = new double[]{1, 2, 3};
...
a = new double[] {2,3}; // this won't compile;
a[1] = 4; // this will compile, run and result the array to {1, 4, 3}

Depending on your context, there might be some solutions, one of them is, if what you really need is a list of double, List a = new List() {1,2,3,4,5}.AsReadOnly(); will give you a content-readonly list of double.

九命猫 2024-08-02 04:34:53

问题是您声明的是双精度常数数组,而不是双精度常数数组。 由于 C# 中数组的工作方式,我认为没有办法拥有常量数组。

The problem is that you're declaring a constant array of double, not an array of constant doubles. I don't think there is a way to have an array of constants due to the way arrays work in C#.

夏有森光若流苏 2024-08-02 04:34:53

编译器错误准确地告诉您为什么不能这样做:

“arr”的类型为“double[]”。
除字符串之外的引用类型的 const 字段只能用 null 初始化。

The compiler error tells you exactly why you can't do it:

'arr' is of type 'double[]'.
A const field of a reference type other than string can only be initialized with null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文