C#类型转换的方法

来源:互联网 发布:mac火狐兼容性视图设置 编辑:程序博客网 时间:2024/06/06 05:04

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# 提供了下列内置的类型转换方法:

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