C# 项目中的事件常量应该放在哪里
我对 C# 非常陌生,我有一个约定问题:
与事件关联的常量应该存储在哪里?
它们是否应该包含在我定义 EventArgs 的同一位置?
作为解释,我想为名为“_difficulty”的私有字段定义不同的常量,并通过我重写的 EventArgs 类的构造函数进行设置。
假设常数是, 公共 const int 简单 = 0,中等 = 1,困难 = 2; (我假设命名约定全部大写)
或者,我可以创建一个像“DifficultyConstants”这样的类,然后将它们插入那里。
我只是好奇这个约定是什么以及对于遵循 OOP 最有意义。
I'm extremely new to C# and I had a question of convention:
Where should constants that are associated with an event be stored?
Should they be included in the same place that I define my EventArgs?
As an explain, I want to define different constants for a private field called "_difficulty", and is set through my overridden EventArgs class's constructor.
Let's say the constants were,
public const int EASY = 0, MEDIUM = 1, HARD = 2; (I'm assuming the naming convention is all caps)
Alternatively, I could make a class like "DifficultyConstants", then insert them there.
I was just curious as to what the convention was and would make the most sense for following OOP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
惯例是不这样做。您所描述的内容通常会实现为
enum
而不是一组命名的整数常量。The convention is to not do this. What you're describing would be conventionally implemented as an
enum
rather than a set of named integer constants.由于您实际上是在添加 EASY、MEDIUM、HARD 等级别,这些级别彼此处于顺序级别,因此我希望使用
enum
。就像在其他语言中一样,您可以使用公共枚举难度{EASY,MEDIUM,HARD}
。但是你把这样的枚举放在哪里呢?如果您希望它在许多不同的 eventArgs 中使用,我建议使用一些抽象基类:
然后,让您的所有 EventArgs 继承自此类。
As you are really adding levels like EASY, MEDIUM, HARD, which are at ordinal level from eachother, I would expect an
enum
to be used. Just as in other languages, you could use anpublic enum Difficulty {EASY, MEDIUM, HARD}
.But where do you leave such an enum? If you want it to be used in a lot of different eventArgs, I would recommend using some abstract base class:
And then, let all your EventArgs inherit from this class.
我会使用枚举来代替:
这样所有的值都定义在一个结构中,该结构被明确定义为连接到类型,而不是作为可以连接到任何东西的松散常量。
将您的私有字段声明为:
分配如下值:
通过指定枚举的数值,如果需要,您还可以将它们与已知整数值相互转换:
I would use an enumeration instead:
That way all the values are defined in a structure that is well defined as being connected to the type, not as loose constants that could be connected to anything.
Declare your private field as:
Assign a value like this:
By specifying the numeric values for the enumeration, you can also convert them to and from known integer values if you need to:
常量应在其使用中最常关联的对象附近或对象中声明。如果您对常量的主要用途是创建自定义 EventArgs,那么这听起来是定义它们的好地方。如果到处使用它们,通常建议使用 Common 或 CommonUtil 库。
一个提示;考虑为这些值创建一个枚举,而不是将它们作为单独的常量。它们是一组相关的、互斥的值,用于指示状态;枚举值的教科书定义。
Constants should be declared near or in the object they are most commonly associated with in their use. If your primary use of the constants is in creating your custom EventArgs, that sounds like a great place to define them. If they're used everywhere, a Common or CommonUtil library is usually recommended.
One tip; consider creating an enum for these values instead of them being individual constants. They are a related, mutually-exclusive set of values that indicate status; the textbook definition of enum values.
看起来更像你想要一个枚举
Looks more like you want an enumeration
这完全取决于您的代码的设置方式。在某些情况下,它可能位于项目或解决方案中的公共命名空间中。
我有几个项目位于公共命名空间中,即 Company.Common
It all depends on how your code is set up. In some cases it could be in a common namespace within your project or solution.
I have several projects where it's in a common namespace, i.e. Company.Common