为什么不能将 nullable 声明为 const?

发布于 2024-08-13 01:33:51 字数 383 浏览 3 评论 0原文

[TestClass]
public class MsProjectIntegration {
    const int? projectID = null;
    // The type 'int?' cannot be declared const
    // ...
}

为什么我不能有 const int?

编辑: 我想要一个可空 int 作为 const 的原因是因为我只是使用它从数据库加载一些示例数据。如果它为空,我将在运行时初始化示例数据。这是一个非常快速的测试项目,显然我可以使用 0 或 -1,但 int? 感觉像是适合我想做的事情的正确数据结构。只读似乎是可行的方法

[TestClass]
public class MsProjectIntegration {
    const int? projectID = null;
    // The type 'int?' cannot be declared const
    // ...
}

Why can't I have a const int??

Edit: The reason I wanted a nullable int as a const is because I'm just using it for loading some sample data from a database. If it's null I was just going to initialize sample data at runtime. It's a really quick test project and obviously I could use 0 or -1 but int? just felt like the right data structure for what I wanted to do. readonly seems like the way to go

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

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

发布评论

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

评论(5

七度光 2024-08-20 01:33:51

这不仅仅是可空值;只有运行时内置的类型才能声明为 const(从内存中看,它是 bool、各种类型的 int、float/double 和字符串)。

为什么?因为该值在编译时直接嵌入到程序集中,并且无法嵌入用户定义的类型。

不过,readonly 关键字应该可以满足您的需要。与 const 相比,任何 readonly 字段都会在运行时而不是编译时初始化,因此可以使用或多或少的任何您想要的表达式来初始化它们。

编辑:正如 Eric Lippert 指出的那样,事情并不是这么简单。例如,constdecimal 有效。

这:

private const decimal TheAnswer = 42;

...编译(好吧,反射器)为:

[DecimalConstant(0, 0, (uint) 0, (uint) 0, (uint) 42)]
private static readonly decimal TheAnswer;

It's not just nullables; only types built into the runtime can be declared const (from memory, it's bools, the various types of int, floats/doubles, and strings).

Why? Because the value gets embedded directly into the assembly at compile time, and there's no way to embed user-defined types.

The readonly keyword should do what you need, however. By contrast with const, any readonly fields get initialized at runtime rather than compile time, so they can be initialized with more or less any expression you want.

Edit: as Eric Lippert points out, it's not this straightforward. For instance, const decimal works.

This:

private const decimal TheAnswer = 42;

...compiles (well, Reflectors) to this:

[DecimalConstant(0, 0, (uint) 0, (uint) 0, (uint) 42)]
private static readonly decimal TheAnswer;
撕心裂肺的伤痛 2024-08-20 01:33:51

http://en.csharp-online.net/const,_static_and_readonly

常量必须是整型
(sbyte、byte、short、ushort、int、
uint、long、ulong、char、float、
双精度型、小数型、布尔型或字符串型)、
枚举,或对 null 的引用。

由于类或结构是
在运行时用新的初始化
关键字,而不是在编译时,您
无法为类设置常量或
结构。

由于 nullable 是一个结构体,因此上面的引用就是原因。

http://en.csharp-online.net/const,_static_and_readonly

Constants must be of an integral type
(sbyte, byte, short, ushort, int,
uint, long, ulong, char, float,
double, decimal, bool, or string), an
enumeration, or a reference to null.

Since classes or structures are
initialized at run time with the new
keyword, and not at compile time, you
can't set a constant to a class or
structure.

Since nullable is a struct, the above quote is the reason why.

守护在此方 2024-08-20 01:33:51

你不能有一个 const 引用类型(或一个结构),因此你不能有一个 const int ?这实际上只是一个 Nullable

您可以将其标记为只读,

readonly int? projectID = null;

然后就不能在类构造函数之外对其进行修改。

You can't have a const reference type (or a struct), therefore you can't have a const int? which is really just a Nullable<int>.

You can mark it as readonly

readonly int? projectID = null;

Then it can't be modified outside the class constructors.

○闲身 2024-08-20 01:33:51

您可能需要考虑使用“readonly”修饰符。

const 在编译时评估,而 readonly 在运行时强制执行。复杂类型的实例无法编译到程序集中,因此必须在运行时创建。

You may want to consider using the "readonly" modifier instead.

consts are evaluated at compile time, whereas readonlys are enforced at run time. Instances of complex types cannot be compiled into the assembly, and so must be created at runtime.

浮世清欢 2024-08-20 01:33:51

你基本上是在说:

我有一个带有projectId字段的类
可能有也可能没有价值,但是
事实上永远没有价值,它是
始终是未定义的。

从逻辑的角度来看……声明本身没有任何意义。

You're basically saying:

I have a class with a projectId field
that may or may not have a value, but
that in fact NEVER has a value, it's
is always undefined.

From a logical point of view... the declaration itself makes no sense.

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