Can I use inheritance with an extension method?

后端 未结 3 1670
猫巷女王i
猫巷女王i 2021-01-17 19:10

I have the following:

public static class CityStatusExt
{
    public static string D2(this CityStatus key)
    {
        return ((int) key).ToString(\"D2\");         


        
3条回答
  •  [愿得一人]
    2021-01-17 19:23

    You can make the method generic. C# will infer the type:

    public static class Extension 
    { 
        public static string D2 (this T key) 
        { 
            return ((int)(object) key).ToString("D2"); 
        } 
    }
    

提交回复
热议问题