implicit關鍵字用於聲明隱式的用戶定義類型轉換運算符。(explicit反之)explicit則用於顯示轉換用戶自定義類型。
static implicit operator target_type ( source_type identifier ){......}
隱式轉換能夠經過消除沒必要要的類型轉換來提升源代碼的可讀性。可是,由於能夠在未指定的狀況下發生隱式轉換,所以必須注意防止使人不愉快的後果。html
通常狀況下,隱式轉換運算符應當從不引起異常而且從不丟失信息,以即可以在不知曉的狀況下安全使用它們。若是轉換運算符不能知足那些條件,則應將其標記爲 explicit 做爲顯示轉換數據。 git
下邊是在網上找的幾個簡單例子安全
例1ide
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); } }
例3ui
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.htmlthis