枚举是有类型的。
That is, if you have a method where you have to pass a certain state to a method for instance, you can only pass valid arguments.
For instance:
enum OrderState
{
pending = 1,
shipped = 2
}
public IList<Order> GetOrdersInState( OrderState )
{
}
This is a good example -imho- of using enums.
When OrderState is an int for which you create 2 const ints, you have no restriction and are able to pass invalid values. The compiler won t complain.
然而,您提出的案例,我认为使用枚举不是一个有效的解决方案。这是对int的错误使用,应使用const int。
Enums are good, but they should be used where they must be used. They re not the preferred tool in every situation.
Having a const or static var is in this case not an antipattern.