".NET framework program design" Chapter 13 enumeration type and bit mark

xiaoxiao2021-03-06  87

Chapter 13 Enumeration Types and Bit Number

First, enumeration type

1. Use enumeration type:

l Enumeration Type is a program easier to write, read, and maintain, use symbol names in the code instead of the number of programming.

l Strong type, easy type test

2. Notes:

l Enumeration Type Inherits from System.enum, System.enum inherits from System.Valurtype

l Enumeration Types No way to define methods, properties, events

l Enumeration Types are constant rather than read-only fields, so it may introduce version issues (see related discussions in Chapter 8)

l Definition of the type of enumerated type with reference to the same level, can reduce the workload of the code entry

3, the application of methods in system.enum:

l public static type getunderlyingtype (Type EnumType);

Gets to save an enumerated type instance worth underlying type. Declare the basic type syntax used by an enumeration type as follows:

ENUM HUMAN: BYTE

{

Male,

Female

}

Then call the above method ENUM.GETUNDERLYINGTYPE (TypeOf (human); will return System.byte;

l public override string tostring ();

Public String Tostring (String); // Parameter is a format string

Public Static String Format (Type EnumType, Object Value, String Format);

// Value - Value to be converted, format - format string (G, G, X, X, D, D, F, F)

l Public static array getValues ​​(Type EnumType);

Get an array of constant values ​​in enumeration

l Public Static String GetName (Type EnumType, Object Value);

Retrieve the name of the constant with the specified value in the specified enumeration

l public static string [] getNames (type enjoy ";

Retrieve an array of constant names in the specified enumeration.

l Public Static Object Parse (TYPE, STRING);

Public Static Object Parse (TYPE, STRING, BOOL);

Convert one or more character strings of one or more of the ordinary names or digital values ​​to the equivalent enumeration object

l Public Static Bool IsDefined (Type EnumType, Object Value);

Returns whether there is an indication of whether there is a constant with the specified value in the specified enumeration, Value is the value or name of the constant.

l series TOOBJECT method

Returns an instance of the type of enumeration type set to the specified value

Second, the bit mark

l Use the System.FlagSattributes customization feature, so that the TOSTRING or FORMAT method can find each matching symbol in the enumeration value, connect them to a string, separated by a comma; Parse method can use this feature to split string and get composite Enumerated type

l There is also the same effect using the format string f or F

The following example illustrates the above

Using system;

[Flags] // Tailor

Public Enum Human: BYTE / / Customized Basic Type

{

Male = 0x01,

Female = 0x10

}

Public Class EnumTest

{

Public static void

Main

()

{

Human human = human.male | Human.female; //man demon?

Console.writeline (human.tostring ()); // Use FLAGS customization characteristics

//Console.writeline (HUMAN.TOSTRING ("f ")); // Do not use the FLAGS customization characteristics

Console.Writeline (enum.format (Typeof (Human), Human, "G")); // Use Flags customization characteristics

//Console.writeline (TYPEOF (HUMAN), HUMAN, "F"))); // does not use Flags customization characteristics

Human = (human) enum.parse (Typeof (Human), "17");

Console.writeline (human.tostring ()); // Use FLAGS customization characteristics

//Console.writeline (HUMAN.TOSTRING ("f ")); // Do not use the FLAGS customization characteristics

}

}

/*operation result

Male, female

Male, female

Male, female

* /

Note: Note in the above program is syntax when not using Flags feature

转载请注明原文地址:https://www.9cbs.com/read-122392.html

New Post(0)