c# - Is it a good practice to add a "Null" or "None" member to the enum? -
this question has answer here:
- c# enums: nullable or 'unknown' value? 6 answers
when creating new enum in c#, practice have null member?
if yes, give value of 0 default? call null member null or null? strictly believe in null or don't have problem calling other null. instance none. can elaborate on answer?
there's design rule in visual studio, ca1008, provides insight question. description of rule (styling mine):
the default value of uninitialized enumeration, other value types, zero. a non-flags−attributed enumeration should define member has value of zero default value valid value of enumeration. if appropriate, name member 'none'. otherwise, assign 0 used member. note that, default, if value of first enumeration member not set in declaration, value zero.
if enumeration has
flagsattribute
applied defines zero-valued member, name should 'none' indicate no values have been set in enumeration. using zero-valued member other purpose contrary use offlagsattribute
in , and or bitwise operators useless member. implies 1 member should assigned value zero. note if multiple members have value 0 occur in flags-attributed enumeration,enum.tostring()
returns incorrect results members not zero.
there enum design article makes following points:
- √ do provide value of 0 on simple enums. consider calling value "none." if such value not appropriate particular enum, common default value enum should assigned underlying value of zero.
- x avoid using flag enum values of 0 unless value represents "all flags cleared" , named appropriately, prescribed next guideline.
- √ do name 0 value of flag enums
none
. flag enum, value must mean "all flags cleared."
based on above, yes, practice, when have [flags]
enum
.
Comments
Post a Comment