Definition and use of enumeration types in C #

Source: Internet
Author: User
Introduction
Enumeration is a specified constant. Its basic type can be divisionCharAny integer outside.
If the basic type is not explicitly declared, useInt32.
Programming Languages generally provide syntax to declare an enumeration consisting of a group of named constants and their values.
Definition
The default base number starts from O and can also be specified.
Enum  Days {Saturday = 1 ,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday} ;
Enum  Colors {Red =   1 ,Green =   2 ,Blue =   4 ,Yellow =   8  } ;

Use
ColorsMycolors =  Colors. Red;
String  Strcolor = Mycolors. tostring ();
Int     Intcolor = ( Int ) Mycolors;
Bit or
ColorsMycolors =  Colors. Red |  Colors. Blue |  Colors. Yellow;
Bitwise AND
ColorsMycolors =  Colors. Red &  Colors. Blue &  Colors. Yellow;
Traversal
Foreach  ( String  S In  Enum. getnames ( Typeof (Days )))
  Response. Write (S +   " -- "   +  Enum. parse ( Typeof (Days ),S). tostring ());
Conversion
ColorsMC = ColorsEnum. parse ( Typeof (Colors), " Red " );
  If  (System. enum. isdefined ( Typeof (Days ), " Monday " ))
   DaysDS =  (Days) enum. parse ( Typeof (Days ), " Monday " );


Example 2:
     Public   Enum  Noticetype
     {
        Notice =   ' A ' ,
        Labrule =   ' H ' ,
        Hotinformation =   ' N ' ,
        Column =   ' C ' ,
        All =   ' 1 ' ,
        Null =   ' 0 '
   }
      // Create Enumeration type
        NoticetypeNoticetype1 =  Noticetype. column;

         // Convert the enumerated type to stringD = "column"
         String  D =  Noticetype1.tostring ();

         // Obtains the base number of the enumeration type.Dd = 'C'
         Char  Dd =  ( Char ) Noticetype1;

         // Obtain the corresponding Enumeration type through the base numberNoticetype2=Noticetype. Notice
         // (Noticetype) 'A '; Both methods are supported.
        NoticetypeNoticetype2 =  (Noticetype) Char. parse ( " A " );

     // Retrieve Enumeration type by nameNoticetype3=Noticetype. Notice
        NoticetypeNoticetype3 =  (Noticetype) enum. parse ( Typeof (Noticetype ), " Notice " );

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.