简体   繁体   English

C#Enum或int常量

[英]C# Enum or int constants

I have a group of values that represent a state (ON, OFF, READY, ...). 我有一组表示状态的值(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. 这些值也作为int字段存储在DB中,所以我想知道最佳实践是否会说这是一个枚举或只是一类const int类型。

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). Enum似乎很适合人类阅读/编码,但它似乎隐藏了这样一个事实,即值映射到哪个整数(或者从DB检索的值将被实例化为不正确的状态)。 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. 我认为enum仍然是可读性的最佳选择。 However, since its values get stored in the DB, you should specify the values explicitly: 但是,由于其值存储在DB中,因此应明确指定值:

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

How is somebody adding a new enum value any different than a new constant int? 有人添加新的枚举值与新的常量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. 474与数据库中的状态不对应。

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. 一堆const int的危险在于,类型没有表明有效值是什么,有人可以很容易地分配任何旧值。 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. 我会说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. 从0开始,休息将自动递增。

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. 0代表False或off,1代表true或on。

You can also convert your enum to int like 您也可以将您的枚举转换为int

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

Save value in database as int. 将数据库中的值保存为int。 and while retrieving you can do like this 检索时你可以这样做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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