Tutorial :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?


  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.CarType seems to be a logical organization for 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 :)


Put it in its own file and just declare it:

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

Consider your namespacing of course.


XML comments help, especially if others will use it

 /// <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)

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


I know my suggestion goes against the .NET Naming conventions, but I personally prefix enums with 'E' and enum flags with 'F' (similar to how we prefix Interfaces with 'I'). I really do not understand why this is not the convention. Enums/Flags are a special case like Interfaces that will never change their type. Not only does it make it clear what it is, it's very easy to type in intellisense since the prefix will filter most other types/variables/etc, and you won't have these naming clashes.

And that would also solve another problem where for examples in WPF they use static classes like enums (e.g. FontWeights) that have pre-defined instances of types but you would not know if you don't search for it. If they just prefixed them with 'E', all you would have to do is type on character to find these special static classes.


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


    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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »