简体   繁体   English

如何在C#中创建公共const大小?

[英]How can I create a public const Size in C#?

I am trying to write the following code: 我正在尝试编写以下代码:

public const Size ImageSize = new Size() { Width = 28, Height = 28 };

But I get the error that Width and Height are read-only. 但我得到WidthHeight是只读的错误。

What is the recommended way to do this? 建议的方法是什么?

The fundamental problem is that you cannot declare an object of type System.Drawing.Size as const . 根本问题是您不能将System.Drawing.Size类型的对象声明为const That indicates that the symbol is to be replaced at compile-time with the value of the constant. 这表明该符号将在编译时被替换为常量的值。

Instead, you should use readonly . 相反,你应该使用readonly This is also a "constant" value in the sense that it cannot be modified once the constructor has run, but the objects are created at run-time instead of compile-time. 这也是一个“常量”值,因为它在构造函数运行后无法修改,但是对象是在运行时而不是编译时创建的。

The following code compiles just fine: 以下代码编译得很好:

public static readonly Size ImageSize = new Size() { Width = 28, Height = 28 };

const is restricted to primitives that the compiler can directly write as IL directly. const仅限于编译器可以直接写为IL的原语。 readonly should suffice here if Size is treated as immutable, ie 如果将 Size视为不可变,即readonly就足够了

public static readonly Size ImageSize = new Size(28,28);

Note that if Size is a mutable struct , bad things can happen; 请注意,如果Size是一个可变struct ,可能会发生不好的事情; I would recommend a property rather than a field to prevent a number of confusing side-effects. 我会推荐一个属性而不是一个字段来防止一些令人困惑的副作用。

public static readonly Size ImageSize = new Size(28,28);

You have to do this instead: 你必须这样做:

public readonly Size ImageSize = new Size(28, 28);

Making the instance read only to prevent it to be changed, as you cannot create Size as a constant. 使实例只读以防止它被更改,因为您不能将Size创建为常量。

From the docs: 来自文档:

A constant expression is an expression that can be fully evaluated at compile time. 常量表达式是一个可以在编译时完全计算的表达式。 Therefore, the only possible values for constants of reference types are string and null. 因此,引用类型常量的唯一可能值是string和null。

The code you're writing: 你写的代码:

public const Size ImageSize = new Size() { Width = 28, Height = 28 };

Actually compiles down to the same as this: 实际上编译如下:

public const Size ImageSize = new Size();
ImageSize.Width = 28;
ImageSize.Height = 28;

The version your using, called the object initializer, is just syntactic shorthand for the latter version above. 您使用的版本称为对象初始化程序,它只是上述后一版本的语法简写。 The two are logically identical. 这两者在逻辑上是相同的。 In the latter version you can see why it's giving you the error, you can't set properties on a const after it's been declared. 在后一版本中,您可以看到它为什么会给出错误,在声明后不能在const上设置属性。

More to the point, I'm not sure if you can even use a reference type like that as a const . 更重要的是,我不确定你是否甚至可以使用类似的引用类型作为const I'm not sure about that, as I can't say I ever tried it. 我不确定,因为我不能说我曾经尝试过。 You may try readonly instead. 你可以试试readonly Though you may run into the same or similar issue. 虽然您可能遇到相同或类似的问题。

Does Size have a constructor you can call with the parameters, rather than using the object initializer? Size是否有一个可以使用参数调用的构造函数,而不是使用对象初始值设定项?

You need to use a constructor that takes width and height as parameters. 您需要使用以width和height为参数的构造函数。 Also, the expression must be completely evaluable during compile time. 此外,表达式必须在编译期间完全可评估。 That might not work with your Size type (don't know which one it is). 这可能不适用于您的Size类型(不知道它是哪一个)。 If so (like with System.Drawing.Size ), you might consider using readonly instead of const . 如果是这样(与System.Drawing.Size ),您可以考虑使用readonly而不是const

You can use : 您可以使用 :

public static readonly Size ImageSize = new Size(28, 28);

It's not actually a const, but it won't be changed after the initialization. 它实际上不是const,但在初始化后不会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM