Switch case and generics checking

后端 未结 7 761
情话喂你
情话喂你 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 11:06

    Another way to do switch on generic is:

    switch (typeof(T))
    {
        case Type intType when intType == typeof(int):
            ...
        case Type decimalType when decimalType == typeof(decimal):
            ...
        default:
            ...
    }
    

提交回复
热议问题