添加“Null”是一个很好的做法。或“无”会员到枚举? [英] Is it a good practice to add a "Null" or "None" member to the enum?

查看:115
本文介绍了添加“Null”是一个很好的做法。或“无”会员到枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果是,您是否默认给它值为0?你会调用null成员Null还是NULL?你是否严格相信Null或者你没有任何问题调用除了null之外的东西。例如无。您可以详细说明您的答案吗?

解决方案

Visual Studio中有一个设计规则, CA1008 ,它提供了一些洞察你的问题。规则的描述是(样式我的):


与其他值类型一样,未初始化枚举的默认值为零。 非标记属性枚举应定义值为零的成员,以使默认值为枚举的有效值。 如果合适,请将成员命名为无。否则,将零分配给最常用的成员。 请注意,默认情况下,如果声明中未设置第一个枚举成员的值,则其值为零。



如果具有 FlagsAttribute 的枚举定义了零值成员,则其名称应为无,表示没有在枚举。 使用零值成员进行任何其他目的与使用 FlagsAttribute 相反,AND和OR按位运算符对成员是无用的。 strong>这意味着只有一个成员应该被赋值为零。请注意,如果具有值为零的多个成员发生在标志属性枚举中,则 Enum.ToString()为不为零的成员返回不正确的结果。


还有一个枚举设计文章,提出以下几点:



  • DO 在简单枚举中提供零值。考虑调用像None这样的值。如果这样的值不适用于这个特定的枚举,那么枚举的最常见默认值应该被分配为零的底层值。

  • X AVOID 使用标志枚举值为零,除非值代表所有标志被清除,并按照下一条指导规定适当地命名。

  • DO 命名为零标志枚举值。对于一个标志枚举,值必须始终表示所有标志被清除。







基于上述,我会说,这是很好的做法,特别是当你有一个 [Flags] 枚举


When creating a new enum in C#, is it a good practice to have null member?

If yes, do you give it the value of 0 by default? Would you call the null member Null or NULL? Do you strictly believe in Null or you don't have any problem with calling it something other than null. For instance None. Can you elaborate on your answer?

解决方案

There's a design rule in Visual Studio, CA1008, that provides some insight into your question. The description of the rule is (styling mine):

The default value of an uninitialized enumeration, just like other value types, is zero. A non-flags−attributed enumeration should define a member that has the value of zero so that the default value is a valid value of the enumeration. If appropriate, name the member 'None'. Otherwise, assign zero to the most frequently used member. Note that, by default, if the value of the first enumeration member is not set in the declaration, its value is zero.

If an enumeration that has the FlagsAttribute applied defines a zero-valued member, its name should be 'None' to indicate that no values have been set in the enumeration. Using a zero-valued member for any other purpose is contrary to the use of the FlagsAttribute in that the AND and OR bitwise operators are useless with the member. This implies that only one member should be assigned the value zero. Note that if multiple members that have the value zero occur in a flags-attributed enumeration, Enum.ToString() returns incorrect results for members that are not zero.

There is also an Enum Design article that makes the following points:

  • DO provide a value of zero on simple enums. Consider calling the value something like "None." If such a value is not appropriate for this particular enum, the most common default value for the enum should be assigned the underlying value of zero.
  • X AVOID using flag enum values of zero unless the value represents "all flags are cleared" and is named appropriately, as prescribed by the next guideline.
  • DO name the zero value of flag enums None. For a flag enum, the value must always mean "all flags are cleared."


Based on the above, I would say that yes, it is good practice, especially when you have a [Flags] enum.

这篇关于添加“Null”是一个很好的做法。或“无”会员到枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆