LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 不同数据类型之间的转换

admin
2025年12月20日 22:0 本文热度 698
序言

由于 C# 是在编译时静态类型化的,因此变量在声明后就无法再次声明,或无法分配另一种类型的值,除非该类型可以隐式转换为变量的类型。 例如,string 无法隐式转换为 int。 因此,在将 i 声明为 int 后,无法将字符串“Hello”分配给它,因此很多地方都要用到数据转换。
在C#中,类型转换是一种将一个数据类型的值转换成另一种数据类型的值的过程。类型转换可以分为几种不同的类型:
1‌.隐式转换(Implicit Conversion)‌:
当从一种数据类型到另一种数据类型的转换是安全的,并且不需要显式地告诉编译器进行转换时,这种转换称为隐式转换。例如,将一个较小的整型(如byte)赋值给一个较大的整型(如int)变量。
byte b = 255;int i = b; // 隐式转换

隐式数值转换包括以下几种:


a.从 sbyte 类型到 short,int,long,float,double,或 decimal 类型。


b.从 byte 类型到 short,ushort,int,uint,long,ulong,float,double,或 decimal 类 型。


c.从 short 类型到 int,long,float,double,或 decimal 类型。


d.从 ushort 类型到 int,uint,long,ulong,float,double,或 decimal 类型。


e.从 int 类型到 long,float,double,或 decimal 类型。


f.从 uint 类型到 long,ulong,float,double,或 decimal 类型。


g.从 long 类型到 float,double,或 decimal 类型。


h.从 ulong 类型到 float,double,或 decimal 类型。


i.从char 类型到 ushort,int,uint,long,ulong,float,double,或 decimal 类型。


j.从 float 类型到 double 类型。


2.显式转换(Explicit Conversion)‌:

当需要显式地将一个数据类型的值转换成另一种数据类型时,就需要使用显式转换。这通常是通过使用类型转换操作符(Type)来实现的。例如,将一个较大的整型(如int)转换为较小的整型(如byte),可能会丢失数据。
int i = 256;byte b = (byte)i; // 显式转换,可能会丢失数据
3.装箱和拆箱(Boxing and Unboxing)‌:
装箱是将值类型转换为引用类型的过程。这通常涉及到将值类型封装在System.Object类或其派生类的实例中。拆箱是将引用类型转换回其原始值类型的过程。
int i = 100;object obj = i; // 装箱int j = (int)obj; // 拆箱
‌4.使用Convert类进行转换‌:
Convert类提供了一系列静态方法来执行各种类型的转换,包括基本数据类型之间的转换。这些方法通常用于需要更安全或更明确的类型转换时。
double d = 123.45;int i = Convert.ToInt32(d); // 使用Convert类进行显式转换
5.使用try-parse方法进行转换‌:
对于字符串到数值类型的转换,可以使用TryParse方法,这比直接使用Convert.ToInt32等方法更安全,因为它在无法解析字符串为指定类型时不会抛出异常。
string s = "123";int.TryParse(s, out int result))
每种类型的转换都有其适用场景和潜在的风险。例如,显式转换时可能会丢失精度或数据范围,而装箱和拆箱会增加内存占用并可能影响性能。因此,在进行类型转换时,应该根据具体情况选择最合适的方法。
6.自定义数据转换类‌:
public static class DataConvertHelper{    [Description("将Int类型数值转换成Bool数组")]    public static bool[] IntToBool(int InputNum)     {      bool[] res = new bool[32];      int temp;      for (int i = 0; i < res.Length; i++)      {        temp = 0;        temp = 1 << i;        if (temp == (temp & InputNum))        {            res[i] = true;        }        else         {            res[i] = true;        }      }      return res;    }    [Description("将Bool数组转换成Int类型数值")]    public static int BoolToInt(bool[] b)    {      int res = 0;      if (b.Length >= 32)      {        return -1;      }      else      {        for (int i = 0; i < b.Length; i++)        {            if (b[i])            {                res += Convert.ToInt32(Math.Pow(Convert.ToDouble(2), Convert.ToDouble(i)));            }        }      }
      return res;    }  [Description("将Byte数组转换成String类型")]  public static string ByteArrToString(byte[] b_arr)  {    string str_res = Encoding.UTF8.GetString(b_arr);    return str_res;  }  public static byte[] StringToByteArr(string str)  {    byte[] res;    res = Encoding.ASCII.GetBytes(str);    return res;  }    //加载NPinyin.Core
  [Description("将拼音转换成汉字")]  public static string EngToChi(string str)  {    string res;    res = Pinyin.GetChineseText(str);    return res;  }  [Description("将汉字转换成拼音")]  public static string ChiToEng(string str)  {    string res;    res = Pinyin.GetPinyin(str);    return res;  }  [Description("将字节数组转换成字符串")]  public static string GetStringFromByteArrayByBitConvert(byte[] valueint start, int count)  {    return BitConverter.ToString(value, start, count);  }  [Description("将Int类型数值转换成字节数组")]  public static byte[] GetByteArrayFromInt(int value)  {    byte[] res = BitConverter.GetBytes(value);    return res;  }      //
  //    }

总结:

在C# 中经常会用到数据类型转换。本文介绍了常见的数据类型转换,通过掌握这些转换技巧,我们可以更灵活地处理不同数据类型之间的转换,提高代码的可读性


阅读原文:原文链接


该文章在 2025/12/22 18:48:31 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved