在 C# 中声明一个 const double[]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1109805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring a const double[] in C#?
提问by David Bo?jak
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?
然而问题依然存在。为什么编译器不让我声明一个 const 值数组?或者会,我只是不知道如何?
采纳答案by JohnFx
From MSDN (http://msdn.microsoft.com/en-us/library/ms228606.aspx)
来自 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.
常量表达式是可以在编译时完全计算的表达式。因为创建引用类型 [数组] 的非空值的唯一方法是应用 new 运算符,并且因为常量表达式中不允许使用 new 运算符,所以引用常量的唯一可能值 -字符串以外的类型为空。
回答by Dykam
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 a array is an reference type.
分配给 const 的值必须是... const。每个引用类型都不是常量,数组是一种引用类型。
The solution my research showed was using an static readonly. Or, in your case with a fixed number of doubles, give everything a 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:
Edit(2):一个小sidenode,每个类型都可以用const,但是赋值给它的值必须是const。对于引用类型,您唯一可以赋值的是 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.
但这完全没有用。字符串是个例外,它们也是唯一可用于属性参数的引用类型。
回答by LukeH
The compiler errortells 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.
“arr”是“double[]”类型。
字符串以外的引用类型的 const 字段只能用 null 初始化。
回答by Yohnny
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#.
问题是您声明的是一个常量数组,而不是常量双精度数组。由于数组在 C# 中的工作方式,我认为没有办法拥有常量数组。
回答by Karl Strings
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.
在 C# 中无法使用 const 数组。您需要使用索引器、属性等来确保数组的内容不被修改。您可能需要重新评估班级的公共方面。
Just to point out though... Static readonly -IS NOT CONST-
只是要指出...静态只读 - 不是 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.
您将需要找到其他方法来保护您的阵列。
回答by louis
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.
我不知道你为什么需要让它保持不变或只读。如果你真的想让整个数组不可变,那么一个简单的 constant/readonly 关键字对你没有帮助,更糟糕的是,它也可能让你转向错误的方式。
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.
根据您的上下文,可能有一些解决方案,其中之一是,如果您真正需要的是双精度列表,则 List a = new List() {1,2,3,4,5}.AsReadOnly(); 会给你一个内容只读的双精度列表。