Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best practice to code shared enums between classes

Tags:

c#

enums

namespace Foo
{
    public enum MyEnum
    {
        High, Low
    }

    public class Class1
    {
        public MyEnum MyProperty { get; set; }
    }
}

MyEnum is declared outside Class1 cause I need it here and in other classes

Seems good, but what if I decide later to delete the file containingClass1?

MyEnum declaration will be lost!!

What's the best practice to code shared enums between classes?

like image 799
Stacked Avatar asked Jan 06 '13 21:01

Stacked


2 Answers

The best practice is creating separate file for each class, enum, or other type.

MyEnum.cs

namespace Foo
{
    public enum MyEnum
    {
        High, 
        Low
    }
}

Class1.cs

namespace Foo
{
    public class Class1
    {
        public MyEnum MyProperty { get; set; }
    }
}
like image 65
Sergey Berezovskiy Avatar answered Oct 05 '22 23:10

Sergey Berezovskiy


What's the best practice to code shared enums between classes?

Have your enumerations each in a file of its own, with the same name as the enumeration.

// Foo\MyEnum.cs
namespace Foo
{
    public enum MyEnum
    {
        High, Low
    }
}
like image 26
Oded Avatar answered Oct 06 '22 01:10

Oded