get value from c# enums

后端 未结 6 2028
无人共我
无人共我 2021-01-18 05:39

I have an enum

public enum ProductionStatus {
    Received = 000,
    Validated = 010,
    PlannedAndConverted = 020,
    InProduction = 030,
    QAChecked          


        
6条回答
  •  失恋的感觉
    2021-01-18 06:02

    var code = (int)ProductionStatus.Validated;
    

    You can also convert an int to an enum value, like this:

    var status = (ProductionStatus)10;
    

    bool eq = 010 == 10; they are actually equal

    If you would like to use strings , use this method.

        static string EnumToString(ProductionStatus val)
        {
            switch (val)
            {
                case ProductionStatus.Received:
                    return "000";
                case ProductionStatus.Validated:
                    return "010";
                case ProductionStatus.PlannedAndConverted:
                    return "020"; 
                default:
                    return "Unknown value";
            }
        }
    

提交回复
热议问题