Static typing is your friend here. If you need a string backed type for whatever reason, as opposed to using enums which are int backed, I've used a pattern like the following:
public class TimeUnit
{
private readonly string _unit;
private TimeUnit(string unit)
{
_unit = unit;
}
public static readonly TimeUnit Hour = new TimeUnit("hour");
public static readonly TimeUnit Day = new TimeUnit("day");
public string ToString()
{
return _unit;
}
}
With this, you still have access to the backing string, assuming the rest of your code relies on that, but you're also forcing clients to either pass in TimeUnit.Hour
or TimeUnit.Day
.