Skip to content

Instantly share code, notes, and snippets.

@jagregory
Created February 11, 2011 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jagregory/822227 to your computer and use it in GitHub Desktop.
Save jagregory/822227 to your computer and use it in GitHub Desktop.
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;
}
}
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;
}
}
@jagregory
Copy link
Author

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.

@juanplopes
Copy link

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