Alex Rivera | Logout

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

Asked 2009-01-22T08:26:29.313
17

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

Edit
Report

2 Answers

8

Put it in its own file and just declare it:

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

Consider your namespacing of course.

answered 2009-01-22T08:29:40.430
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
    }
}
answered 2009-01-22T09:17:19.023

Your Answer