An alternative to using an enum could be something like this:
public class ColorString
{
private readonly string _value;
public static ColorString DarkRed = new ColorString("Dark red");
public static ColorString LightBlue = new ColorString("Light blue");
private ColorString(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
}
This is equivalent to the enum but retains the string's value and allows you to add additional behaviour to the type.
If you are feeling ambitious, you can even allow it to be treated as a string with:
public static implicit operator string(ColorString colorString)
{
return colorString.ToString();
}
Though this should be treated with caution.
If it really has to be a string, you could add a runtime check on setting the value via the property setter. Something like:
public class Route
{
private string _color;
private readonly ISet<string> _validColors
= new HashSet<string> {"Dark red", "Light blue"};
public string Color
{
get { return _color; }
set
{
if (_validColors.Contains(value))
_color = value;
else
throw new ArgumentException("Invalid color");
}
}
}
This doesn't stop the value being set directly on _color
though.