Brief Analysis of Enumeration Usage in.NET

  • 2021-06-28 09:05:57
  • OfStack

This paper gives a brief analysis of enumeration usage in.NET.Share it for your reference.Specific analysis is as follows:

The enumeration I understand is an optional value agreed upon in programming.For example, the online status of QQ, there are online, Q me, invisible, busy and so on.I think this is an enumeration.

1. General Enumeration

1) Instances

public enum UserState
{
 QMe ,
 OnLine ,
 OffLine     // Later omitted
}

ps: Like the enumeration above, it can be written separately in an CS file.

2) The nature of ordinary enumeration

1. Mutual Exclusion

2. Each value has a value to represent it

3. Enumerations are already represented by numbers at compile time.

3) Convert string to enumeration

(UserState)Enum.Parse(typeof(UserState),"Red") ;

4) Enumeration into numeric values, strings

(int)myColor.Red;
myColor.Red.ToString();

2. Identity Enumeration

But when a thing has multiple attributes, ordinary enumeration is not enough.

Ordinary enumerations are mutually exclusive, identities and enumerations can be combined.

1) Definition

// The result after use is Tostring () instead of a number. 
public enum GoodMan
{
   high =1 ,
   rich =2 ,
   Handsome =4     // This must be 2 Of N Power, never repeat.
}

2) Combination of states (or operations)

GoodMan winter = GoodMan. high | GoodMan. rich | GoodMan. Handsome; 

3) Determine if there is an enumerated value in winter (and operation)

if(winter&GoodMan. high ==GoodMan. high )

I hope that the description in this paper will be helpful to your.net program design.


Related articles: