在 C# 中声明 const double[]?
我使用了几个常量,我的计划是将它们放入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这可能是因为
实际上与说
分配给 const 的值必须是... const 相同。 每个引用类型都不是常量,而数组是引用类型。
我的研究表明,解决方案是使用静态只读。 或者,在您的情况下,使用固定数量的双精度数,为所有内容提供单独的标识符。
编辑(2):
一个小sidenode,每个类型都可以使用const,但是分配给它的值必须是const。 对于引用类型,唯一可以分配的是 null:
但这完全没有用。 字符串是例外,它们也是唯一可用于属性参数的引用类型。
This is probably because
is in fact the same as saying
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:
But this is completely useless. Strings are the exception, these are also the only reference type which can be used for attribute arguments.
来自 MSDN (http://msdn.microsoft.com/en-us/library/ ms228606.aspx)
From MSDN (http://msdn.microsoft.com/en-us/library/ms228606.aspx)
C# 中无法使用 const 数组。 您需要使用索引器、属性等来确保数组的内容不被修改。 您可能需要重新评估班级的公共方面。
只是要指出...静态只读 - 不是常量 -
这是完全有效的,而不是您想要的:
您将需要找到其他方法来保护您的阵列。
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:
You will need to find other ways to protect your array.
我不知道为什么你需要将其设置为常量或只读。 如果你真的想让整个数组不可变,那么简单的常量/只读关键字不会帮助你,更糟糕的是,它还可能将你引向错误的方向。
对于任何非不可变引用类型,将它们设置为只读意味着您永远无法重新分配变量本身,但内容仍然是可变的。 请参阅下面的示例:
根据您的上下文,可能有一些解决方案,其中之一是,如果您真正需要的是双精度列表,则 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:
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.
问题是您声明的是双精度常数数组,而不是双精度常数数组。 由于 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#.
编译器错误准确地告诉您为什么不能这样做:
The compiler error tells you exactly why you can't do it: