首頁  >  文章  >  後端開發  >  C#學習日記17---顯示類型轉換特定用例

C#學習日記17---顯示類型轉換特定用例

黄舟
黄舟原創
2017-01-21 15:07:061551瀏覽

   在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型別;

  • 從byte 到sbyte、char型別;

  • 從short 到sbyte、char型;

  • 從ushort 到sbyte、byte、short 、char型別;

  • 從int 到sbyte、byte、short、ushort、uint、ulong、char型號; ushort、int、char型別;

  • 從long 到sbyte、byte、short、ushort、int、uint、ulong、char型別;

  • 從ulong 到 sbyte、byte、short

    、int、int、int、 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);  
                  
            }  
        }  
    }
  • 運行結果:

對比發現當double 資料範圍超出float的有效值範圍,顯示轉換時對第9位四捨五入,轉換為int型別時只保留整數部分。


 

顯示枚舉轉換:C#學習日記17---顯示類型轉換特定用例

    顯示枚舉轉換包括以下幾個:

從sbyte、byte、short、ushort、intuint、long

從sbyte、byte、short、ushort、intuint、long、ulong、mint、longdecile、irode到任何枚舉類型;

  • 從任何枚舉類型到 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);  
                  
            }  
        }  
    }

    運行結果:

顯示引用轉換:

類型t的轉換,其中s是t的基類;

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类  
              
        }  
    }  
}
C#學習日記17---顯示類型轉換特定用例從類類型s到接口t的轉換,其中s不是密封類,並沒有實現t;(有關接口(interface)的內容後面會寫到,它只聲明方法不定義方法)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

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 t不是密封類,並沒有實作s;

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

從介面型別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  //定义一个继承于man的类man  
        { }  
        class student   //定义一个新类  
        { }  
        static void Main(string[] args)  
        {  
            man teac=new teacher(); //间接实例化一个接口  
            student stu = (student)teac;  // 显示转换  
                          
        }  
    }  
}

引用型別與引用型別陣列顯示轉換,其中兩者是父類與子類的關係(維數要相同)
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;      //显示转换   
              
        }  
    }  
}

從System.Array到數組類型(array 是所有數組類型的基類)

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.Delegate到代表(委託)類型

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;    //显示转换  
        }  
    }  
}

以上就是 C#學習日記17---顯示類型轉換具體用例的內容,更多相關內容請關注PHP中文網(www.php.cn)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn