Why we can't have “char” enum types

前端 未结 9 1016
不知归路
不知归路 2020-12-06 03:50

I want to know why we can\'t have \"char\" as underlying enum type. As we have byte,sbyte,int,uint,long,ulong,short,ushort as underlying enum type. Second what is the defau

9条回答
  •  忘掉有多难
    2020-12-06 04:54

    char charPC = 'P';
    if (Enum.IsDefined(typeof(PayCode), (PayCode)charPC)) { 
            // check if charPC is a valid value
            PayCode enumPC = (PayCode)charPC; // enumPC == PayCode.Paid
    }
    
    • Made a small change, no need to convert it to an (int) in the if for all cases, depends on the enum type. But if you cast it directly to the enum type itself, it should always work.

提交回复
热议问题