zoukankan      html  css  js  c++  java
  • C#中Enum用法小结

     

    enums枚举是值类型,数据直接存储在栈中,而不是使用引用和真实数据的隔离方式来存储。

    (1)默认情况下,枚举中的第一个变量被赋值为0,其他的变量的值按定义的顺序来递增(0,12,3...),因此以下两个代码定义是等价的:

    [csharp] view plaincopy
     
    1. enum TrafficLight  
    2. {  
    3.     Green,  
    4.     Yellow,  
    5.     Red  
    6. }  
    [csharp] view plaincopy
     
    1. enum TrafficLight  
    2. {  
    3.     Green = 0,  
    4.     Yellow = 1,  
    5.     Red = 2  
    6. }  

    (2)enum枚举类型的变量的名字不能相同,但是值可以相同,例如:
    [csharp] view plaincopy
     
    1. enum TrafficLight  
    2. {  
    3.     Green = 0,  
    4.     Yellow = 1,     // Duplicate value, OK   
    5.     Red = 1         // Duplicate value, OK   
    6. }  
    (3)如果enum中的部分成员显式定义了值,而部分没有;那么没有定义值的成员还是会按照上一个成员的值来递增赋值,例如:
    [csharp] view plaincopy
     
    1. enum LoopType  
    2. {  
    3.     None,          // value is 0    
    4.     Daily,         // value is 1    
    5.     Weekly = 7,  
    6.     Monthly,       // value is 8    
    7.     Yeayly,        // value is 9    
    8.     DayGap = 15,           
    9.     WeekGap,       // value is 16    
    10.     MonthGap,      // value is 17    
    11.     YearGap        // value is 18    
    12. }  
    (4)enum枚举成员可以用来作为位标志,同时支持位的操作(位与,位或等等),例如:
    [csharp] view plaincopy
     
    1. enum CardDeckSettings : uint  
    2. {  
    3.     SingleDeck = 0x01,      // Bit 0  
    4.     LargePictures = 0x02,   // Bit 1  
    5.     FancyNumbers = 0x04,    // Bit 2  
    6.     Animation = 0x08        // Bit 3      
    7. }  
  • 相关阅读:
    桟错误分析方法
    gstreamer调试命令
    sqlite的事务和锁,很透彻的讲解 【转】
    严重: Exception starting filter struts2 java.lang.NullPointerException (转载)
    eclipse 快捷键
    POJ 1099 Square Ice
    HDU 1013 Digital Roots
    HDU 1087 Super Jumping! Jumping! Jumping!(动态规划)
    HDU 1159 Common Subsequence
    HDU 1069 Monkey and Banana(动态规划)
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4998268.html
Copyright © 2011-2022 走看看