Declaring a const double[] in C#?(在 C# 中声明一个 const double[]?)
问题描述
我有几个我使用的常量,我的计划是把它们放在一个双精度的 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.
我试过这样声明:
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};
但是问题仍然存在.为什么编译器不让我声明一个 const 值数组?或者会,我只是不知道怎么做?
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?
推荐答案
来自 MSDN (http://msdn.microsoft.com/en-us/library/ms228606.aspx)
常量表达式是一个表达式可以在编译时.因为只有这样创建 a 的非空值reference-type [an array] 是应用新的运算符,并且因为 new 运算符不允许在常量表达式,唯一可能的引用类型常量的值字符串以外的为空.
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.
这篇关于在 C# 中声明一个 const double[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# 中声明一个 const double[]?
基础教程推荐
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
