C# 项目中的事件常量应该放在哪里

发布于 2024-10-23 17:07:43 字数 321 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(6

清风不识月 2024-10-30 17:07:43

惯例是不这样做。您所描述的内容通常会实现为 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.

宛菡 2024-10-30 17:07:43

由于您实际上是在添加 EASY、MEDIUM、HARD 等级别,这些级别彼此处于顺序级别,因此我希望使用 enum 。就像在其他语言中一样,您可以使用公共枚举难度{EASY,MEDIUM,HARD}

但是你把这样的枚举放在哪里呢?如果您希望它在许多不同的 eventArgs 中使用,我建议使用一些抽象基类:

public class LevelEventArgs : EventArgs
{
    public enum Difficulty
    { 
        EASY, 
        MEDIUM, 
        HARD 
    }
}

然后,让您的所有 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 an public 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:

public class LevelEventArgs : EventArgs
{
    public enum Difficulty
    { 
        EASY, 
        MEDIUM, 
        HARD 
    }
}

And then, let all your EventArgs inherit from this class.

甜味超标? 2024-10-30 17:07:43

我会使用枚举来代替:

public enum DifficultyLevel {
  Easy = 0,
  Medium = 1,
  Hard = 2
}

这样所有的值都定义在一个结构中,该结构被明确定义为连接到类型,而不是作为可以连接到任何东西的松散常量。

将您的私有字段声明为:

private DifficultyLevel _difficulty;

分配如下值:

_difficulty = DifficultyLevel.Easy;

通过指定枚举的数值,如果需要,您还可以将它们与已知整数值相互转换:

_difficulty = (DifficultyLevel)1;

int level = (int)_difficulty;

I would use an enumeration instead:

public enum DifficultyLevel {
  Easy = 0,
  Medium = 1,
  Hard = 2
}

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:

private DifficultyLevel _difficulty;

Assign a value like this:

_difficulty = DifficultyLevel.Easy;

By specifying the numeric values for the enumeration, you can also convert them to and from known integer values if you need to:

_difficulty = (DifficultyLevel)1;

int level = (int)_difficulty;
话少情深 2024-10-30 17:07:43

常量应在其使用中最常关联的对象附近或对象中声明。如果您对常量的主要用途是创建自定义 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.

俯瞰星空 2024-10-30 17:07:43

看起来更像你想要一个枚举

    public enum Difficulty
{
   Easy,
   Medium,
   Hard
}

Looks more like you want an enumeration

    public enum Difficulty
{
   Easy,
   Medium,
   Hard
}
壹場煙雨 2024-10-30 17:07:43

这完全取决于您的代码的设置方式。在某些情况下,它可能位于项目或解决方案中的公共命名空间中。

我有几个项目位于公共命名空间中,即 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

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