码迷,mamicode.com
首页 > 编程语言 > 详细

C#: enum

时间:2015-05-05 12:08:41      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

C#的枚举类型跟C++差不多,一般我们将enum设为单个状态,比如enum color_t { RED, BLACK, GREEN}, 只能选择一个

而有的时候枚举可以作为位运算来进行与或运算,比如ControlStyles这个枚举,看下面一段从TabControlEx中的一段代码

技术分享
1            base.SetStyle(
2                 ControlStyles.UserPaint |
3                 ControlStyles.OptimizedDoubleBuffer |
4                 ControlStyles.AllPaintingInWmPaint |
5                 ControlStyles.ResizeRedraw |
6                 ControlStyles.SupportsTransparentBackColor,
7                 true);
8             base.UpdateStyles();       
View Code

这里我自己写了一段代码来看个别位是否设置了,注意&的优先级要比>小,需要括号起来

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace test4
 7 {
 8     class Program
 9     {
10         #region
11         public enum person
12         {
13             Id1 = 1,
14             Id2 = 2
15         }
16         #endregion
17         static void Main(string[] args)
18         {
19             person n = person.Id1 | person.Id2;
20             //person n = person.id1;
21             if ((n & person.Id1) > 0) Console.WriteLine("you 1");
22             if ((n & person.Id2) > 0) Console.WriteLine("you 2");
23             if ((n & person.Id1) > 0 && (n & person.Id2) > 0) Console.WriteLine("you 1 he 2");
24             Console.WriteLine(((int)n).ToString());
25         }
26     }
27 }
View Code

 

C#: enum

标签:

原文地址:http://www.cnblogs.com/yingzhongwen/p/4478697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!