Search code examples
c#performanceenumsstatic-classes

What's the difference between enums & using static classes with constants?


What are the performance implications between these two items? I've seen the static class in the wild recently and I'm not sure what to make of it.

public enum SomeEnum
{
   One = 1,
   Two,
   Three
}

public static class SomeClass
{
   public static readonly int One = 1;
   public static readonly int Two = 2;
   public static readonly int Three = 3;
}

Solution

  • The difference is type safety. Suppose you have two of these enums. How are you going to tell the difference:

    void SomeMethod(int x, int y)
    
    // Compiles, but won't do what you want.
    SomeMethod(SomeOtherClass.Xyz, SomeClass.One);
    

    vs

    void SomeMethod(SomeEnum x, SomeOtherEnum y)
    
    // Compile-time error
    SomeMethod(SomeOtherEnum.Xyz, SomeEnum.One)
    

    So everywhere you have an expression which wants to be one of a particular set of values, you can make it clear to both the reader and the compiler which set of values you're interested in if you use enums. With just ints... not so much.