Car*_*rra 5 .net c# enums unique
I just noticed that you can create something like this in C#:
enum TestEnum
{
First = 1,
Second = 1,
Third = 2
}
[TestMethod]
public void Test()
{
TestEnum test = TestEnum.First;
var en = (TestEnum) 1;//And en is now set to First
}
Run Code Online (Sandbox Code Playgroud)
We would like the enums value to be unique. Right now, I have written a unit test that checks that each value is only used once in our enum. Is there a better way to force the uniqueness?
小智 5
枚举用于赋予整数含义。当你说 Second = 1 没有意义时,想想这样的例子:
enum HumanEnum
{
Man = 1,
Woman = 0,
Lady = 0,
Boy = 1,
Girl = 0
}
Run Code Online (Sandbox Code Playgroud)
假设 0 和 1 代表性别,这样 Man 和 Boy 具有相同的性别值。
| 归档时间: |
|
| 查看次数: |
480 次 |
| 最近记录: |