Created
February 11, 2011 11:34
-
-
Save jagregory/822227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyEnum | |
{ | |
public static readonly MyEnum Value1 = new MyEnum("value1"); | |
public static readonly MyEnum Value2 = new MyEnum("value2"); | |
readonly string name; | |
MyEnum(string name) | |
{ | |
this.name = name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Country | |
{ | |
public static readonly Country US = new Country("US", "United States", "USD"); | |
public static readonly Country GB = new Country("GB", "Great Britain", "GBP"); | |
public string CountryCode { get; private set; } | |
public string Description { get; private set; } | |
public string Currency { get; private set; } | |
Country(string countryCode, string description, string currency) | |
{ | |
CountryCode = countryCode; | |
Description = description; | |
Currency = currency; | |
} | |
} |
https://gist.github.com/823834
This way you don't need to override Equals nor GetHashCode.
You still have to override string, but it's supposed to be easier.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obviously, you need to override
Equals
,ToString
and what-not to make it actually useful.This example doesn't give anything over a real enum, but it's a lot more flexible. My current example is adding a "value" that's something other than an
int
. Throw in some operator overloads and it can be quite tasty.