經過前面的學習C#中基本的數據類型就介紹的差不多了,下面就學習下類型之間的互相轉換.C# 中類型轉換可以分爲2類: 隱式轉換 和 顯式轉換.
隱式轉換:
隱式轉換是系統默認的轉換,不需要申明就可以進行轉換。在隱式轉換過程中,編譯器無需對轉換進行檢查就能夠安全的執行轉換,比如從int類型轉到long類型,就是隱式轉換。隱式轉換一般不會失敗,轉換過程中也不會丟失信息.
比如:int i = 100;
long a = i; //無需聲明自動的將int 型轉換爲long型
隱式轉換並非對任意兩種類型都成立,比如,我們將上面的long類型隱式轉換爲int類型就不會成功:
long a = 100;
int i = a; //編譯器會報錯
因此隱式轉換有以下的規則:
- 隱式數值轉換
- 隱式枚舉轉換
- 隱式引用轉換
隱式數值轉換:
隱式數值轉換包括以下幾種:
- 從sbyte 類型到short、int、long、float、double、decimal類型;
- 從byte 類型到short、ushort、int、uint、long、ulong、float、double、decimal類型;
- 從short 類型到 int、long、flaot、double、decimal類型;
- 從ushort 類型到 int、uint、long、ulong、flaot、double、decimal類型;
- 從int 類型到 long、flaot、double、decimal類型;
- 從uint 類型到 long、ulong、flaot、double、decimal類型;
- 從long 類型到 float、double、decimal類型;
- 從ulong 類型到 float、double、decimal類型;
- 從char 類型到 ushort、int、uint、long、ulong、flaot、double、decimal類型;
- 從float 類型到 double類型;
寫了這麼多總結下吧,概括的說就是從低精度類型到高精度類型轉換(因爲不丟失精度與數據信息),而從高精度類型到低精度不能隱式轉換(可能會丟失部分信息,不安全)。有關類型的精度與範圍請參照 C#學習日記04 。這裏要提醒的是 不存在其他類型到Char類型的隱式轉換。
隱式數值轉換實例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
byte x = 255; //byte 表示的範圍0~255
short y = x; //將從byte到short隱式轉換
y++;
Console.WriteLine("y = {0}",y);
y = 32767; //shot的範圍 -32768~32767
int i = y + 5; //從 short 到 int 隱式轉換擴大範圍 結果是準確的
y+=5; //超出範圍了結果會不準確
Console.WriteLine("y = {0}",y); //y超出範圍數據會丟失部分
Console.WriteLine("i = {0}",i);
}
}
}
結果:
從這個例子可以看出,及時的採用類型轉換還是很重要噠。
隱式枚舉轉換:
隱式枚舉轉換允許把十進制0,轉換爲任何枚舉類型,注意的是,它只能轉換0,對其他整數不存在這種隱式轉換,看下面的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
enum weekday //定義一個枚舉類型
{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
static void Main(string[] args)
{
weekday day;
day = 0; //隱式將0轉換爲枚舉類型(只能是0)
Console.WriteLine(day);
}
}
}
輸出結果是:
Sunday
上面代碼中如果我們把 day = 0 該爲 day = 1 編譯器就會給出錯誤。
隱式引用轉換:
- 從任何引用類型到對象類型的轉換;
(Person p = new Person())
- 從類類型s到類類型t的轉換,其中s是t的派生類;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class person //定義了一個基類(父類) person
{
}
class person1 : person // person1 派生於基類person,person1就叫person的一個子類,
{
}
class Program
{
static void Main(string[] args)
{
person1 per = new person1(); //將子類person1實例化一個對象per
person Per = per; //將子類隱式轉換爲父類
}
}
}
- 從類類型s到接口類型t的轉換,其中類s實現了接口t;(有關接口(interface)的內容後面會寫到,用它只聲明方法不定義方法)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public interface Infa //定義了一個接口
{
void Output();
}
class person : Infa //定義一個person類繼承於接口並實現方法
{
public void Output()
{
Console.WriteLine("Welcome");
}
}
class Program
{
static void Main(string[] args)
{
person per = new person(); //實例化
Infa fa = per; //從person到interface(接口)隱式轉換
}
}
}
- 從接口類型s到接口類型t的轉換,其中t是s的父接口;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public interface Infa //定義了一個接口
{
void Output(); //接口只聲明方法,具體實現由它的派生類寫代碼決定
}
public interface infa1 : Infa //定義一個infa1接口繼承於Infa接口
{
void input();
}
class person1 : infa1 //由infa1派生一個person1類,因爲接口不能直接實例化
{
}
class Program
{
static void Main(string[] args)
{
person1 per = new person1 { }; //接口不能直接實例化,需要實例化一個派生於infa1接口person1類
Infa fa = per; //實現子接口到父藉口隱式轉換
}
}
}
- 引用類型數組s到引用類型數組t轉換,其中s是t的派生類,並且數組維數得相同;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Person //定義一個基類 Person
{
}
class person1 : Person //由基類派生一個子類person1
{
}
class Program
{
static void Main(string[] args)
{
person1[] per = new person1[5]; //實例化一個person1
Person[] Per = per; //實現隱式轉換
}
}
}
在這裏要提醒的是,引用類型數組 如果是值類型數組以下代碼就會報錯:
class Program
{
static void Main(string[] args)
{
int[] n_int = new int[10];
double[] n_doubel = new double[10];
n_doubel = n_int; //這裏報錯啦
}
}
- 從數組類型到System.Array的轉換;(Array是所有數組的基類 參考上一篇^_^)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] n_int = new int[10]; //實例化一個int類型的數組 n_int
Array arr = n_int; // Array表示的就是數組 所以不能Array[] arr
}
}
}
- 從任何代表類型到System.Delegate的轉換;(後面會寫到delegate)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
public static int output(int s) //定義一個方法
{
Console.WriteLine("welcome,{0}",s);
return 1;
}
public delegate int mydel(int s); //聲明一個委託(以後我會說到委託)
static void Main(string[] args)
{
mydel my = new mydel(output); //將 output方法委託給my
Delegate MYDEL = my; //向 MYDEL 隱式轉換
}
}
}
常用到的就介紹這麼多了,需要補充請私信我,有關委託我後面會寫到的,請繼續支持HC66,感謝你的閱讀^_^,