C# 中的枚举应该有自己的文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2282976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:02:50  来源:igfitidea点击:

Should enums in C# have their own file?

c#coding-styleenums

提问by Finglas

I have a class which uses an enumeration, the enum is currently in its own file which seems wasteful.

我有一个使用枚举的类,该枚举目前在它自己的文件中,这似乎很浪费。

What is the general opinion on enums being placed within the namespace of a file that they are consumed in? Or should the enum really live in its own cs file?

关于将枚举放置在使用它们的文件的命名空间中的一般意见是什么?还是应该枚举真的存在于它自己的 cs 文件中?

Edit

编辑

I should mention that while the class in question uses these enumerations, so does external callers. In other words, another class can set these enumerations. So they are not used internally to the class, otherwise this question would be a no brainer.

我应该提到,虽然所讨论的类使用这些枚举,但外部调用者也使用这些枚举。换句话说,另一个类可以设置这些枚举。所以它们不会在课堂内部使用,否则这个问题就很简单了。

采纳答案by James Curran

I wouldn't say "wasteful" (how much does an extra file cost?), but it is often inconventient. Usually there's one class that's most closely associtated with the enum, and I put them in the same file.

我不会说“浪费”(一个额外的文件要花多少钱?),但这通常很不方便。通常有一个类与枚举最相关,我将它们放在同一个文件中。

回答by Fredrik M?rk

This is entirely a matter of style. What I tend to do is to have a file called Enums.csin the solution in which the enum declarations are collected.

这完全是一个风格问题。我倾向于做的是Enums.cs在收集枚举声明的解决方案中调用一个文件。

But they are typically found through the F12key anyway.

F12无论如何,它们通常是通过密钥找到的。

回答by Jeff Sternal

This is really just a matter of preference.

这实际上只是一个偏好问题。

I prefer to put each enumeration in its own file (likewise for each interface, class, and struct, no matter how small). It makes them easier to find when I'm coming from another solution or otherwise don't already have a reference to the type in question.

我更喜欢将每个枚举放在自己的文件中(对于每个接口、类和结构也是如此,无论多么小)。当我来自另一个解决方案或没有对相关类型的引用时,这使它们更容易找到。

Putting a single type in each file also makes it easier to identify changes in source control systems without diffing.

在每个文件中放置一个类型还可以更轻松地识别源控制系统中的更改而无需进行区分。

回答by Patrick Kafka

I like to have one public enums file named E containing each seperate enum, then any enum can be accessed with E... and they are in one place to manage.

我喜欢有一个名为 E 的公共枚举文件,其中包含每个单独的枚举,然后可以使用 E 访问任何枚举......并且它们在一个地方进行管理。

回答by Nikos Steiakakis

Generally I prefer my enums to be in the same file as the Class that it will most probably be an attribute of. If for example I have a class Taskthen the enum TaskStatuswill be in the same file.

通常,我更喜欢我的枚举与最有可能是其属性的类位于同一个文件中。例如,如果我有一个类,Task那么枚举TaskStatus将在同一个文件中。

However, if I have enums of a more generic nature, then I keep them contextually in various files.

但是,如果我有更通用的枚举,那么我会将它们根据上下文保存在各种文件中。

回答by Vishal Mistry

I think that depends on the scope of the enum. For example if the enum is specific to one class, for example used to avoid the magic constant scenario, then I would say put it in the same file as the class:

我认为这取决于枚举的范围。例如,如果枚举特定于一个类,例如用于避免魔术常量场景,那么我会说将它放在与类相同的文件中:

enum SearchType { Forward, Reverse }

If the enum is general and can be used by several classes for different scenarios, then I would be inclined to use put it in its own file. For example the below could be used for several purposes:

如果枚举是通用的并且可以被多个类用于不同的场景,那么我会倾向于将它放在自己的文件中。例如,以下内容可用于多种目的:

enum Result { Success, Error }

回答by Dan Tao

I tend to put enums in their own file for a very simple reason: as with classes and structs, it's nice to know exactlywhere to look if you want to find a type's definition: in the file of the same name. (To be fair, in VS you can always use "Go to Definition," too.)

出于一个非常简单的原因,我倾向于将枚举放在自己的文件中:与类和结构一样,如果您想找到类型的定义,很高兴确切知道在哪里查找:在同名文件中。(公平地说,在 VS 中,您也可以随时使用“转到定义”。)

Obviously, it can get out of hand. A colleague where I work even makes separate files for delegates.

显然,它可能会失控。我工作的一位同事甚至为代表制作了单独的文件。

回答by Adeel

I place mostly inside in namespace and outside of class so that it is easily accessible other classes in that namespace like below.

我主要放在命名空间内部和类外部,以便可以轻松访问该命名空间中的其他类,如下所示。

namespace UserManagement
{
    public enum UserStatus { Active, InActive }
    class User
    {
        ...
    }
}

回答by Bryan Watts

The question to ask yourself would be: is there anything about an enumeration type in C# that indicates I should treat it differently from all other types I create?

要问自己的问题是:C# 中的枚举类型是否有任何内容表明我应该将它与我创建的所有其他类型区别对待?

If the enumeration is public, it should be treated like any other public type. If it is private, declare it as a nested member of the class using it. There is no compelling reason to put two public types in the same file simply because one is an enumeration. The fact that it is a public type is all that matters; the flavor of type does not.

如果枚举是公开的,则应像对待任何其他公开类型一样对待它。如果它是私有的,则将其声明为使用它的类的嵌套成员。没有令人信服的理由将两个公共类型放在同一个文件中,因为一个是枚举。它是一种公共类型这一事实才是最重要的;类型的味道没有。

回答by Doug Ferguson

One advantage of using a separate file for enums is that you can delete the original class that used the enum and write a new class using the enum.

为枚举使用单独文件的一个优点是您可以删除使用枚举的原始类并使用枚举编写新类。

If the enum is independent of the original class then putting it in a separate file makes future changes easier.

如果枚举独立于原始类,则将其放在单独的文件中会使以后的更改更容易。