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

前端 未结 7 940
深忆病人
深忆病人 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:19

    You can use the TryParse method that returns true if it successful:

    Age age;
    
    if(Enum.TryParse("myString", out age))
    {
       //Here you can use age
    }
    

提交回复
热议问题