enum-type enum usage in C #

Source: Internet
Author: User

To define a simple enumeration type:
enum        Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; At this time Days.sat = 0, followed by this increment.

Of course, you can also specify the starting value manually:
enum Days {sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; That's starting with 1.

The implied type of the enum type is int, such as int x= (int) Days.sat; Not a bit of a problem.        According to MSDN, the implied type of an enumeration type can be any number type other than char ... [Every enumeration type has a underlying type, which can be any integral type except char.]
The following routines demonstrate the use of long integers as an implicit type of enumeration:

//Keyword_enum2.cs
Using Long Enumerators
usingSystem;
Public classEnumtest
{
enum range:long {Max = 2147483648L, Min = 255L};
static voidMain ()
{
Longx = (Long)Range.max;
Longy = (Long)range.min;
Console.WriteLine("Max = {0}", x);
Console.WriteLine("Min = {0}", y);
}
}
If you need to retrieve the long-shaped value again, it is a conversion.Longx = (Long)Range.max;

The most interesting thing is to say the following, the [flags] tag of the enum. Don't say anything, look at the program:
//the Following code example illustrates the use and effect of the
System.FlagsAttribute attribute on an enum declaration.

//EnumFlags.cs
//Using the FlagsAttribute on enumerations.
usingSystem;

[flags ]
public enum   FileAttribute
{
         readonly= 0x01,
          hide= 0x02,
         system= 0x04,
         archived= 0x08
}

CLASS  flagtest
{
    static void  main ()
         {
         fileattributeoptions  options= < Span style= "color: #333399;" >fileattribute . Readonly| fileattribute . System;
        console . writeline (options);
        console . writeline ((int ) options);
        }
}

The output is:

ReadOnly, System
5
Did you see that? Oh. This is a common sign-in C # has become more easy to use.

There are enum days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

1> I give a value of 1, how to return the corresponding sun as a string? (used when taking xxx_id in a database and converting it to a corresponding value.) --but a little hard-coded feeling. )
Answer: Convert.changetype (Enumvalue, enumtype). ToString ();//enumvalue=1; Enumtype=typeof (days)

2> I given a string "Sun", how do I return to the enum Day.sun?
Answer: You can use Enum.parse (Enumtype, String,[boolean]) to solve the problem directly. For example, Enum.parse (day), "Sun", True) returns Day.sun, and the 3rd parameter specifies whether the case is sensitive or not. can be omitted.

3> I want to know all the string values in Enum day. How to write?
A: This looks very simple, too, foreach (string name in Enum.getnames (typeof)) Console.WriteLine (name); There is also a enum.getname (), specific usage see MSDN go ....

Source: http://blog.163.com/li_crane/blog/static/19502097200822210217451/

enum-type enum usage 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.