现摘抄MSDN示例如下,请注意中文注释处的疑问注释1:这里算赋值?或作其它的理解,但为什么有按位或运算符“|”。如果是赋值,那Seasons.Winter 为什么不为4,而是winter?注释2:这里的表达式和上面差不多,是不是说一个键可以对应多个键值?注释3:既然e.Value 和 seasons[i] 的值都是字符串值,又怎么可以按位与运算?另外这个if块说明了什么,也就是说 ((Seasons)e.Value & seasons[i]) > 0 能说明什么问题恭迎各位解答,老夫多谢啦!!!using System;
using System.Collections;// A traditional enumeration of some root vegetables.
public enum SomeRootVegetables
{
HorseRadish,
Radish,
Turnip
}// A bit field or flag enumeration of harvesting seasons.
[Flags]
public enum Seasons
{
None = 0,
Summer = 1,
Autumn = 2,
Winter = 4,
Spring = 8,
All = Summer | Autumn | Winter | Spring // 注释1
}// Entry point.
public class EnumerationSample
{
public static void Main()
{
// Hash table of when vegetables are available.
Hashtable AvailableIn = new Hashtable(); AvailableIn[SomeRootVegetables.HorseRadish] = Seasons.All;
AvailableIn[SomeRootVegetables.Radish] = Seasons.Spring;
AvailableIn[SomeRootVegetables.Turnip] = Seasons.Spring |
Seasons.Autumn; // 注释2 // Array of the seasons, using the enumeration.
Seasons[] seasons = new Seasons[] { Seasons.Winter, Seasons.Spring,
Seasons.Summer, Seasons.Autumn }; // Print information of what vegetables are available each season.
for (int i = 0; i < seasons.Length; i++)
{
Console.WriteLine("\r\nThe following root vegetables are
harvested in " + seasons[i].ToString("G") + ":");
foreach (DictionaryEntry e in AvailableIn)
{
// A bitwise comparison.
if (((Seasons)e.Value & seasons[i]) > 0) //注释3
Console.WriteLine("\t" +
((SomeRootVegetables)e.Key).ToString("G"));
}
}
}
}
此程序的输出如下所示。
The following root vegetables are harvested in Summer:
HorseRadish
The following root vegetables are harvested in Autumn:
Turnip
HorseRadish
The following root vegetables are harvested in Winter:
HorseRadish
The following root vegetables are harvested in Spring:
Turnip
Radish
HorseRadish
using System.Collections;// A traditional enumeration of some root vegetables.
public enum SomeRootVegetables
{
HorseRadish,
Radish,
Turnip
}// A bit field or flag enumeration of harvesting seasons.
[Flags]
public enum Seasons
{
None = 0,
Summer = 1,
Autumn = 2,
Winter = 4,
Spring = 8,
All = Summer | Autumn | Winter | Spring // 注释1
}// Entry point.
public class EnumerationSample
{
public static void Main()
{
// Hash table of when vegetables are available.
Hashtable AvailableIn = new Hashtable(); AvailableIn[SomeRootVegetables.HorseRadish] = Seasons.All;
AvailableIn[SomeRootVegetables.Radish] = Seasons.Spring;
AvailableIn[SomeRootVegetables.Turnip] = Seasons.Spring |
Seasons.Autumn; // 注释2 // Array of the seasons, using the enumeration.
Seasons[] seasons = new Seasons[] { Seasons.Winter, Seasons.Spring,
Seasons.Summer, Seasons.Autumn }; // Print information of what vegetables are available each season.
for (int i = 0; i < seasons.Length; i++)
{
Console.WriteLine("\r\nThe following root vegetables are
harvested in " + seasons[i].ToString("G") + ":");
foreach (DictionaryEntry e in AvailableIn)
{
// A bitwise comparison.
if (((Seasons)e.Value & seasons[i]) > 0) //注释3
Console.WriteLine("\t" +
((SomeRootVegetables)e.Key).ToString("G"));
}
}
}
}
此程序的输出如下所示。
The following root vegetables are harvested in Summer:
HorseRadish
The following root vegetables are harvested in Autumn:
Turnip
HorseRadish
The following root vegetables are harvested in Winter:
HorseRadish
The following root vegetables are harvested in Spring:
Turnip
Radish
HorseRadish
可能我连枚举的基本概念都没弄清楚,如果你手头有枚举的详细论述,不妨贴给我,希望大家帮我UP!谢谢注释1: 为什么讲“枚举类型本质上是整型?”并且Season.Winter值可以转换成整型注释2: 我不懂表达式 Seasons.Spring | Seasons.Autumn 到底是什么意思?一字符串?注释3: 还是不明白
注释2:这里的表达式和上面差不多,是不是说一个键可以对应多个键值?没错,Flags特性标识多值枚举。注释3:既然e.Value 和 seasons[i] 的值都是字符串值,又怎么可以按位与运算?另外这个if块说明了什么,也就是说 ((Seasons)e.Value & seasons[i]) > 0 能说明什么问题错!e.Value和seasons[i]都不是字符串,都是枚举值,而前面说了枚举值存在到int的隐式类型转换,int可以按位与运算。按位与运算经常用作掩码计算,简而言之,这里是判断e.Value是否包含seasons[i]季节。
注释1:我不明白表达式Seasons.Winter输出的是“Winter”,“Winter”是一字符串对吧?字符串怎么可以隐式的转换成int型呢另外ToString("G")这个"G"是作什么用的?,居然没报错
为什么说((Seasons)e.Value & seasons[i]) > 0 ,说明了e.Value包含了seasons[i]季节?我知道这里进行了布尔运算,但楼上的我看不大懂,谁帮我打通任督二脉
只不过是Season.Winter.ToString()方法输出"Winter"罢了,怎么能说Season.Winter是字符串呢?概念不清。所有类型的对象在输出的时候多半都是调用他们的ToString方法,如int i = 1;的ToString返回"1",那int不也是字符串了?"G"是格式化字符串,表明输出格式。多翻翻文档。
最后一个问题就必须你自己去看书了。
例如,假定唯一的枚举常数命名为“Red”,其值为 1。如果将 value 指定为 1,则此格式返回“Red”。然而,如果将 value 指定为 2,则此格式返回“2”。- 或 -如果将 FlagsAttribute 自定义属性应用于枚举,则 value 将被视为位域,该位域包含一个或多个由一位或多位组成的标志。如果 value 等于已命名的枚举常数的组合,则返回用分隔符分隔的这些常数名称的列表。将在 value 中搜索标志,从具有最大值的标志到具有最小值的标志进行搜索。对于与 value 中的位域相对应的每个标志,常数的名称连接到用分隔符分隔的列表。则将不再考虑该标记的值,而继续搜索下一个标志。如果 value 不等于已命名的枚举常数的组合,则返回 value 的等效十进制数。
“X”或“x” 以十六进制形式表示 value(不带前导“0x”)。
“D”或“d” 以十进制形式表示 value。
“F”或“f” 对于“G”或“g”执行的行为是相同的,只是在 Enum 声明中不需要FlagsAttribute。