C# Enums: Nullable or 'Unknown' Value?

前端 未结 6 1122
生来不讨喜
生来不讨喜 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:51

    Definitely use a nullable value type - that's what they're for. It explicitly states your intention. It also means you can use Enum.IsDefined (or the equivalent from Unconstrained Melody if you want generic type safety) to easily determine whether a particular value is a real value without worrying about the "fake" one too.

提交回复
热议问题