Switch case and generics checking

后端 未结 7 776
情话喂你
情话喂你 2021-01-01 10:36

I want to write a function that format int and decimal differently into string

I have this code:

and I want to rewrite it to generi

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-01 10:58

    You could check the type of the variabele;

        public static string FormatWithCommaSeperator(T value)
        {
            if (value is int)
            {
                // Do your int formatting here
            }
            else if (value is decimal)
            {
                // Do your decimal formatting here
            }
            return "Parameter 'value' is not an integer or decimal"; // Or throw an exception of some kind?
        }
    

提交回复
热议问题