Enum.Parse(), surely a neater way?

前端 未结 5 1956
猫巷女王i
猫巷女王i 2020-12-14 05:32

Say I have an enum,

public enum Colours
{
    Red,
    Blue
}

The only way I can see of parsing them is doing something like:



        
5条回答
  •  自闭症患者
    2020-12-14 06:18

    Use Enum.IsDefined() first, to save yourself from wrapping in a try/catch. It will return a boolean value of whether or not the input is a valid member of that enum.

提交回复
热议问题