简体   繁体   English

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

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

I have an enum which is used by multiple classes. 我有一个供多个类使用的枚举。 What is the best way to implement this? 实现此目的的最佳方法是什么?

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 :) 这是Microsoft编写.NET框架的方法,所以我也这样做,以确保一致性:)

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. 当然,请考虑您的命名空间。

  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. 如果更合理,则将它们放在某个(静态?)类中。

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类,以及一个可以设置汽车类型限制的Road类和Bridge类。 Car.CarType seems to be a logical organization for this... Car.CarType似乎是对此的合乎逻辑的组织。

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,

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
    }
}

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. 您可以将枚举放入扩展名为.cs的新代码文件中,以使intellisense正常工作,请确保它是您的项目/解决方案的一部分,当然,它应该是公共枚举,以便为您提供解决方案的范围。 If intellisense is a problem , make sure you build your solution once, i had this problem once and just a rebuild solved it. 如果intellisense是一个问题,请确保您一次构建您的解决方案,我曾经遇到过这个问题,只是通过重新构建解决了它。 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. .NET框架很大。 so it has enums under namespaces just for better understanding and code organization. 因此它在命名空间下具有枚举,只是为了更好地理解和组织代码。

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 马策

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM