C#类型转换的方法

C# 类型转换方法

类型转换从根本上说是把数据从一种类型转换为另一种类型。在 C# 中,类型转换有两种形式:

  • 隐式类型转换 - 这些转换是 C# 默认的以安全方式进行的转换, 不会导致数据丢失。例如,从小的整数类型转换为大的整数类型,从派生类转换为基类。
  • 显式类型转换 - 显式类型转换,即强制类型转换。显式转换需要强制转换运算符,而且强制转换会造成数据丢失转换类型的范围大小和从属关系和隐式转换相反。显式转换可能会导致数据出错,或者转换失败,甚至无法编译成功。

下面的实例显示了一个隐式的类型转换:
[csharp]  view plain  copy
  1. namespace TypeConvertion  
  2. {   class Base  
  3.     {  
  4.   
  5.     }  
  6.     class Derive : Base //类Derive是类Base的子类  
  7.     {  
  8.   
  9.     }  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             int inum = 100;  
  15.             long lnum = inum; // 进行了隐式转换,将 int 型(数据范围小)数据转换为了 long 型(数据范围大)的数据  
  16.             Base b = new Derive(); // 这里也是隐式转换,将一个新建的 Derive 实例转换为了其基类 Base 类型的实例b  
  17.         }  
  18.     }  
  19. }  

下面的实例显示了一个显式的类型转换:

[csharp]  view plain  copy
  1. namespace TypeConversionApplication  
  2. {  
  3.     class ExplicitConversion  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             double d = 5673.74;  
  8.             int i;  
  9.   
  10.             // 强制转换 double 为 int  
  11.             i = (int)d;  
  12.             Console.WriteLine(i);  
  13.             Console.ReadKey();  
  14.               
  15.         }  
  16.     }  
  17. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 5673  

显示转换的例子2:

[csharp]  view plain  copy
  1. double dnum = 100.1;  
  2. int ifromd = (int)dnum; //double类型显式转换转为int类型  
  3.   
  4. Base b = new Derive();  
  5. Derive d = b as Derive; //使用as进行显式转换  
  6. Console.WriteLine(d is Base);  
  7. Console.WriteLine(d is Derive);  

运行结果:

[csharp]  view plain  copy
  1. FALSE  
  2. FALSE  


C# 类型转换方法

1、C# 提供了下列内置的类型转换方法:

序号 方法 & 描述
1 ToBoolean
如果可能的话,把类型转换为布尔型。
2 ToByte
把类型转换为字节类型。
3 ToChar
如果可能的话,把类型转换为单个 Unicode 字符类型。
4 ToDateTime
把类型(整数或字符串类型)转换为 日期-时间 结构。
5 ToDecimal
把浮点型或整数类型转换为十进制类型。
6 ToDouble
把类型转换为双精度浮点型。
7 ToInt16
把类型转换为 16 位整数类型。
8 ToInt32
把类型转换为 32 位整数类型。
9 ToInt64
把类型转换为 64 位整数类型。
10 ToSbyte
把类型转换为有符号字节类型。
11 ToSingle
把类型转换为小浮点数类型。
12 ToString
把类型转换为字符串类型。
13 ToType
把类型转换为指定类型。
14 ToUInt16
把类型转换为 16 位无符号整数类型。
15 ToUInt32
把类型转换为 32 位无符号整数类型。
16 ToUInt64
把类型转换为 64 位无符号整数类型。

下面的实例把不同值的类型转换为字符串类型:

[csharp]  view plain  copy
  1. namespace TypeConversionApplication  
  2. {  
  3.     class StringConversion  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             int i = 75;  
  8.             float f = 53.005f;  
  9.             double d = 2345.7652;  
  10.             bool b = true;  
  11.   
  12.             Console.WriteLine(i.ToString());  
  13.             Console.WriteLine(f.ToString());  
  14.             Console.WriteLine(d.ToString());  
  15.             Console.WriteLine(b.ToString());  
  16.             Console.ReadKey();  
  17.               
  18.         }  
  19.     }  
  20. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 75  
  2. 53.005  
  3. 2345.7652  
  4. True  


2、C# 提供了Convert和Parse的类型转换方法:

[csharp]  view plain  copy
  1. using System;  
  2.   
  3. namespace TypeConversion  
  4. {  
  5.     class ConversionDemo  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             string locstr = 123.ToString();  
  10.             //如果要将"locstr"转成整型数  
  11.             //方法一: 用 Convert   
  12.             int i = Convert.ToInt16(locstr);  
  13.             Console.WriteLine(i);  
  14.               
  15.             //方法二: 用 Parse  
  16.             int ii = int.Parse(locstr);  
  17.             Console.WriteLine(ii);  
  18.   
  19.             /*int.TryParse(string s,out int i); 
  20.             该方式也是将数字内容的字符串转换为int类型, 
  21.             但是该方式比int.Parse(string s) 好一些,它不会出现异常,最后一个参数result是输出值, 
  22.             如果转换成功则输出相应的值,转换失败则输出0。*/  
  23.   
  24.             string s1 = "abcd";  
  25.             string s2 = "1234";  
  26.             int a, b;  
  27.   
  28.             bool bo1 = int.TryParse(s1, out a);  
  29.             Console.WriteLine(s1 + " " + bo1 + " " + a);  
  30.   
  31.             bool bo2 = int.TryParse(s2, out b);  
  32.             Console.WriteLine(s2 + " " + bo2 + " " + b);  
  33.         }  
  34.     }  
  35. }  
运行结果:

[csharp]  view plain  copy
  1. 123  
  2. 123  
  3. abcd False 0  
  4. 1234 True 1234  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值