implicit關鍵字用於聲明隱式的用戶定義類型轉換運算符。(explicit反之)explicit則用於顯示轉換用戶自定義類型。
static implicit operator target_type ( source_type identifier ){......}
隱式轉換可以通過消除不必要的類型轉換來提高源代碼的可讀性。但是,因為可以在未指定的情況下發生隱式轉換,因此必須注意防止令人不愉快的后果。
一般情況下,隱式轉換運算符應當從不引發異常並且從不丟失信息,以便可以在不知曉的情況下安全使用它們。如果轉換運算符不能滿足那些條件,則應將其標記為 explicit 作為顯示轉換數據。
下邊是在網上找的幾個簡單例子
例1
class Digit { public Digit(double d) { val = d; } public double val; // User-defined conversion from Digit to double public static implicit operator double(Digit d) { return d.val; } // User-defined conversion from double to Digit public static implicit operator Digit(double d) { return new Digit(d); } } class Program { static void Main(string[] args) { Digit dig = new Digit(7); //This call invokes the implicit "double" operator double num = dig; //This call invokes the implicit "Digit" operator Digit dig2 = 12; Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val); Console.ReadLine(); } }
例2
//基本數據類型到用戶自定義類型 class Distance { private int feet; private double inches; //默認構造函數 public Distance() { feet = 0; inches = 0.0; } //帶有單參數的構造函數 public Distance(double metres) { double f; f = 3.28 * metres; this.feet = (int)f; this.inches = 12 * (f - feet); } //由一個double隱式構造一個Distance public static implicit operator Distance(double metres) { return new Distance(metres); } //由一個Distance顯式返回一個double public static explicit operator double(Distance d) { double metres; metres = d.inches / 12 + (double)d.feet; return (metres / 3.28); } public override string ToString() { return String.Format("{0}英尺{1}英寸 ", this.feet, this.inches); } } class DistanceDemo { public static void Main() { Distance d1 = 1.25; Console.WriteLine(d1); double d = (double)d1; Console.WriteLine(d); } }
例3
using System; namespace Hunts.Keywords { // 定義一個人民幣結構。數據類型轉換的語法對於結構和類是一樣的 public struct RMB { // 注意:這些數的范圍可能不能滿足實際中的使用 public uint Yuan; public uint Jiao; public uint Fen; public RMB(uint yuan, uint jiao, uint fen) { if (fen > 9) { jiao += fen / 10; fen = fen % 10; } if (jiao > 9) { yuan += jiao / 10; jiao = jiao % 10; } this.Yuan = yuan; this.Jiao = jiao; this.Fen = fen; } public override string ToString() { return string.Format("¥{0}元{1}角{2}分", Yuan, Jiao, Fen); } // 一些操作 public static RMB operator +(RMB rmb1, RMB rmb2) { return new RMB(rmb1.Yuan + rmb2.Yuan, rmb1.Jiao + rmb2.Jiao, rmb1.Fen + rmb2.Fen); } public static implicit operator float(RMB rmb) { return rmb.Yuan + (rmb.Jiao / 10.0f) + (rmb.Fen / 100.00f); } public static explicit operator RMB(float f) { uint yuan = (uint)f; uint jiao = (uint)((f - yuan) * 10); uint fen = (uint)(((f - yuan) * 100) % 10); return new RMB(yuan, jiao, fen); } // more } class App { static void Main() { RMB r1, r2, r3, r4; // 記得小學時的某次捐款,我把口袋里藏好的一塊錢加6張一毛錢以及13個一分錢的硬幣都貢獻出去了:( r1 = new RMB(1, 6, 13); // 其實當時其他人都已經交過了,他們總共交了: r2 = new RMB(46, 9, 3); // 那么加上我的就是: r3 = r1 + r2; Console.WriteLine("r3 = {0}", r3.ToString()); // 隱式轉換 float f = r3; Console.WriteLine("float f= {0}", f); // 顯式轉換 r4 = (RMB)f; Console.WriteLine("r4 = {0}", r4.ToString()); //如果不進行顯示轉換,將出現錯誤 CS0266: 無法將類型“float”隱式轉換為“Hunts.Keywords.RMB”。存在一個顯式轉換(是否缺少強制轉換?) Console.Read(); } } }
出處:http://www.cnblogs.com/chengxiaohui/articles/1914190.html
