• 枚举类型转换


    枚举类型转换成int类型
    例:

    1 public enum Gender
    2 {
    3 男,
    4 5 }
    6 
    7 Gender gender = Gender.男; 
    8 int a = gender;
    9 Console.WriteLine(a);

    输入结果:
    0

    还有一个功能:

    1 public enum Gender
    2 {
    3 男=5,
    4 5 }
    6 
    7 Gender gender = Gender.男; 
    8 int a = gender;
    9 Console.WriteLine(a);

    输入结果:
    5

    1 Gender gender = Gender.女; 
    2 int a = gender;
    3 Console.WriteLine(a);

    输出结果:
    6

    int类型转换成枚举类型

     1 public enum Gender
     2 {
     3 男,
     4  5 }
     6 
     7 int a = 0;
     8 Gender gender;
     9 gender = (Gender)a;
    10 Console.WriteLine(gender);

    输入结果:

    枚举类型转换成String类型

    1 public enum Gender
    2 {
    3 男,
    4 5 }
    6 Gender gender = Gender.男;
    7 String st = gender.ToString();
    8 
    9 Console.WriteLine(st);

    输出结果:

    String类型转换成枚举类型
    注意:无法强制类型转换

    1 public enum Gender
    2 {
    3 男,
    4 5 }
    6 Gender gender = Gender.男;
    7 String st = gender.ToString();
    8 
    9 Console.WriteLine(st);

    强制类型转换是错误的
    正确的:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     public enum Gender
    10     {
    11         男,
    12 13     }
    14     class Program
    15     {
    16         static void Main(string[] args)
    17         {
    18             string s = "0";
    19             Gender gender = (Gender)Enum.Parse(typeof(Gender), s);
    20             /*public static Object Parse(
    21                                             Type enumType,
    22                                             string value
    23                                         )
    24               将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。
    25              typeof():获取类型
    26              */
    27             Console.WriteLine(gender);
    28             Console.ReadKey();
    29         }
    30     }
    31 }
    代码

    输出结果:

    请按任意键继续. . .
    遇到无法转换的字符串时应付抛异常。
    例:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     public enum Gender
    10     {
    11         男,
    12 13     }
    14     class Program
    15     {
    16         static void Main(string[] args)
    17         {
    18             string s = "abc";
    19             Gender gender = (Gender)Enum.Parse(typeof(Gender), s);
    20             /*
    21              *该处出错,无法转换
    22              */
    23             Console.WriteLine(gender);
    24             Console.ReadKey();
    25         }
    26     }
    27 }
    代码

    字符串与枚举元素相同也是可以转的

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9     public enum Gender
    10     {
    11         男,
    12 13     }
    14     class Program
    15     {
    16         static void Main(string[] args)
    17         {
    18             string s = "";
    19             //字符串与枚举的元素完全相同也是可以转的
    20             Gender gender = (Gender)Enum.Parse(typeof(Gender), s);
    21             /*public static Object Parse(
    22                                             Type enumType,
    23                                             string value
    24                                         )
    25               将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。
    26              typeof():获取类型
    27              */
    28             Console.WriteLine(gender);
    29             Console.ReadKey();
    30         }
    31     }
    32 }
    代码
  • 相关阅读:
    Codeforces Round #632 (Div. 2)
    Codeforces Round #630 (Div. 2)
    多项式全家桶
    Educational Codeforces Round 84 (Rated for Div. 2)
    【cf1186E】E. Vus the Cossack and a Field(找规律+递归)
    [CF847B] Preparing for Merge Sort
    [CF858D] Polycarp's phone book
    [CF911D] Inversion Counting
    [CF938C] Constructing Tests
    [CF960C] Subsequence Counting
  • 原文地址:https://www.cnblogs.com/2016Study/p/5472516.html
Copyright © 2020-2023  润新知