C# 创建由多个类使用的枚举的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/468413/
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-04 04:36:14  来源:igfitidea点击:

C# What is the best way to create an enum that is used by multiple classes?

c#enums

提问by Martijn

I have an enum which is used by multiple classes. What is the best way to implement this?

我有一个被多个类使用的枚举。实现这一点的最佳方法是什么?

采纳答案by lc.

  1. Put the enums right in your namespace (or make a new namespace for them) or
  2. Put them in a (static?) class somewhere if that makes more sense.
  1. 将枚举放在您的命名空间中(或为它们创建一个新的命名空间)或
  2. 如果这更有意义,请将它们放在某个(静态?)类中。

I'd keep them in their own file for easy access and good organization either way.

无论哪种方式,我都会将它们保存在自己的文件中,以便于访问和良好的组织。

I usually wouldn't put them in a class unless they somehow "belong" there. Like you have a Car class with an enum for the types of car, and a Road class and Bridge class that can set limits for types of car. Car.CarTypeseems to be a logical organization for this...

我通常不会把它们放在一个类中,除非它们以某种方式“属于”那里。就像你有一个带有汽车类型枚举的 Car 类,以及一个可以为汽车类型设置限制的 Road 类和 Bridge 类。Car.CarType似乎是一个合乎逻辑的组织...

回答by DarkwingDuck

Put it in its own file and just declare it:

把它放在它自己的文件中并声明它:

public enum Foo { a, b, c, d }

公共枚举 Foo { a, b, c, d }

Consider your namespacing of course.

当然考虑你的命名空间。

回答by Sekhat

Typically I just throw them into the namespace. It's what Microsoft did writing the .NET framework, so it's what I do too, for consistency you understand :)

通常我只是将它们放入命名空间。这是微软在编写 .NET 框架时所做的,所以我也是这样做的,为了保持一致性,你理解:)

回答by atsjoo

Here's an example of having two enums in different namespaces:

这是在不同命名空间中有两个枚举的示例:

using TotallyDifferentNameSpace.Enums;

namespace EnumEx
{
    class Program
    {
        static void Main(string[] args)
        {
            CarType car = CarType.Audi;
            PetrolType pType = PetrolType.Diesel;
        }
    }

    public enum CarType
    {
        Volkswagen,
        Audi,
        Toyota,
        Ford,
        Porsche,
        Lada
    }
}

namespace TotallyDifferentNameSpace.Enums
{
    public enum PetrolType
    {
        Gasoline,
        Diesel
    }
}

回答by Matze

My advice is to not use Enums at all. Use singletons instead.

我的建议是根本不要使用枚举。改用单身人士。

Like

喜欢

    public class A : ISomeInterface
    {
        public static readonly A Instance = new A();
        private A()
        {
        }

    }

    public class B : ISomeInterface
    {
        public static readonly B Instance = new A();
        private B()
        {
        }
    }

In most cases that works better and is better to extend later.

在大多数情况下,效果更好,最好在以后扩展。

Best wishes

最好的祝愿

Matze

马策

回答by Robert MacLean

XML comments help, especially if others will use it

XML 注释很有帮助,尤其是在其他人会使用它的情况下

 /// <summary>
/// Defines the types of cars we support
/// </summary>
public enum CarType
{
    /// <summary>
    /// VW - the peoples car
    /// </summary>
    Volkswagen,

Your enum name should be plural (as instructed by FxCop)

您的枚举名称应该是复数(按照 FxCop 的指示)

public enum CarTypes

Add numeric values and use bitwise numbering even if you don't plan to use bitwise (it's a pain to renumber later).

添加数值并使用按位编号,即使您不打算使用按位编号(稍后重新编号会很痛苦)。

    Volkswagen = 1,

    /// <summary>
    /// They own lambo... can't be all that bad
    /// </summary>
    Audi = 2,

    /// <summary>
    /// Good, cheap, reliable
    /// </summary>
    Toyota = 4,

回答by Sumit Ghosh

You can put an enum is a new codefile with .cs extension, for intellisense to work make sure its part of your project/solution and ofcourse it should be a public enum so that you have a solution scope for it. If intellisense is a problem , make sure you build your solution once, i had this problem once and just a rebuild solved it. Namespacing is a good option if you want to organize your code properly and you are coding a large project. The .NET framework was large. so it has enums under namespaces just for better understanding and code organization.

您可以将枚举放在一个带有 .cs 扩展名的新代码文件中,为了让智能感知工作,请确保它是您的项目/解决方案的一部分,当然它应该是一个公共枚举,以便您有一个解决方案范围。如果智能感知是一个问题,请确保您构建了一次解决方案,我曾经遇到过这个问题,只是重建解决了它。如果您想正确组织代码并且正在编写大型项目,命名空间是一个不错的选择。.NET 框架很大。所以它在命名空间下有枚举只是为了更好地理解和代码组织。