how to check if string value is in the Enum list?

前端 未结 7 878
深忆病人
深忆病人 2021-01-30 05:15

In my query string, I have an age variable ?age=New_Born.

Is there a way I can check if this string value New_Born is in my Enum list

7条回答
  •  借酒劲吻你
    2021-01-30 05:27

    You can use the Enum.TryParse method:

    Age age;
    if (Enum.TryParse("New_Born", out age))
    {
        // You now have the value in age 
    }
    

提交回复
热议问题