How to assign string values to enums and use that value in a switch

后端 未结 10 1652
小鲜肉
小鲜肉 2021-02-02 10:45

Basically a series of titles will be passed into the switch statement and I need to compare them against the string values of the enum. But I have little to no idea how to do th

10条回答
  •  失恋的感觉
    2021-02-02 11:17

    Enum can only have integral underlying types (except char). Therefore you cannot do what you want, at least directly.

    However you can translate the string you have to the enum type:

    EnumType eVal = (EnumType)Enum.Parse(typeof(EnumType), strValue);
    
    switch(eVal)
    {
        case EnumType.doctor:/*...*/; break;
        case EnumType.mr: /*...*/; break;
    }
    

提交回复
热议问题