在赋值的基础上改变枚举的输出。 [英] Varying output of enum on the basis of assignment of values.
问题描述
假设我们有这个枚举(在c#中)
枚举工作日
{
周日,周二
,周二
,周三
,周四
,周五
,周二
}
我们需要执行以下操作:
Console.WriteLine(Weekdays.Sunday.ToString());
这是我得到输出为星期日
但是现在如果我将我的枚举修改为
enum 工作日
{
星期日,
星期一=星期日,
星期二,
星期三,
星期四,
星期五,
星期六
}
我得到输出
。
我再次改变我的枚举:
{
星期日,星期一= b $ b星期日,星期二
,星期三
=周日,周四
,周五
,
周六
}
我得到不同的输出再次。任何人都可以告诉我这里发生了什么。
如果你有两个在枚举中具有相同值的项目,它可以告诉你哪一个你的意思。
请记住,枚举存储为int,名称是一种语法糖,使您的代码更易于阅读。如果你说:
工作日dayOne = Weekdays.Sunday;这些名称并没有结转这些值。
平日dayTwo = Weekdays.Monday;然后dayOne和dayTwo包含相同的值:0
你不能工作如果是星期日,星期一或星期三,则从零值出来!
想一想:如果我有一个取整数的方法:
void MyMethod( int i)
{
Console.WriteLine(i);
}它能否说出这两者之间的区别:
MyMethod( 55 - 44 );和
MyMethod( 11 );
当然不能 - 只是得到一个数字值。这就是枚举!
我找到了原因。
非常有趣...
在你的情况下
< pre lang =cs> public enum 工作日
{
星期日,
星期一=星期日,
星期二,
星期三=星期日,
星期四,
星期五,
星期六
}
几个规则:
1.如果你没有为枚举设置任何值,它将按顺序逐步设置。(0 ...)
2.枚举是一种值类型
3.微软的建议总是将值设置为枚举,不要将其置于没有值的位置。
我们将讨论一些可能与现实世界无关的事情,但是对于优秀的开发人员来说非常重要。
您设置3枚具有相同值的枚举,但未设置Sun一天也是
所以星期日= 0
设定星期一=星期日(星期一也是0)
设定星期三=星期日(星期三也是星期三) 0)
在这种情况下.Net将选择他们添加到Enum的第一个订单
在你的情况下
星期日
星期一
星期三
任何上述枚举将打印星期日。星期日首先注册值0,你也可以参考。
星期日= 0
星期一= 0
星期三= 0
现在,如果您只为所有这些(星期日,星期一和星期三)设置值0,它将显示星期三,因为它设置为值和所有值与编译器选择最后一个相同。
如果您从另一个枚举中引用或设置它将选择第一个参考值(如果相同剂量不存在)
另一种情况:
我不管你是否测试其他枚举然后你会发现
星期二和星期四它总会显示星期四因为
看看编译器将如何定义这些价值
星期日= 0,
星期一= 0,
星期二= 1 ,
星期三= 0,
星期四= 1 ,
星期五= 2,
星期六= 3
好了现在编译将显示最后一个按顺序添加到枚举中。
所以它会显示星期四
不能检查相同的只是替换星期二和星期四的位置
然后它将返回星期二
另一个案例:
public 枚举工作日
{
星期日,
星期一=星期日,
星期二=星期一,
星期三=星期二,
星期四=星期三,
星期五=星期四,
星期六=星期五
}
现在它将打印< b>星期三
在这种情况下.net会选择中间的。因为有7个项目它将选择第4个如果有6个项目它将选择3 :)
结论总是为enum设置一些值。
享受......
Suppose we have this enum (in c#)
enum Weekdays
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
And we need to do the following:
Console.WriteLine(Weekdays.Sunday.ToString());
Here are I get output as "Sunday"
But now if I am modifying my enum as
enum Weekdays
{
Sunday,
Monday=Sunday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
I get the output Monday
.
Again I change my enum as:
{
Sunday,
Monday=Sunday,
Tuesday,
Wednesday=Sunday,
Thursday,
Friday,
Saturday
}
I get different outputs on this again. Can any one tell me what is happening here.
If you have two items that have the same value in an enum, it can;t tell which one you mean.
Bear in mind that an enum is stored as an int, and the "names" are a syntactic sugar to make your code easier to read. The names do not "carry forward" with the values, if you say:
Weekdays dayOne = Weekdays.Sunday; Weekdays dayTwo = Weekdays.Monday;then both dayOne and dayTwo contain the same value: 0
You can't work out from a value of zero if that was Sunday, Monday or Wednesday!
Think about it: if I have a method which takes an integer:
void MyMethod(int i) { Console.WriteLine(i); }Can it tell the difference between these two:
MyMethod(55 - 44);and
MyMethod(11);
Of course it can't - it just gets a numeric value. And that's just what a enum is!
Hi,
I found the cause.
Its very funny...
In your case
public enum Weekdays { Sunday, Monday=Sunday, Tuesday, Wednesday=Sunday, Thursday, Friday, Saturday }
Few rule :
1. If you don't set any value to enum it will set incrementally by order of it.(0...)
2. Enum is a value type
3. Microsoft's recommendation always set a value to enum don't place it without a value.
We will discusses about few things which may irrelevant to real world but those very important to know for good developers.
You set 3 enum with same value and you did not set Sunday also
so Sunday =0
set Monday =Sunday (mean Monday is also 0)
set Wednesday = Sunday (mean Wednesday is also 0)
in this case .Net will pick the First one in which order they added to Enum
in your case
Sunday
Monday
Wednesday
any of above enum will print Sunday. Sunday is register with value 0 at first and you refer the same.
Sunday=0
Monday=0
Wednesday=0
Now if you just set value 0 to all of them (Sunday,Monday and Wednesday) it will show Wednesday because its set to value and all value are same to compiler pick last one.
If you refer or set from another enum it will pick the 1st reference value (if same dose not exists)
Another case:
I don't whether you test other enum's if you then you will find
Tuesday and Thursday it will always show Thursday because
see how compiler will define those values
Sunday=0,
Monday=0,
Tuesday=1,
Wednesday=0,
Thursday=1,
Friday=2,
Saturday=3
Ok now compile will show the last one in order which they added in enum.
So it will show Thursday
No to check the same just alternate the position of Tuesday and Thursday
then it will return Tuesday
Another Case :
public enum Weekdays { Sunday, Monday = Sunday, Tuesday = Monday, Wednesday = Tuesday, Thursday = Wednesday, Friday = Thursday, Saturday = Friday }
Now it will print Wednesday
In this scenario .net will pick the middle one. as there were 7 item it will pick 4th if there 6 item it will pick 3 :)
Conclusion always set some value against enum.
Enjoy...
这篇关于在赋值的基础上改变枚举的输出。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!