最近工作的开发人员开始在枚举通常适合的地方使用类模式而不是枚举.相反,他使用类似于下面的东西:
internal class Suit
{
public static readonly Suit Hearts = new Suit();
public static readonly Suit Diamonds = new Suit();
public static readonly Suit Spades = new Suit();
public static readonly Suit Clubs = new Suit();
public static readonly Suit Joker = new Suit();
private static Suit()
{
}
public static bool IsMatch(Suit lhs, Suit rhs)
{
return lhs.Equals(rhs) || (lhs.Equals(Joker) || rhs.Equals(Joker));
}
}
Run Code Online (Sandbox Code Playgroud)
他的推理是它无形地看起来像枚举,但允许他包含与枚举有关的方法(如上面的IsMatch),它包含在枚举本身中.
他称这是一个Enumeration课程,但这不是我以前见过的.我想知道优点和缺点是什么以及我可以在哪里找到更多信息?
谢谢
编辑:他描述的另一个优点是能够为枚举添加特定的ToString()实现.