Enum
The enum full name (enumeration), which is a unique type consisting of a set of named constants called enumerated lists.
In general, it is a good idea to define an enum directly within a namespace so that all classes in that namespace can access it as easily as possible.
Of course, enums can also be nested within a class or struct.
By default, the value of the first enumerator is 0, followed by the value of each enumerator incremented by 1. For example, the following enumeration, the Sat is 0,sun is 1,mon is 2 et.
0 1 2 and so on. enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
The enumerator can override the default values as shown in the following example.
enum Days {sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
In this enumeration, the element sequence is forced to start at 1 instead of 0.
However, general recommendations include constants with a value of 0。 For more information, see Enumeration types (C # Programming Guide).
Each enumeration type has an underlying type, which can be any integral type except char. The default underlying type of an enumeration element is int. To declare another integer enumeration (such as Byte), follow the type immediately after the identifier, and then use the colon, as shown in the following example.
enum byte {sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
The standard types of enums are Byte, sbyte, short, ushort, int, uint, long, or ulong.
Variables of type days can be assigned to any value in the range of the underlying type, and the assigned value is not limited to the named constant.
The default value for enum E is the value generated by expression (e) 0.
Description
The name of the enumerator cannot contain whitespace.
The most common place to enumerate enumerations is to use the Swith case to complete the if else function;
Enum Reference: https://msdn.microsoft.com/zh-cn/library/sbbt4032 (v=vs.120). aspx
FlagsAttribute
The enum is paired with the Flags (System.FlagsAttribute) property, which indicates that the enumeration can be treated as a bit field (that is, a set of flags). This allows you to perform an AND and (&), or OR (|), not non-(~), and xor (^) bitwise operations on it.
C # operator Https://msdn.microsoft.com/zh-cn/library/6a71f45d.aspx
FlagsAttribute Reference: Https://msdn.microsoft.com/zh-cn/library/system.flagsattribute.aspx
Demo
1. Define enum as Cweapontype
[System.flags] Public enum cweapontype{ unknow=0, //Attack weapon mat=2, //Evolutionary unknown material cat=4, Mouse=8, //Evolutionary material mouse PLUS=16, //Egg exp=32, //Experience Pig //Awakening Fragment money=128, //Money cat crit=256, //critical hit fragment fragment=512,//Fragment of the Shard //Weapon }
2. Determine whether fragment is a weapon
private void Start () {var type1 = Cweapontype.fragment; var type2 = Cweapontype.exp; var log1 = string . Format ( "{0} is weapon:{1}" , Type1, (int ) (cweapontype.weapon & type1)! = 0); var log2 = string . Format ( "\ t {0} is weapon:{1}" , Type2, (int ) (cweapontype.weapon & type2)! = 0); Debug.logwarning (Log1 + log2);}
3. Running Results
Fragment is Weapon:true Exp is Weapon:false
This demo is to determine whether fragment belongs to weapon?
The code uses the & symbol to judge, the result is 0 does not belong to, the result is (int) cweapontype.fragment is belongs to.
Attached: Turning string into enumeration
Cweapontype type= (Cweapontype) enum.parse (typeof"Normal");
Gets the name of the enumeration
string enumname=enum.getname (typeof4);
// result output: Cat
Resources
Http://www.dotnetperls.com/enum-flags
http://www.dotblogs.com.tw/atowngit/archive/2009/11/19/12051.aspx?fid=70079
C # Enum Flags