C # Enumeration

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;using System.Collections; namespace consoleapplication1{//enum declaration: Access rhetorical character enum enum name: base type public enum Enumname:int//This int can omit {enum into Member}//The underlying type must be able to represent all enumerated values defined in the enumeration. An enumeration declaration can explicitly declare a byte, sbyte, short, ushort, int, uint, long, or ulong type as the corresponding underlying type.    An enumeration declaration that does not explicitly declare an underlying type means that the corresponding underlying type is int. The default underlying type of an enumeration element is int.

By default. The first enumerator has a value of 0, and the value of each subsequent enumerator increments by 1. Enum days {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; ↑//This default value is enum days{0,1,2,3,4,5,6}, if you want to specify a default value of 1, you must enum days{stat=1,sun=2, ...} An enumeration member is a named constant of the enumeration type. Arbitrarily two enumeration members cannot have the same name. Each enumeration member has an associated constant value.

The type of this value is the underlying type of the enumeration.

The constant value of each enumeration member must be within the range of the underlying type of the enumeration. Enum Gender:uint {//Male = -3, Female =-2, unkown =-1//generates a compile-time error. The reason is that the constant value-1,-2, and –3 are not in the range of the base integral uint. }; Defines an enumeration. This enumeration has three values of moning, afternoon, Evening enum TimeOfDay {moning, afternoon, Evening}; The value of the morning value of 0,afternoon is 2 for the value of 1,evening. Class Program {public static string Week (TimeOfDay str)//defines a Week method. It has a reference to an enumeration type (TimeOfDay) str {string result = string. Empty; The default value for switch (str) {//timeofday.moning is 0 case timeofday.moning: result = "Morning"; Break Case TimeOfDay.Afternoon:result = "PM"; Break Case TimeOfDay.Evening:result = "Night"; Break } return result; } static void Main (string[] args) {string s = Program.week (timeofday.moNing); Console.WriteLine (s); Output morning Console.readkey (); } }}


C # Enumeration

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.