C#中Enum(枚举)与Int、String类型之间的转换

前言:
在面对对象的动态开发中,难免会遇到各种类型转换的问题,今天我们来简单介绍一下enum(枚举)与int(整型)和string(字符串)类型之间的相互转换;

一.三者类型的简单介绍:

1.enum:枚举的声明用enum,枚举是值类型;详细介绍请前往:enum枚举的介绍
2.int:int是32位有符号的整型;

int i =100 ;
Console.WriteLine(i);

在这里插入图片描述
3.string:是字符串类型,双引号中随便写,唯独写符号需要小心;

string i = "第一个字符串156456456=43567dufcdj";
Console.WriteLine(i);

在这里插入图片描述

二.enum枚举转换为字符串类型:

在转化为字符串类型时主要用到enum_name.枚举值.Tostring(),也就是将当前对象转换为字符串样式;
举例:

       enum Sss
        {
            吃饭,
            睡觉,
            打豆豆,
            打游戏,
            看电影,
            三国杀
        }
         static void Main(string[] args)
        {
            Console.WriteLine("这是将枚举转换为字符串:    {0}", Sss.打游戏.ToString());
            Console.ReadLine();
            Console.WriteLine("这是将枚举转换为字符串:    {0}", Sss.三国杀.ToString());
            Console.ReadLine();
            Console.WriteLine("这是将枚举转换为字符串:    {0}", Sss.吃饭.ToString());
            Console.ReadLine();
            Console.WriteLine("这是将枚举转换为字符串:    {0}", Sss.打豆豆 .ToString());
            Console.ReadLine();
         }

运行结果:
在这里插入图片描述

三.enum枚举转化为整型:

在转换为整形时一般用的是强制转化,会用到(int)enum_name.枚举值,也就是将当前对象转化为整型;
举例:

enum Sss
        {
            吃饭,
            睡觉,
            打豆豆,
            打游戏,
            看电影,
            三国杀
        }
 static void Main(string[] args)
        {
            Console.WriteLine("这是将枚举转换为整型:    {0}", (int)Sss.吃饭);
            Console.ReadLine();
            Console.WriteLine("这是将枚举转换为整型:    {0}", (int)Sss.三国杀);
            Console.ReadLine();
            Console.WriteLine("这是将枚举转换为整型:    {0}", (int)Sss.打游戏);
            Console.ReadLine();
            Console.WriteLine("这是将枚举转换为整型:    {0}", (int)Sss.打豆豆);
            Console.ReadLine();
            Console.WriteLine("这是将枚举转换为整型:    {0}", (int)Sss.看电影);
            Console.ReadLine();
         }

运行结果:
在这里插入图片描述
如果好奇怎么回得到这样的结果,请细阅:enum枚举详解
如果好奇怎么不用int.Parse(),那是由于这个方法是将其数字的字符串形式转化为与其等效的32位有符号整数,如果引用这种会报错,有两种报错形式,一种在编写时报错,一种在运行时报错;
一.编写时报错:
在这里插入图片描述
二.编写时正确,运行时报错;
在这里插入图片描述
所以一般推荐用(int)enum_name.枚举值这种书写!

四.将整型转化为enum枚举:

1.第一种方法

在将整型转化为枚举时主要用到了:(enum_name)enum符号的值这个方法;

 enum Sss
        {
            吃饭,
            睡觉,
            打豆豆,
            打游戏,
            看电影,
            三国杀
        }
        static void Main(string[] args)
        {
            Console.WriteLine("这是将整数转换为枚举:     {0}", (Sss)1);
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", (Sss)2);
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", (Sss)3);
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", (Sss)4);
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", (Sss)5);
            Console.ReadLine();
        }

代码中的1,2,3,4,5为枚举符号值,所以运行结果为:
在这里插入图片描述

2.第二种方法:

在这种方法中主要用到了:Enum.GetName(typeof(enum_name), enum符号值)这个方法,Enum.GetName()的意思就是:在制定的枚举中检索具有指定值的常熟的名称,括号里写的是枚举的名称以及枚举的符号值;
举例;

enum Sss
        {
            吃饭,
            睡觉,
            打豆豆,
            打游戏,
            看电影,
            三国杀
        }
        static void Main(string[] args)
        {
            Console.WriteLine("这是将整数转换为枚举:     {0}", Enum.GetName(typeof(Sss), 1));
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", Enum.GetName(typeof(Sss), 2));
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", Enum.GetName(typeof(Sss), 3));
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", Enum.GetName(typeof(Sss), 4));
            Console.ReadLine();
            Console.WriteLine("这是将整数转换为枚举:     {0}", Enum.GetName(typeof(Sss), 5));
            Console.ReadLine();
         }

和第一种方法一样,所以结果不想而知了吧:
在这里插入图片描述
但个人一般推荐第一种因为比较简单,哈哈哈

五.将字符串转化为enum枚举:

在其进行转化是首先肯定得定义一个string类型的量,然后通过(enum_name)Enum.Parse(typeof(enum_name), string_name)方法进行设置调用;
举例:

 enum Sss
        {
            cc,
            dd,
            bb,
            打游戏,
            看电影,
            三国杀
        }
         static void Main(string[] args)
        {
string str = "11223344";
            //Sss sss = (Sss)Enum.Parse(typeof(Sss),str);
            Console.WriteLine("这是将字符串转换为枚举:     {0}", (Sss)Enum.Parse(typeof(Sss), str));
            Console.ReadLine();
        }

运行结果:
在这里插入图片描述


以上为本次博客全部内容,下次更新如何查看枚举具体转换的是什么类型,敬请期待!

好的,以下是C#与SQL Server的类型映射枚举类的代码: ``` public enum SqlType { BigInt, Binary, Bit, Char, Date, DateTime, DateTime2, DateTimeOffset, Decimal, Float, Geography, Geometry, HierarchyId, Image, Int, Money, NChar, NText, Numeric, NVarChar, Real, SmallDateTime, SmallInt, SmallMoney, SqlVariant, SysName, Text, Time, Timestamp, TinyInt, UniqueIdentifier, VarBinary, VarChar, Xml } public static class SqlTypeMapper { public static string Map(SqlType type) { switch (type) { case SqlType.BigInt: return "bigint"; case SqlType.Binary: return "binary"; case SqlType.Bit: return "bit"; case SqlType.Char: return "char"; case SqlType.Date: return "date"; case SqlType.DateTime: return "datetime"; case SqlType.DateTime2: return "datetime2"; case SqlType.DateTimeOffset: return "datetimeoffset"; case SqlType.Decimal: return "decimal"; case SqlType.Float: return "float"; case SqlType.Geography: return "geography"; case SqlType.Geometry: return "geometry"; case SqlType.HierarchyId: return "hierarchyid"; case SqlType.Image: return "image"; case SqlType.Int: return "int"; case SqlType.Money: return "money"; case SqlType.NChar: return "nchar"; case SqlType.NText: return "ntext"; case SqlType.Numeric: return "numeric"; case SqlType.NVarChar: return "nvarchar"; case SqlType.Real: return "real"; case SqlType.SmallDateTime: return "smalldatetime"; case SqlType.SmallInt: return "smallint"; case SqlType.SmallMoney: return "smallmoney"; case SqlType.SqlVariant: return "sql_variant"; case SqlType.SysName: return "sysname"; case SqlType.Text: return "text"; case SqlType.Time: return "time"; case SqlType.Timestamp: return "timestamp"; case SqlType.TinyInt: return "tinyint"; case SqlType.UniqueIdentifier: return "uniqueidentifier"; case SqlType.VarBinary: return "varbinary"; case SqlType.VarChar: return "varchar"; case SqlType.Xml: return "xml"; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } } } ``` 使用时,可以通过 `SqlTypeMapper.Map(SqlType)` 方法来获取对应的SQL Server数据类型。例如: ``` var sqlType = SqlTypeMapper.Map(SqlType.VarChar); ``` 这样就可以得到 `varchar` 这个数据类型了。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木偶☜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值