Enum type enum (for value type) in C #

Source: Internet
Author: User

Original Enum type enum (for value type) in C #

C # supports two special types of values: enumerations and structs.
Declare enumeration: Declare all possible values when declaring.

    Using System;      Using System.Collections.Generic;      Using System.Linq;            Using System.Text; Namespace Enumtype {enum Season//enum type defined outside class {Spring, Summer, Fall, Wi Nter//After the last element is not added;              "} class Program {//enum Season//enum variable definition can also be//{here              Spring, Summer, Fall, Winter//} static void Main (string[] args)                  {Season beauty = Season.fall;                  Season Coldseason = Season.winter;                        Season Currentseason = Season.summer;                                Console.WriteLine ("The beautiful season is {0}.", beauty); When the enumeration variable is displayed with WriteLine, the compiler automatically generates the code, the output and the value of the variable match the string Console.WriteLine ("The beautiful season is {0}.", Being Auty.                     ToString ());             You can also use the ToString method to explicitly convert an enumeration variable to a string representing its current value           Console.WriteLine ("The current season is {0}.", Currentseason);              Console.WriteLine ("{0} is very cold.", Coldseason);   }          }      }

After the run, the results are as follows:

Within the enumeration, each element of it is associated (corresponding) with an integer value. By default, the first corresponds to an integer of 0, and the integer corresponding to each element is incremented by 1. We can get the underlying integer value of an enumeration variable, which must first be converted to the base type.

    Using System;      Using System.Collections.Generic;      Using System.Linq;      Using System.Text;            Namespace Enumtype      {          enum Season     //enum type defined outside class          {              Spring, Summer, Fall, Winter    // The last element does not add the following "; "          }                Class program          {              static void Main (string[] args)              {                  Season Currentseason = Season.summer;                        Console.WriteLine ("Summer is {0}", (int) currentseason); The underlying integer value of the enumeration              }}      }  

After the run, the results are as follows:

You can also associate a specific integer constant with a literal constant of an enumerated type.

    Using System;      Using System.Collections.Generic;      Using System.Linq;      Using System.Text;            Namespace Enumtype      {          enum Season     //defined as short can save space          {              Spring = 168, Summer, Fall, Winter    //Last Element is not added after "; "          }                Class program          {              static void Main (string[] args)              {                  Console.WriteLine (" Spring is {0} ", (int) season.spring);     168                  Console.WriteLine ("Summer is {0}", (int) season.summer);     169 in turn +1                  Console.WriteLine ("Fall is {0}", (int) season.fall);                  Console.WriteLine ("Winter is {0}", (int) season.winter);     171              }}}        

after the run, the results are as follows:

Multiple enumerated literal constants may have the same underlying value, which can be declared as follows.

    Using System;      Using System.Collections.Generic;      Using System.Linq;      Using System.Text;            Namespace Enumtype      {          enum Season     //enum type defined outside class          {              Spring, Summer, Fall, Autumn = Fall, winter< c10/>//after the last element does not add "; "          }                Class program          {              static void Main (string[] args)              {                  Console.WriteLine (" Spring is {0} ", (int) season.spring);     0                  Console.WriteLine ("Summer is {0}", (int) season.summer);     1                  Console.WriteLine ("Fall is {0}", (int) season.fall);         2                  Console.WriteLine ("Autumn is {0}", (int) season.autumn);     2 Base value Same                  Console.WriteLine ("Winter is {0}", (int) season.winter);     3              }}}        

After the run, the results are as follows:

When you declare an enumeration, the literal constants of the enumeration will default to the value of type int. However, you can choose the base type of the enumeration.

Enum type enum (for value type) in C #

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.