其实不用自己写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了,不知道为啥会有人想不开,觉得这样写不好看...
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了,不知道为啥会有人想不开,觉得这样写不好看...
相关文章推荐
- .Net开发新的知识积累(Enum, protected,前台用后台和PageBase中定义的公用方法,不用ItemDataBound)
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- .NET不用代码生成器自己写一个生成Code的DLL 自动动态生成三层架构(一)概况
- 不用Unity库,自己实现.NET轻量级依赖注入
- 不用Unity库,利用.NET动态代理自己实现AOP
- 有时候真的觉得自己真tmd可以埋头搞技术,不用管别的
- 谁能提供1个.net下的多语言解决方案[mutil language solution]
- Golang的CSP很酷?其实.NET也可以轻松完成
- 【持续更新】.Net 开发中给自己埋下的坑!
- .NET和JAVA的跨平台,我们很期望,其实不容易
- 2017-03-03 Oracle在.Net中出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误
- 不用第三方之C#自己实现Visual SourceSafe 管理Oracle的程序
- 如果你想从自己的应用程序检查GitHub的状态,我们提供一个基本的JSON API
- 如何将自己的App作为外部数据源提供给Android系统搜索?
- .NET Datetime直接输出中文星期,不用单独写Switch循环
- ADO.NET入门教程(二)了解.NET数据提供程序
- 自己误点AdSense广告不用再通知Google了
- .NET:InTheHand提供的类操作SMS
- 自己动手写三层(不用代码生成器)
- Enum定义位域, 即可以通过位操作来产生未命名的值