June 7, 2013

Fun With Strongly Typed Enums!

Nobody likes using plain old boring Enums in C# these days! So why not try using a cool strongly typed Enum instead.


namespace Models
{
public class Alignment
{
public static readonly Alignment Good = new Alignment("Good");
public static readonly Alignment Neutral = new Alignment("Neutral");
public static readonly Alignment Evil = new Alignment("Evil");
private readonly string _value;
private Alignment(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
}
}
namespace Models
{
public interface IPerson
{
string Name { get; set; }
Alignment Alignment { get; }
string Gloat();
}
}
view raw gistfile1.cs hosted with ❤ by GitHub