Can you use Enum for Double variables?

后端 未结 6 484
情话喂你
情话喂你 2021-01-02 07:00

I have created a class for handling Unit Conversion in C#. It is not working as it should only returning strings.

Here is the class:

using System;
us         


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-02 08:05

    Sorry, can't be done. Enums are strictly integers/bytes. Really, they are supposed to be their own type.

    You can try:

    enum Units() {
        Grams ,
        KiloGrams ,
        Milligram 
    }
    
    public function GetUnitsFloatValue(Units unit) : float
    {
        switch (unit)
        {
            case Units.Grams :
                return 1;
            case Units.KiloGrams :
                return 0.001;
            case Units.MilliGrams:
                return 1000;           
        }
    
        //unhandled unit?
        return 0;
    }
    

提交回复
热议问题