none
Enumerations RRS feed

  • Question

  • Hi,

    Since enumerations are "hard-coded" in code, isn't it better to use a normal class for values that are for most of the time static but can get occasionally a new value: e.g. CustomerType, ...?

    Thanks.

    Tuesday, January 27, 2015 11:01 AM

Answers

  • It's an interesting thought but there are some things that are favour the use of enums. It describes the intention. If you look at enumColours.Red vs classColours.Red you know the exact intention of the enum, not so the class. Flags; this is an inbuilt mechanism for a enum, harder to do for a class. Direct conversion to integer (esp if you specifically set the integer type in the enum) which can be helpful with external services/apis. Duck Typing, again useful for external services/apis, you can move numbers, not so easy with types. Small storage, typically and int will be better to store/move around that a string, but realistically that's has a more complicated story. But I do take the overall point, I would probably go that route where I wanted something that looks a bit like an enum in terms of holding a set of related constants, perhaps WebSiteEndpoints{AddEndpoint, DeleteEndpoint} where these would be URLs.


    http://pauliom.wordpress.com

    • Marked as answer by Guy Dillen Wednesday, February 4, 2015 3:58 PM
    Wednesday, January 28, 2015 8:21 AM

All replies

  • It's an interesting thought but there are some things that are favour the use of enums. It describes the intention. If you look at enumColours.Red vs classColours.Red you know the exact intention of the enum, not so the class. Flags; this is an inbuilt mechanism for a enum, harder to do for a class. Direct conversion to integer (esp if you specifically set the integer type in the enum) which can be helpful with external services/apis. Duck Typing, again useful for external services/apis, you can move numbers, not so easy with types. Small storage, typically and int will be better to store/move around that a string, but realistically that's has a more complicated story. But I do take the overall point, I would probably go that route where I wanted something that looks a bit like an enum in terms of holding a set of related constants, perhaps WebSiteEndpoints{AddEndpoint, DeleteEndpoint} where these would be URLs.


    http://pauliom.wordpress.com

    • Marked as answer by Guy Dillen Wednesday, February 4, 2015 3:58 PM
    Wednesday, January 28, 2015 8:21 AM
  • Thanks for your insights.
    Wednesday, February 4, 2015 3:58 PM
  • I agree with Guy Dillen. Most of the time enums are the best way to go. They require little memory and are easy to understand. There are only few exceptions which I've ran across.

    If you are building an XML-file with mandatory field values you can code those values using static class with static read-only values. With good naming with enums you can accomplish same results with enum names.

    Using a class introduces an architectural problem if someone implements a method or extension method for that class. Then strict "read-only" type or enumeration likeness has changed.

    Monday, March 2, 2015 12:55 PM