在项目中声明常量
我已经看到了项目中使用的这两种常量声明方法。
公共模块中的常量。
NonInheritable(Sealed) 类中的常量
有人使用任何其他方法来声明常量吗?
这些方法之间有什么区别,有什么优点和缺点吗?
谢谢。
I have seen these two approaches for constant declaration which are to be used in the project.
Constants in a public module.
Constants in a NonInheritable(Sealed) class
Does anybody uses any other approach for the constant declartion ?
Is there any difference between these approaches, any pros and cons ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将常量放在最相关的类中 - 例如
Math.Pi
、Int32.MinValue
等。我见过各种实际上也应该是枚举的常量 - 特别是 我认为仅为常量
创建一个类(或模块)是不明智的,除非确实没有与这些常量关联的合理类。
Put the constant in the class where it's most relevant - e.g.
Math.Pi
,Int32.MinValue
etc.I've seen various constants which should actually be enums, too - particularly for status codes etc.
I don't think it's wise to create a class (or module) just for constants unless there really is no sensible class that those constants are associated with.
我同意@Jon 的观点。 然而,对于应用程序特有的常量——即它们是配置默认值、调整参数等,我有时会创建一个强类型的静态配置类来保存它们。 我通过仅定义公共 getter 的属性公开这些值。 这样,它们可以从配置文件加载,但在整个代码中作为固定值引用。
I agree with @Jon. However, for constants that are unique to your application -- i.e., they are configuration defaults, tuning parameters, etc., I sometimes create a strongly-typed static configuration class to hold them. I expose the values via properties with only public getters defined. This way they can be loaded from a configuration file, but referenced throughout your code as fixed values.
我的做法与乔恩所说的类似。 绝大多数枚举和常量显然与特定的类或模块相关,并在其中声明为公共。
我的软件的最低级别称为实用程序,包含数学例程和其他通用实用程序的函数和方法(而不是该应用程序模式的特定实用程序)。 其中有一个模块,它具有一堆函数,这些函数只是没有共同主题的实用函数/子例程。 任何枚举或常量都位于这一区域。
应用程序模型有一个类似的区域,其中包含该应用程序通用的实用程序。 这就是不能与模型的任何一类相关联的常量所在的位置。
因此,在我的个人框架中可以找到常量的三个区域:
类声明
应用模型的通用实用模块
实用组件的通用实用模块。
实际上,只需在任一实用程序模块中声明数十个枚举和常量中的一小部分。 其中大部分是 Win32 函数使用的 Win32 减速,导入到我的程序集中。
My practice is similar to what Jon states. The vast majority of the enums and constant are obviously tied to a particular class or module and are declared public in there.
The lowest level of my software is called Utility and contains math routines and other functions and method of general utility (rather than specific utility to that application's mode). In there is a module that has as a bunch of function that just are utility functions/subroutines that have no common theme. It is in that one area that any enums or constant go.
There is a similar area for the application model where utilities that are of general use for that application go. And that where constants that can't be tied to any one class of the MODEL go.
So there three areas where constant can be found in my personal framework
The class declarations
The general utility module of the application model
The general utility module of the utility assembly.
In practice only a handful out of dozens of enum and constant ever needed to be declared in either utility modules. Most of those are Win32 decelerations used by Win32 function that are imported into my assembly.