Summary of Enum in C #

Source: Internet
Author: User

Http://apps.hi.baidu.com/share/detail/23749270

1. Definition of EnumEnum Fabric

{Cotton = 1, silk = 2, wool = 4, rayon = 8, other = 128}
2. Conversion between symbol names and constant values Fabric Fab = fabric. cotton; int fabnum = (INT) Fab; // convert to a constant value. Mandatory conversion is required. Fabric fabstring = (fabric) 1; // convert a constant value to a symbolic name. If tostring () is used (Fabric) 1). tostring (), note that there must be brackets. String fabtype = Fab. tostring (); // display the symbol name string fabval = Fab. tostring (" D "); // Display a constant value 3. Obtain all symbolic names (For details, refer to the enum class) Public Enum myfamily {yangzhiping = 1, guanguiqin = 2, yanghaoran = 4, Liwei = 8, guanguizhi = 16, lisiwen = 32,
Lisihua = 64,} foreach (string s in Enum. getnames (Typeof (Myfamily) {console. writeline (s );} 4. Process enumeration as a bit flag
According to the two examples below, on the one hand, setting the flag [flags] or [flagsattribute] indicates that the symbol name must be listed. On the other hand, you can use force conversion to list the symbol names, convert a number Symbol name. Inaccurate. Let's take a look at the example below. Note:
Example 1: Fabric Fab = fabric. cotton | fabric. rayon | fabric. silk; console. writeline ("myfabric = {0}", FAB); // output: fabric. cotton | fabric. rayon | fabric. silk;
Example 2: Class flagsattributedemo {// define an Enum without flagsattribute. Enum singlehue: Short {black = 0, Red = 1, Green = 2, Blue = 4 };
// Define an Enum with flagsattribute. [flagsattribute]
Enum multihue: Short {black = 0, Red = 1, Green = 2, Blue = 4 };
Static void main () {console. writeline ("this example of the flagsattribute attribute \ n" + "generates the following output. "); console. writeline ("\ Nall possible combinations of values of an \ n" + "Enum without flagsattriations: \ n"); // display all possible combinations of values. For (INT val = 0; Val <= 8; Val ++) Console. writeline ("{0, 3}-{1}", Val, (singlehue) Val). tostring ()); Console. writeline ("\ Nall possible combinations of values of an \ n" + "Enum with flagsattriations: \ n"); // display all possible combinations of values. // also display an invalid value. For (INT val = 0; Val <= 8; Val ++) Console. writeline ("{0, 3}-{1}", Val, (multihue) Val). tostring ()); }}
/* This example of the flagsattribute attribute generates the following output.
All possible combinations of values of an Enum without flagsattribute:
0-black 1-Red 2-green 3-3 4-Blue 5-5 6-6 7-7 8-8
All possible combinations of values of an Enum with flagsattriations:
0-black 1-Red 2-green 3-red, green 4-Blue 5-red, blue 6-green, blue 7-red, green, blue 8-8 */
5. enumeration is used as a function parameter. It is often used in combination with a switch. The following example

Public static double getprice (fabric FAB)

{Switch (FAB)

{Case fabric. Cotton: Return (3.55 );

Case fabric. Silk: Return (5.65 );

Case fabric. wool: Return (4.05 );

Case fabric. Rayon: Return (3.20 );

Case fabric. Other: Return (2.50 );

Default: Return (0.0 );}}

6. The above 3.1 complete examples

// 1. Definition of Enum public Enum fabric: short

{Cotton = 1, silk = 2, wool = 3, rayon = 8, other = 128}

// Pass enumeration as a parameter public static double getprice (fabric FAB)

{Switch (FAB)

{

Case fabric. Cotton: Return (3.55 );

Case fabric. Silk: Return (5.65 );

Case fabric. wool: Return (4.05 );

Case fabric. Rayon: Return (3.20 );

Case fabric. Other: Return (2.50 );

Default: Return (0.0 );}}

Public static void main ()

{Fabric Fab = fabric. cotton;

Int fabnum = (INT) Fab;

String fabtype = Fab. tostring ();

String fabval = Fab. tostring ("D ");

Double cost = getprice (FAB );

Console. writeline ("fabnum = {0} \ nfabtype = {1} \ nfabval = {2} \ n", fabnum, fabtype, fabval );

Console. writeline ("cost = {0}", cost );
}

7. Use of Enum class

Enum. isdefinde and enum. parse are often used together to determine whether a value or symbol is an enumeration member and then create an instance. Enum. getname prints the value of a member. enum. getnames prints the value of all members. Note:Typeof. This is important.

Public Enum myfamily

{Yangzhiping = 1, guanguiqin = 2, yanghaoran = 4, Liwei = 8, guanguizhi = 16, lisiwen = 32,
Lisihua = 64 ,}

String S = "yanghaoran ";

If (enum.Isdefined ( Typeof (Myfamily), S) {myfamily F = (Myfamily) Enum. Parse (Typeof (myfamily), S); getmyfamily (f); console. writeline ("the name is:" + enum. Getname ( Typeof (Myfamily), 2); string [] SA = enum. Getnames (Typeof (myfamily ));
Foreach (string SS in SA) {console. writeline (SS );}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.