Enum type converted to int type
Cases:
1 Public enum Gender 2 {3 male,4 female 5} 6 7 Gender Gender =8int a = Gender; 9 Console.WriteLine (a);
Input Result:
0
There is also a feature:
1 Public enum Gender 2 {3 male =5,4 female 5}67 Gender Gender =8int a = Gender; 9 Console.WriteLine (a);
Input Result:
5
1 Gender Gender =2int a = Gender; 3 Console.WriteLine (a);
Output Result:
6
int type converted to enum type
1 Public enum Gender 2 { 3 male, 4 female 5} 6 7 int 0 ; 8 Gender Gender; 9 gender = (gender) A; Ten Console.WriteLine (gender);
Input Result:
Man
Enum type converted to String type
1 Public enum Gender 2 {3 male,4 female 5} 6 Gender Gender = Gender. male; 7 String st = Gender. ToString (); 8 9 Console.WriteLine (ST);
Output Result:
Man
String type conversion to enum type
Note: Type conversions cannot be enforced
1 Public enum Gender 2 {3 male,4 female 5} 6 Gender Gender = Gender. male; 7 String st = Gender. ToString (); 8 9 Console.WriteLine (ST);
Coercion of type conversion is wrong
Correct:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceConsoleApplication18 {9 Public enumGenderTen { One Male, A female - } - class Program the { - Static voidMain (string[] args) - { - strings ="0"; +Gender Gender = (Gender) enum.parse (typeof(Gender), s); - /*Public static Object Parse ( + Type Enumtype, A String Value at ) - converts the string representation of the name or numeric value of one or more enumeration constants to an equivalent enumeration object. - typeof (): Get type - */ - Console.WriteLine (gender); - Console.readkey (); in } - } to}
Code
Output Result:
Man
Please press any key to continue ...
Handle thrown exception when encountering a string that cannot be converted.
Cases:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceConsoleApplication18 {9 Public enumGenderTen { One Male, A female - } - class Program the { - Static voidMain (string[] args) - { - strings ="ABC"; +Gender Gender = (Gender) enum.parse (typeof(Gender), s); - /* + * Error in this place, unable to convert A */ at Console.WriteLine (gender); - Console.readkey (); - } - } -}
Code
The string is the same as the enumeration element and can be transferred.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceConsoleApplication18 {9 Public enumGenderTen { One Male, A female - } - class Program the { - Static voidMain (string[] args) - { - strings ="male"; + //The string is exactly the same as the enumerated element and can be transferred. -Gender Gender = (Gender) enum.parse (typeof(Gender), s); + /*Public static Object Parse ( A Type Enumtype, at String Value - ) - converts the string representation of the name or numeric value of one or more enumeration constants to an equivalent enumeration object. - typeof (): Get type - */ - Console.WriteLine (gender); in Console.readkey (); - } to } +}
Code
Enum type conversions