Enum type conversions

Source: Internet
Author: User

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

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.