Constants 类中的静态属性是否应该实现支持字段?

发布于 2024-09-12 02:12:41 字数 498 浏览 4 评论 0原文

我有一个 Constants 类,其中存储了许多静态只读变量。

我应该这样做吗:

private static readonly int _maxThings = 100;
...
public static int MaxThings { get { return _maxThings; } }

这对我来说似乎有点多余。我有什么理由不执行以下操作吗?

public static int MaxThings { get { return 100; } }

编辑

好的,所以这是一个脑残问题。我认为重点是,如果我要在初始化时设置这个值,那么使用静态支持字段并公开一个公共的仅获取属性(该属性本身不需要是静态的)是有意义的。

但是,如果我愿意将公共静态属性设置为硬值,那么这与将其烘焙到程序集中没有功能差异。除非我在这里遗漏了一些其他概念,否则在这种情况下我只会使用 const。

感谢您的回答。

I have a class Constants in which I store a number of static readonly variables.

Should I do this:

private static readonly int _maxThings = 100;
...
public static int MaxThings { get { return _maxThings; } }

That seems kind of redundant to me. Is there any reason why I wouldn't just do the following?

public static int MaxThings { get { return 100; } }

Edit

Okay, so this was a brain fart of a question. I think the point is that if I'm going to be setting this value at initialization then it makes sense to use a static backing field and expose a public get-only property that wouldn't need to be static itself.

If, however, I'm comfortable setting a public static property to a hard value, then there's no functional difference between that and just baking it into the assembly. Unless there's some other concept I'm missing here, in this case I'd just use a const.

Thanks for the answers.

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

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

发布评论

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

评论(3

回忆那么伤 2024-09-19 02:12:41

你应该这样做

public const int MaxThings = 100;

在这种情况下我没有理由看到使用属性。

更新->

回应评论.. 如果您正在开发一个库并导出常量,那么了解常量如何在 .net 中使用非常重要。当针对您的库进行编译时,常量值将内联并包含在消费应用程序中,这样,如果您的库已更新而消费应用程序未更新,则旧的常量值仍将存在。当然,这是应该使用静态属性的时候。

如果您不开发库,那么使用 const 就可以了。

you should do

public const int MaxThings = 100;

there is no reason that i can see to use properties in this case.

Update ->

In response to comments.. If you are developing a library and exporting constants then it's important to understand how constants are consumed int .net. When compiled against your library, the constant values will be inlined and included in the consuming application, such that if your library is updated and consuming application is not then the old constant values will still be present. This is, of course, when static properties should be used.

If you are not developing a library, then the use of const's are fine.

不必了 2024-09-19 02:12:41

因为常数是常数,所以值永远不会改变。

对属性的偏好是字段和属性的签名不同。因此,从字段更改为属性需要重新编译所有调用者。因此,如果您稍后需要添加 getter/setter 逻辑,则在第一个实例中创建属性将避免该问题。

因为你有一个永远不会改变的常量,所以根本没有理由将它实现为属性。尽管您定义了一个只读静态,但由于它只能在类内部更改,因此在外部它与常量之间没有区别。

Because constants are well... constant, the value will never change.

The preference for properties are that the signature of a field and a property are different. So changing from a field to a property requires that all callers are recompiled. Thus creating a property in the first instance will avoid that issue if you need to add getter/setter logic at a later date.

Because you have a constant, which will never change, there is no reason at all to implement it as a property. Although you defined a readonly static, as this can only be changed from within the class, externally there is no difference between that and a constant.

許願樹丅啲祈禱 2024-09-19 02:12:41

从代码可读性的角度来看,我发现第二个是一个改进。不确定编译器是否足够聪明,可以在这种情况下进行优化,但影响如此之小,我只会选择可读性。

From a code readability standpoint I find the second one to be an improvement. Not sure if the compiler is smart enough to optimize in that situation, but the hit is so negligible I would just go with readability.

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