在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);
}
}
}
運行結果:
對比發現當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);
}
}
}
運行結果:
顯示引用轉換:
- 從對象到任何引用類型的轉換;
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; //顯示轉換
}
}
}
顯示轉換就介紹到這裏了,您的評論與建議是我完善自我的動力,文章中有一些內容前面沒介紹過後面會具體寫出來的,謝謝閱讀 ^_^……