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:
![](https://upload-images.jianshu.io/upload_images/13808716-e455437fb0b69966.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/13808716-3e01ed525ee3a0f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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
![](https://upload-images.jianshu.io/upload_images/13808716-67dac1e10d555e5f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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 ?
![](https://upload-images.jianshu.io/upload_images/13808716-eac3ce6f329ddd48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
This time we thought of adding Attribute
Respectively, plus the head of the enum member [the Description] shown below:
![](https://upload-images.jianshu.io/upload_images/13808716-e56998e11602c66e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Do not forget to introduce namespaces:
![](https://upload-images.jianshu.io/upload_images/13808716-bf82e4da487d6b8c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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)
![](https://upload-images.jianshu.io/upload_images/13808716-afa6298b8bd2170b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/13808716-8350afccdf64c918.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/13808716-3ab55cd9add831b1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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:
![](https://upload-images.jianshu.io/upload_images/13808716-da9f44a55dee17b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Next, we went to test the console:
![](https://upload-images.jianshu.io/upload_images/13808716-255d74881ee3bd4f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/13808716-fe429ec6bedcb2dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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