An enum
may not be the right construct to model this kind of problem.
I would suggest creating a class to represent country information, and provide methods the convert to and from numeric representations. With problems like this, you also have to decide what coding value you will use when converting a selected Country instance into a numeric value.
The Enum Object pattern can be helpful starting point for modeling this kind of situation:
public sealed class Country
{
// initialize appropriately in the constructor...
private readonly int[] m_Values;
private readonly string m_Name;
// make the constructor private so that only this class can set up instances
private Country( string name, int[] codes ) { ... }
public static Country US = new Country("United States", new[]{ 1,2 } );
public static Country Canada = new Country("Canada", new[] {3,4} );
public static Country FromCode( int code ) { ... }
public override string ToString() { return m_Name; }
// ... etc...
}
Based on your example, you should also consider whether you need to model Country sub-regions as first-class entities, rather than simply folding them into the implementation details of your Country enumeration. Whether you should do this or not depends on your requirements and use cases, so only you can make an appropriate decision on that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…