C#学习日记17---显示类型转换具体用例

黄舟
黄舟原创
2017-01-21 15:07:061001浏览

在C#的类型转换中,除了上一篇中介绍到的 隐式类型转换 外还有一种需要我们声明的类型转换-----显示类型转换.

显示类型转换,又叫强制类型转换,在进行转换的时候它需要我们明确的指定转换类型. 比如,当我们把long类型转换为int类型时,由于这种转换是丢失精度的转换,系统不会自动进行隐式转换,所以需要进行强制转换:

      long l = 6000;
                  int i = (int)l;    //需要用在 ()里面声明转换类型

显示类型转换并非是对任意2种类型都成立,比如:

       int i = 6000;
                  string i = (string)i;    //这里会报错

因此显示类型转换也是有一定规则的:

  • 显示数值转换;

  • 显示枚举转换;

  • 显示引用转换;

显示转换并不是总能成功,而且常常可能引起信息的丢失(因为类型不同,范围、精度也是不同的 详情参照数据类型),显示转换包括所有的隐式转换,因此也可以把隐式转换写成显示转换的形式,比如:

        int i = 6000;
                            long l = (long)i;    //等价于 long l = i;

显示数值转换:

显示数值转换,是指值类型与值类型之间的转换,有如下的规则:

  • 从 sbyte 到 byte、ushort、uint、ulong、char类型;

  • 从 byte 到 sbyte、char类型;

  • 从 short 到 sbyte、byte、ushort、uint、ulong、char类型;

  • 从 ushort 到 sbyte、byte、short 、char类型;

  • 从 int 到 sbyte、byte、short、ushort、uint、ulong、char类型;

  • 从 uint 到 sbyte、byte、short、ushort、int、char类型;

  • 从 long 到 sbyte、byte、short、ushort、int、uint、ulong、char类型;

  • 从 ulong 到 sbyte、byte、short、ushort、int、uint、long、char类型;

  • 从 char 到 sbyte、byte、short类型;

  • 从 float 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、decimal类型;

  • 从 double 到 sbyte、byte、short、ushort、int、uint、long、ulong、float、char、decimal类型;

  • 从 decimal 到 sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double类型;

写了这么多总结下吧,就是从高精度到低精度的转换,有可能是保留转换也有可能是四舍五入转换,写个例子:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        static void Main(string[] args)  
        {  
            double n_double = 1.73456789;  
            float n_float = (float)n_double;  //显示转换 float的有效为只有8位(.也是一位)所以从第9位四舍五入  
  
            int n_int = (int)n_double; //只保留整数  
  
            Console.WriteLine("n_float = {0}\nn_int = {1}",n_float,n_int);  
              
        }  
    }  
}

运行结果:


640.png

对比发现当double 数据范围超出float的有效值范围,显示转换时对第9位四舍五入,转换为int类型时只保留整数部分。

显示枚举转换:

显示枚举转换包括下面几个内容:

  • 从sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double、decimal类型到任何 枚举 类型;

  • 从任何枚举类型 到 sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double、decimal类型;

  • 从任何枚举类型 到 任何其他枚举类型;

写个列子:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        enum weekday   //定义2个枚举  
        {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday }  
        enum Month  
        {Janurary=1,February,March,April,May,Jun,July }  
        static void Main(string[] args)  
        {  
            int n_int = 2;  
            double n_double = 3.0;  
            decimal n_decimal = 5m;  //声明decimal 类型要加m  
  
            weekday weki = (weekday)n_int;     //从int、double、decimal到枚举转换  
            weekday wekd = (weekday)n_double;  
            weekday wekde = (weekday)n_decimal;  
  
            weekday wek = weekday.Tuesday;   //枚举类型之间的转换  
            Month mon = (Month)wek;  
  
            int i = (int)wek;  //从枚举类型到int的转换  
            int t = (int)mon;  
            Console.WriteLine("n_int = {0}\nn_double = {1}\nn_decimal = {2}",weki,wekd,wekde);  
            Console.WriteLine("wek = {0}\nmon = {1}\nwek ={2}\tmon = {3}",wek,mon,i,t);  
              
        }  
    }  
}

运行结果:

641.png

显示引用转换:

从对象到任何引用类型的转换;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        //定义2个类 teacher与man  
        class teacher  
        { }  
        class man  
        { }  
        static void Main(string[] args)  
        {  
            man per = new man();  //将man实例化一个对象per  
            object o = per;      //装箱  
            teacher p = (teacher)o;  // 将o显示转换为teacher类  
              
        }  
    }  
}

从类类型s到类类型t的转换,其中s是t的基类;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        class man   //定义一个基类  
        { }  
        class student:man  //student继承man  
        { }  
        static void Main(string[] args)  
        {  
            man per = new man();  //man实例化一个对象per  
            student stu = (student)per;  //将父类转换为子类  
              
        }  
    }  
}

从类类型s到接口t的转换,其中s不是密封类,并没有实现t;(有关接口(interface)的内容后面会写到,它只声明方法不定义方法)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{  
    class Program
    {
        public interface teacher  //定义一个接口 
        { }
        class student   //定义一个类
        { }
        static void Main(string[] args)
        {
            student stu = new student(); //实例化一个对象
            teacher tea = (teacher)stu;  // 显示转换
                        
        }
    }
}

从接口型s到类类型t的转换,其中t不是密封类,并没有实现s;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        public interface man  //定义一个接口   
        { }  
        class teacher:man  //定义一个继承于man的类man  
        { }  
        class student   //定义一个新类  
        { }  
        static void Main(string[] args)  
        {  
            man teac=new teacher(); //间接实例化一个接口  
            student stu = (student)teac;  // 显示转换  
                          
        }  
    }  
}

从接口类型s到接口类型t的转换,其中s不是t的子接口;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        public interface man  //定义一个接口   
        { }  
        class teacher : man    //由接口派生一个类  
        { }  
        public interface person //定义一个接口  
        { }  
        class student:person   //由接口派生一个类  
        { }  
        static void Main(string[] args)  
        {  
            man teac=new teacher(); //间接实例化一个接口  
            person stu = (person)teac;  // 显示转换  
                          
        }  
    }  
}

引用类型数组与引用类型数组显示转换,其中两者是父类与子类的关系(维数要相同)

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        class teacher  
        { }  
        class student:teacher  //studnet继承teacher  
        { }  
        static void Main(string[] args)  
        {  
            teacher[] teac = new teacher[5];  
            student[] stu = new student[5];  
            stu = (student[])teac;      //显示转换   
              
        }  
    }  
}

如果换成下面的数组就不行

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        static void Main(string[] args)  
        {  
           double[] n_double = new double[5];  
            float[] n_float = new float[5];  
            n_float = (float[])n_double;     //这里出错啦  
              
        }  
    }  
}

从System.Array到数组类型 (array 是所有数组类型的基类)

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        static void Main(string[] args)  
        {  
           Array arr = new Array[5];   //定义一个Array类型的数组并初始化  
           double[] d = new double[5];  
           d = (double[])arr;    //显示转换  
        }  
    }  
}

从System.Delegate到代表(委托)类型

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
          
        public static delegate int mydele();  //声明一个委托  
  
        class DE : Delegate  //定义一个继承于Delegate 的类DE  
        { }  
        static void Main(string[] args)  
        {  
            Delegate MY =new DE();   // 将Delegate 抽象类间接实例化  
            mydele my = (mydele)MY;  //显示转换  
        }  
    }  
}

以上就是 C#学习日记17---显示类型转换具体用例的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
PHP培训优惠套餐