Enumerate how many applications you know?

Hello everyone, I was the culmination son, today to talk about the application enumeration.

Foreword

Enumeration enum type in C # is the basic data type value types, proper good use of it, it helps to improve the simplicity and legibility.

Enumeration grammar definition format: enum {Enumeration enum name element 1, element 2 enumeration, ......};

application

In normal development, like a person's gender, status data, education level of people, music player's play mode and so on scenarios, often stored in the database is the corresponding code, such as gender, male 0 --- 1 --- female 2 --- unknown, we have to enumerate in the code behind:


Sex enumeration

Enumeration data state

The corresponding conversion

In practice, stored in the database field is generally varchar, is the string to the back, so are the mutual conversion between string and enumeration.

string => Enumeration


 

In fact I am here first string converted int converted to an enumeration value

But when characters exist in our database in the background and can not be converted to int type, how to do it, such as to save the time of year nodes years --Mid, at the end of --End, is it also defined such enumeration ?


This display does not work

This time we thought of adding Attribute

Respectively, plus the head of the enum member [the Description] shown below:


Year enumeration nodes

Do not forget to introduce namespaces:


The introduction of namespaces

Another expand from a class: NOTE: The class must be a static class method must be a static method, the keyword this (there are methods to explain the expansion of learning in my another article)


static class

static method

 


Implementation

Xiao Bian concrete realization of the code is not go into detail, there do not understand can then contact Xiao Bian, do not forget the need to introduce namespace:


The introduction of namespaces

Next, we went to test the console:


 

Test Results

Obviously, there is nothing wrong. Well, today's the end of the study on here slightly, we next goodbye!

Micro-channel public number: School net with Dacheng child

Website: https://www.dachengzi.co/

Know almost: https://www.zhihu.com/people/tu-dou-hen-dou-49/posts

Guess you like

Origin www.cnblogs.com/xingxingzhou/p/11575515.html