C # Converts an enumeration into a dictionary collection

Source: Internet
Author: User

The use of enumerations in software development

1. An enumeration type (enum type) is a unique value type with a set of named constants.

2. Definition of enumeration:

 Public enum Sex        {            0,            1        }

Or: If only the male is assigned, then the female =1

 Public enum Sex        {            0,            female         }

3. In actual development, the design of the database will often require a lot of state fields (such as gender, audit status, classification status, etc.), and these status fields have only a fixed number of values, this time we will generally need a data dictionary to maintain the data. And what form does the data dictionary exist in?

In my own experience, I generally save these state data in two ways:

3.1. Build a database data dictionary table (key,value,parentkey) to hold the data

Advantages: You can represent a data dictionary with a subordinate relationship, and you can modify the name of the data at the production stage.

Cons: Need to get from the database, slightly worse performance

3.2. Save this data as an enumeration (we can use enumerations to represent a set of states for a field in a database table, such as the value of a sex field in the person table, which we can use as an enumeration)

Pros: When assigning a value, you can assign a value in a strongly typed way instead of a number, such as:

int a = (int) enumhelper.sex. female;

Disadvantage: The production stage cannot modify the name

Reciprocal transfer between enum, int, string three types
int a = (int) Sex. female;             string b = Sex. Female. ToString ();            Sex s= (Sex) enum.parse (typeof" female ");            Sex t= (Sex)1;

Enumeration methods for converting to dictionary collections

1. Here I will directly enumerate the code as follows:

  Public Static classEnumhelper {/// <summary>        ///gets the enumeration name based on the value of the enumeration/// </summary>        /// <typeparam name= "T" >Enum Type</typeparam>        /// <param name= "status" >The value of the enumeration</param>        /// <returns></returns>         Public Static stringGetenumname<t> ( This intstatus) {            returnEnum.getname (typeof(T), status); }        /// <summary>        ///Gets the enumeration name collection/// </summary>        /// <typeparam name= "T" ></typeparam>        /// <returns></returns>         Public Static string[] getnamesarr<t>()        {            returnEnum.getnames (typeof(T)); }        /// <summary>        ///convert an enumeration to a dictionary collection/// </summary>        /// <typeparam name= "T" >Enum Type</typeparam>        /// <returns></returns>         Public Staticdictionary<string,int> getenumdic<t>() {Dictionary<string,int> resultlist =Newdictionary<string,int>(); Type type=typeof(T); varStrlist = getnamesarr<t>().            ToList (); foreach(stringKeyinchstrlist) {                stringval = Enum.format (Type, enum.parse (type, key),"D"); Resultlist.add (Key,int.            Parse (Val)); }            returnresultlist; }           }
 Public enum Sex    {        0,        female    }

Here's how to use it:

Static void Main (string[] args)        {            var name = enumhelper.getenumname<sex> (1 );            Console.WriteLine (name);             var dic = enumhelper.getenumdic<sex>();             int a = (int) Sex. female;        }

C # Converts an enumeration into a dictionary collection

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.