2

Possible Duplicate:
Anyone know a good workaround for the lack of an enum generic constraint?
Can I use inheritance with an extension method?

I have created extensions methods for various enums to get their strings in certain format. Is there a simpler way to make the code looks smart so that I can just add the type and extension method should be created for that type.

    public static string GetString(this State p1)
    {
        return p1.ToString("F");
    }

    public static string GetString(this Brand p1)
    {
        return p1.ToString("F");
    }

    public static string GetString(this Status p1)
    {
        return p1.ToString("F");
    }

Thank you.

Community
  • 1
  • 1
Kannaiyan
  • 12,554
  • 3
  • 44
  • 83
  • 4
    I don't understand. Why do you create an extension method when you could just as well use ToString() ? – Falanwe Oct 29 '12 at 16:54
  • I swear I answered a similar question within the past week, just with .ToString("N2") instead, but I can't find it... – lc. Oct 29 '12 at 16:55
  • A wrapper method overload around a single generic method? I don't get it. – LightStriker Oct 29 '12 at 16:56
  • What you want is a generic constraint of type `enum`, but such a thing does not exist. http://stackoverflow.com/questions/7244/anyone-know-a-good-workaround-for-the-lack-of-an-enum-generic-constraint – Servy Oct 29 '12 at 16:56
  • 1
    Found it http://stackoverflow.com/questions/13043827/can-i-use-inheritance-with-an-extension-method/13043866#13043866 – lc. Oct 29 '12 at 16:57
  • `code` public static string GetString(this Enum p1) { return p1.ToString("F"); } – Kannaiyan Oct 29 '12 at 17:07
  • Enums are evil.http://www.colourcoding.net/blog/archive/2009/07/25/enums-are-evil-the-state-pattern.aspx – Richard Schneider Oct 29 '12 at 17:21

0 Answers0