您的位置:首页 > 其它

其实不用自己写Enum的位操作的,.net提供了

2009-01-05 16:39 260 查看
using System;

class FlagsAttributeDemo

{

// Define an Enum without FlagsAttribute.

enum SingleHue : short

{

Black = 0,

Red = 1,

Green = 2,

Blue = 4

};

// Define an Enum with FlagsAttribute.

[FlagsAttribute]

enum MultiHue : short

{

Black = 0,

Red = 1,

Green = 2,

Blue = 4

};

static void Main( )

{

Console.WriteLine(

"This example of the FlagsAttribute attribute \n" +

"generates the following output." );

Console.WriteLine(

"\nAll possible combinations of values of an \n" +

"Enum without FlagsAttribute:\n" );

// Display all possible combinations of values.

for( int val = 0; val <= 8; val++ )

Console.WriteLine( "{0,3} - {1}",

val, ( (SingleHue)val ).ToString( ) );

Console.WriteLine(

"\nAll possible combinations of values of an \n" +

"Enum with FlagsAttribute:\n" );

// Display all possible combinations of values.

// Also display an invalid value.

for( int val = 0; val <= 8; val++ )

Console.WriteLine( "{0,3} - {1}",

val, ( (MultiHue)val ).ToString( ) );

}

}

/*

This example of the FlagsAttribute attribute

generates the following output.

All possible combinations of values of an

Enum without FlagsAttribute:

0 - Black

1 - Red

2 - Green

3 - 3

4 - Blue

5 - 5

6 - 6

7 - 7

8 - 8

All possible combinations of values of an

Enum with FlagsAttribute:

0 - Black

1 - Red

2 - Green

3 - Red, Green

4 - Blue

5 - Red, Blue

6 - Green, Blue

7 - Red, Green, Blue

8 - 8

*/

最近看到有人自己写Enum的位操作来判读OR和AND的情况...

其次,我认为写SexEnum.男没有问题,毕竟这个产品不是用作多语言里的,都是中文的,枚举就用中文就ok了,不知道为啥会有人想不开,觉得这样写不好看...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐