相关疑难解决方法(0)


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

[TestClass]
public class MsProjectIntegration {
    const int? projectID = null;
    // The type 'int?' cannot be declared const
    // ...
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能拥有const int?

编辑:我想要一个可空的int作为const的原因是因为我只是用它来从数据库加载一些示例数据.如果它为null我只是在运行时初始化样本数据.这是一个非常快速的测试项目,显然我可以使用0或-1,但int?感觉就像我想做的那样正确的数据结构.readonly似乎是要走的路

c# null nullable const

14
推荐指数
3
解决办法
2720
查看次数

将Color声明为常量

如何将Color类型声明为const:

private const Color MyLovedColor = Color.Blue;
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为Color.Blue是静态而不是const.

(readonly不会帮助我,因为我需要一个"仅支持"常量的属性的颜色

.net c#

9
推荐指数
2
解决办法
9676
查看次数

标签 统计

c# ×3

const ×2

.net ×1

null ×1

nullable ×1

stack ×1

struct ×1