I have the following:
public static class CityStatusExt
{
public static string D2(this CityStatus key)
{
return ((int) key).ToString("D2");
}
public static class CityTypeExt
{
public static string D2(this CityType key)
{
return ((int) key).ToString("D2");
}
Plus other classes with similar extensions that return the key formatted as a “D2”
Is there a way I could inherit from a base class and have the base class provide the functionality so
don’t I don’t have to repeat the same extension method code?
Update. I am sorry I did not mention this but my classes like CityType are Enums.
From the comment below,
CityTypeandCityStatusare enums. Therefore you can do this:Original answer:
You can use a generic method and an interface
ID2Able:This way the extension method won’t show up for absolutely every type; it’ll only be available for things you inherit
ID2Ablefrom.