C # enum usage enumeration usage Asp.net ENUM usage

Source: Internet
Author: User

Original article: c # enum usage enumeration usage Asp.net ENUM usage

The enum keyword is used to declare enumeration, which is a unique type consisting of a group of named constants called the enumeration list. Each Enumeration type has a base type, which can be any integer except char. That is:

(Byte, sbyte, short, ushort, int, uint, long, And ulong)

The concept of Emumeration was available in the C era, but it has never been used before.

Basic expression, changing the default value and default type

The default value of Enumeration is int starting from 0, as follows:
Enum Direction
{
UP,
RIGHT,
DOWN,
LEFT,
};
In this case, UP = 0, RIGHT = 1...

Change default value:
Enum Direction
{
UP = 1,
RIGHT = 2,
DOWN = 3,
LEFT = 4,
};

Change the type (only byte, sbyte, short, ushort, int, uint, long, ulong can be changed ):
Enum Direction: long
{
UP = 1111111111,
Go DOWN = 1111111112,
LEFT = 1, 1111111113,
RIGHT = 1111111114.
};

Value of the Enumeration variable

Cast (forced type conversion) before assigning values ):
Long direct = (long) Direction. UP;

Description of the Enumeration variable

If you want Enumeration to return a meaningful string so that you can know what it represents, you can define it as follows:
Using System. ComponentModel; // Add the reference first
Enum Direction
{
[Description ("this means facing to UP (Negtive Y)")]
UP = 1,
[Description ("this means facing to RIGHT (Positive X)")]
RIGHT = 2,
[Description ("this means facing to DOWN (Positive Y)")]
DOWN = 3,
[Description ("this means facing to LEFT (Negtive X)")]
LEFT = 4
};

Use the following method to obtain the text description:
Using System. Reflection;
Using System. ComponentModel;
Public static String GetEnumDesc (Direction e)
{
FieldInfo EnumInfo = e. GetType (). GetField (e. ToString ());
DescriptionAttribute [] EnumAttributes = (DescriptionAttribute []) EnumInfo.
GetCustomAttributes (typeof (DescriptionAttribute), false );
If (EnumAttributes. Length> 0)
{
Return EnumAttributes [0]. Description;
}
Return e. ToString ();
}

Alternatively, you can customize Discription Attributes.
Enum Direction
{
[EnumDescription ("Rover is facing to UP (Negtive Y)")]
UP = 1,
[EnumDescription ("Rover is facing to DOWN (Positive Y)")]
DOWN = 2,
[EnumDescription ("Rover is facing to RIGHT (Positive X)")]
RIGHT = 3,
[EnumDescription ("Rover is facing to LEFT (Negtive X)")]
LEFT = 4
};

AttributeUsage (AttributeTargets. Field)]

Public class EnumDescriptionAttribute: Attribute
{
Private string _ text = "";
Public string Text
{
Get {return this. _ text ;}
}
Public EnumDescriptionAttribute (string text)
{
_ Text = text;
}

}

Although now ,. in net, the ToString operation on enum will output the enum name. However, in many cases, we want to display it in different languages, or, the string we want to display contains invalid characters (such as spaces, which is a common requirement for explicit display, it helps us to display the string defined by enum, but unfortunately, although any enum is from System. enum is derived. However, we cannot rewrite its ToString function. Therefore, we need a Helper class to help its output:

Public class EnumStringHelper
{
Public static string ToString (object o)
{
Type t = o. GetType ();
String s = o. ToString ();
EnumDescriptionAttribute [] OS = (EnumDescriptionAttribute []) t. GetField (s). GetCustomAttributes (typeof (EnumDescriptionAttribute), false );
If (OS! = Null & OS. Length = 1)
{
Return OS [0]. Text;
}
Return s;
}
}

Usage, like this:
Static void Main (string [] args)
{
Direction myDirection = Direction. DOWN;
String s = EnumStringHelper. ToString (myDirection );
Console. WriteLine ("{0}", s );
Console. ReadKey ();
}

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.