简体   繁体   中英

C# Enum or int constants

I have a group of values that represent a state (ON, OFF, READY, ...). These values also get stored in a DB as an int field, so I am wondering if best practices would say to make this an enum or just bunch of const int types on a class.

Enum seems like a natural fit for human reading/coding, but it seems like it hides the fact that it matters which integers the values map to (or else values retrieved from a DB will be instantiated to the incorrect state). Someone may come in later and add a new value to the enum or something and throw the whole thing off.

Which is the better approach?

I think enum is still the best choice for readability. However, since its values get stored in the DB, you should specify the values explicitly:

enum State { On = 1, Off = 2, Ready = 3};

How is somebody adding a new enum value any different than a new constant int?

Remember, you can set an enum to a specific integer value.

Embrace readability!

As an added bonus, you can now use strong typing to prevent shenanigans like

Widget.state = 474;

Where 474 does not correspond to a state in your database.

I would go with an enum and make sure it's very well documented which table the enum corresponds to.

The danger with a bunch of const ints is that the type conveys no indication of what the valid values are and someone could very easily assign any old value. A malicious user of your code could still do that with a cast but you can't stop some people from shooting themselves in their own foot...

If your values will rarely ever change, or if you have no problem with editing and recompiling your code, then sure use an enum with explicit declaration of the integer values for each. Otherwise, I would create an object wrapper that pulls the values from the database (you could always cache the values for a bit if you are worried about performance). Then do all your comparisons with that object wrapper. Unless you can insure the values in the database won't change without the code being updated using an enum in your code is just volatile and risky. If you are really adventurous and want to get into emitting code you can do some pretty cool things with that.

您也可以自己确定Enum中的值,即:

enum STATE {ON=2, OFF=9, READY=14};

I would say Enums. They give you the option of a machine format to a human readable format. Make sure you document it very well it this way

///Summary
/// About State Enum
///Summary
enum State : int
{ 
    ///Summary
    /// About Off Enum Value 
    ///Summary
    Off = 0, 
    ///Summary
    /// About On Enum Value
    ///Summary
    On,
    ///Summary
    /// About Ready Enum Value
    ///Summary
    Ready
};

No need to assign value to each and every member. Start from 0 and rest will be incremented automatically.

Since your enum is about on / off, you can use use it in boolean way. 0 stands for False or off and 1 to true or on.

You can also convert your enum to int like

int value = (int)State.On; // value will have 1 

Save value in database as int. and while retrieving you can do like this

State st = (State)int.Parse(mydatabasevalue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM