C# Enums: Nullable or 'Unknown' Value?

前端 未结 6 1127
生来不讨喜
生来不讨喜 2020-12-01 07:12

If I have a class with an enum member and I want to be able to represent situations where this member is not defined, which is it better?

a) Declare the

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 07:46

    You just have to decide whether you need a value to represent unknown values, or you do need a way to represent absence of any value.

    In the case of a need to represent unknown values, an extra enum-member sounds good as a solution.

    In case of a need to represent absence of any value, make it nullable.

    Have in mind, that there is nothing wrong with having both "unknown" enum-member and the enum itself being nullable at the same time.

    HTH.

提交回复
热议问题