私有常量与公共只读
我得到了三个类文件,其中 in 常量被声明为
private const string PAGE_SIZE = "PageSize";
是否可以将其移动到新文件以保存声明为的所有常见常量
public readonly string PageSize = "PageSize";
这有什么优点和缺点?
I got three class files where in constant is declared as
private const string PAGE_SIZE = "PageSize";
Is it good to move this to new file to hold all common constants declared as
public readonly string PageSize = "PageSize";
What are the pros and cons for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
const
和readonly
字段之间存在一些重要区别:const
在编译时进行评估。如果您在从应用程序引用的单独程序集中声明const
,则对const
的更改仅会影响应用程序(如果使用更新的程序集重新编译)。引用 .NET 开发类库的设计指南:<块引用>
请对永远不会改变的常量使用常量字段。
例如,Math 类将 E 和 PI 定义为静态常量。
编译器将 const 字段的值直接插入到调用代码中,这意味着永远无法更改 const 值,否则会带来兼容性问题的风险。
只读
字段可以在运行时初始化,使您能够执行运行时计算来计算值和使用。const
只能由可在编译时完全求值的常量表达式来声明。唯一可以是const
的引用类型是String
。关于您的具体问题,这实际上取决于如何使用这些常量。显然,同一个常量不应该有多个定义。否则,如果常量在使用它的地方“附近”声明,例如在类中甚至在使用它的方法中,可能会更容易理解。
There are some important differences between a
const
and areadonly
field:The
const
is evaluated at compile time. If you declare theconst
in a separate assembly that you reference from you application a change to theconst
will only affect the application if it is recompiled using the updated assembly. To quote from the .NET Design Guidelines for Developing Class Libraries:A
readonly
field can be initialized at run-time enabling you to perform run-time calculations to compute the value and use. Aconst
can only be declared by a constant expression that can be fully evaluated at compile time. The only reference type that can beconst
isString
.About your specific question it really depends on how these constants are used. Obviously you shouldn't have multiple definitions of the same constant. Otherwise it is probably easier to understand if the constant is declared "near" where it is used, e.g. in the class or even the method where it is used.
撇开性能考虑 -
赞成:您的常量都集中在一个地方。
反对:您的常量不再接近它们的使用点。
对于在类之间共享的常量,将它们分解为一个公共的单个类是有意义的,这样它们只被指定一次。然而,这意味着“不适当的耦合”,因此可能使用该常量的所有逻辑都需要位于同一个类中。
Performance considerations aside -
In favour: your constants are all centralised in one place.
Against: your constants are no longer close to the point at which they're used.
For constants that are shared between classes it makes sense to break them out to a common single class so they're only specified once. However this implies "inappropriate coupling", so it might be that all the logic that uses this constant needs to be in the same class.
两者可能具有相似的实际效果,但它们对于表达您的意图很有用。
因此,const 值可供类的所有实例使用并且永远不会改变。只读表示您的数据值对于类的每个实例可能不同,但一旦创建类,该数据值将是不可变的。当您在不同的使用者之间共享类的实例时,不变性可能是一个非常有用的保证。顺便说一句,在 CLR Via C# 中,Richter 更喜欢只读公共成员而不是仅具有公共设置器的属性,我必须将其挖掘出来并提醒自己原因。
The two may have similar practical effects, but they are useful for signalling your intentions.
So, a const value is something that is available to all instances of your class and will never change. A readonly signals that you have a data value that could be different for each instance of you class, but will be immutable once the class is created. Immutability can be a really useful guarantee when you are sharing the instance of the class between different consumers. In passing, in CLR Via C#, Richter prefers readonly public members to properties with only public setters, I'll have to dig it out and remind myself why.
可能是您优化得太早了。
这可能意味着通过共享和公开常量而失去灵活性——在不知道现有类是什么的情况下很难说。
如果将来 PageSize 实际上需要在它们之间变化怎么办?它实际上是否需要在所有三个类之间同步,或者这只是目前看来是个好主意的一个小调整?
It might be that you're optimizing too early.
This might mean losing flexibility by making the constants shared and public -- hard to say without knowing what the existing classes are.
What if PageSize actually needs to vary between them in the future? Does it actually need to be synchronised across all three classes, or is this just a small tweak that seems like a good idea at the moment?