当前位置: 首页 > news >正文

C# 中的枚举类型 enum (属于值类型)

原文 C# 中的枚举类型 enum (属于值类型)

 

C# 支持两种特殊的值类型:枚举和结构。
声明枚举:声明时要声明所有可能的值。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using  System; 
using  System.Collections.Generic; 
using  System.Linq; 
using  System.Text; 
   
namespace  enumType 
     enum  Season      // enum 类型定义在 class 外面 
    
         Spring, Summer, Fall, Winter     // 最后一个元素后面不加" ; " 
    
   
     class  Program 
    
         //enum Season     // 枚举变量定义在此处也可以 
         //{ 
         //    Spring, Summer, Fall, Winter     
         //} 
   
         static  void  Main( string [] args) 
        
             Season beauty = Season.Fall; 
             Season coldSeason = Season.Winter; 
             Season currentSeason = Season.Summer; 
   
             Console.WriteLine( "The beautiful season is {0}." , beauty);               
             // 用 WriteLine 显示枚举变量时,编译器会自动生成代码,输出和变量值匹配的字符串 
   
             Console.WriteLine( "The beautiful season is {0}." , beauty.ToString());    
             // 也可以使用 ToString 方法,显式地将一个枚举变量转换成代表其当前值的一个字符串 
   
             Console.WriteLine( "The current season is {0}." , currentSeason); 
             Console.WriteLine( "{0} is very cold." , coldSeason); 
        
    

 

 运行后结果如下:

在枚举的内部,它的每个元素都关联(对应)着一个整数值。默认情况下,第一个对应整数 0,以后每个元素所对应的整数都递增 1。我们可以获取一个枚举变量的基础整数值,为此,必须先将它转换为基本类型。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using  System; 
using  System.Collections.Generic; 
using  System.Linq; 
using  System.Text; 
   
namespace  enumType 
     enum  Season      // enum 类型定义在 class 外面 
    
         Spring, Summer, Fall, Winter     // 最后一个元素后面不加" ; " 
    
   
     class  Program 
    
         static  void  Main( string [] args) 
        
             Season currentSeason = Season.Summer; 
   
             Console.WriteLine( "Summer is {0}" , ( int )currentSeason);  // 枚举的基础整数值 
        
    

 

运行后结果如下:

 也可以把一个特定的整数常量和一个枚举类型的文字常量关联起来。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using  System; 
using  System.Collections.Generic; 
using  System.Linq; 
using  System.Text; 
   
namespace  enumType 
     enum  Season      // 定义为 short 可以节省空间 
    
         Spring = 168, Summer, Fall, Winter     // 最后一个元素后面不加" ; " 
    
   
     class  Program 
    
         static  void  Main( string [] args) 
        
             Console.WriteLine( "Spring is {0}" , ( int )Season.Spring);      // 168 
             Console.WriteLine( "Summer is {0}" , ( int )Season.Summer);      // 169 依次 +1 
             Console.WriteLine( "Fall is {0}" , ( int )Season.Fall);          // 170 
             Console.WriteLine( "Winter is {0}" , ( int )Season.Winter);      // 171 
        
    

 

 运行后结果如下:

多个枚举文字常量可能拥有相同的基础值,可以像如下这样声明。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using  System; 
using  System.Collections.Generic; 
using  System.Linq; 
using  System.Text; 
   
namespace  enumType 
     enum  Season      // enum 类型定义在 class 外面 
    
         Spring, Summer, Fall, Autumn = Fall, Winter     // 最后一个元素后面不加" ; " 
    
   
     class  Program 
    
         static  void  Main( string [] args) 
        
             Console.WriteLine( "Spring is {0}" , ( int )Season.Spring);      // 0 
             Console.WriteLine( "Summer is {0}" , ( int )Season.Summer);      // 1 
             Console.WriteLine( "Fall is {0}" , ( int )Season.Fall);          // 2 
             Console.WriteLine( "Autumn is {0}" , ( int )Season.Autumn);      // 2 基础值相同 
             Console.WriteLine( "Winter is {0}" , ( int )Season.Winter);      // 3 
        
    

 

运行后结果如下:

声明枚举时,枚举的文字常量将默认获得 int 类型的值。但可以选择枚举的基本类型。

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。

    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/5948127.html ,如需转载请自行联系原作者

相关文章:

  • jQuery选择器之表单对象属性过滤选择器Demo
  • Cloudera Mountable HDFS (hadoop-fuse-dfs).
  • linux reiserfs文件系统损坏后的数据恢复过程记录
  • 把一个用户的相关权限赋予另外一个用户
  • gets函数的不安性详解
  • Silverlight知识链接整理(11月-12月)
  • ORACLE 分区与索引
  • C# 视频监控系列(2):客户端——封装API (1)
  • Silverlight书籍推荐阅读排行榜【续】
  • 文本编辑命令
  • VI批量替换
  • Heron and His Triangle 2017 沈阳区域赛
  • Oracle在线 redo log文件丢失后的恢复
  • python time 与datetime之间的区别与联系
  • Exchange帐号如何手动更新?
  • canvas 绘制双线技巧
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • JSDuck 与 AngularJS 融合技巧
  • Logstash 参考指南(目录)
  • mongo索引构建
  • Promise面试题2实现异步串行执行
  • Python socket服务器端、客户端传送信息
  • React 快速上手 - 07 前端路由 react-router
  • V4L2视频输入框架概述
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 前端学习笔记之观察者模式
  • 前嗅ForeSpider教程:创建模板
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 白色的风信子
  • FaaS 的简单实践
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • 阿里云服务器购买完整流程
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (8)STL算法之替换
  • (DFS + 剪枝)【洛谷P1731】 [NOI1999] 生日蛋糕
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (一) springboot详细介绍
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)C#开发微信门户及应用(1)--开始使用微信接口
  • .Net Web窗口页属性
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .net 后台导出excel ,word
  • .NET的数据绑定
  • .Net小白的大学四年,内含面经
  • // an array of int
  • @JsonSerialize注解的使用
  • @ModelAttribute使用详解
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [2]十道算法题【Java实现】
  • [Android] 240204批量生成联系人,短信,通话记录的APK
  • [Asp.net mvc]国际化