Validate Enum Values

前端 未结 11 993
你的背包
你的背包 2020-11-30 09:43

I need to validate an integer to know if is a valid enum value.

What is the best way to do this in C#?

11条回答
  •  眼角桃花
    2020-11-30 10:01

    Brad Abrams specifically warns against Enum.IsDefined in his post The Danger of Oversimplification.

    The best way to get rid of this requirement (that is, the need to validate enums) is to remove ways where users can get it wrong, e.g., an input box of some sort. Use enums with drop downs, for example, to enforce only valid enums.

提交回复
热议问题