C#之自己定義的implicit和explicit轉換

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   for   sp   

在類型轉換時常會遇到隱式轉換和顯式轉換。那我們自己定義的類型要怎樣去定義隱式轉換和顯式轉換?我們來看一段代碼

public class Rational    {        private Int32 _inner_int = 0;        public Rational()        {        }        public Rational(Int32 num)        {            this._inner_int = num;        }        public Int32 ToInt32() { return this._inner_int; }        // Implicitly constructs and returns a Rational from an Int32         public static implicit operator Rational(Int32 num)        {            return new Rational(num);        }        // Explicitly returns an Int32 from a Rational         public static explicit operator Int32(Rational r)        {            return r.ToInt32();        }        public override string ToString()        {            //return base.ToString();            String s = String.Format("{0}", this._inner_int);            return s;        }    }

測試代碼

  class Program    {        static void Main(string[] args)        {            Rational r1 = 10;                      Console.WriteLine(r1);                   Int32 i = r1;                 Console.WriteLine(i);                   Console.ReadLine();        }    }
這時編輯會報錯,見

從提示能夠看到,是由於Int32 i=r1時缺少了顯式轉換。如今我們加入顯示轉換,改動後的代碼及輸出結果例如以下:

結果正常輸出為10.

那為什麼會這樣呢?究其原因是在Rational轉換成 Int32時,指定了explicit(顯式的),所以必需要指定轉換類型Int32。假設將explicit換成implicit(隱式),原來的代碼將能夠正常執行。

改動後的Rational

 public class Rational    {        private Int32 _inner_int = 0;        public Rational()        {        }        public Rational(Int32 num)        {            this._inner_int = num;        }        public Int32 ToInt32() { return this._inner_int; }        // Implicitly constructs and returns a Rational from an Int32         public static implicit operator Rational(Int32 num)        {            return new Rational(num);        }        // Explicitly returns an Int32 from a Rational         public static <span style="color:#ff0000;">implicit</span> operator Int32(Rational r)        {            return r.ToInt32();        }        public override string ToString()        {            //return base.ToString();            String s = String.Format("{0}", this._inner_int);            return s;        }    }
測試代碼及輸出結果


可見explicit和implicit影響著類型的顯式轉換和隱式轉換。

事實上在Rational r1=10已經運行了隱式轉換,相應的轉碼例如以下:

 // Implicitly constructs and returns a Rational from an Int32         public static implicit operator Rational(Int32 num)        {            return new Rational(num);        }
假設將implicit換成explicit,Rational r1=10也將會報錯(能夠自行測試)。


轉載請註明出處:http://blog.csdn.net/xxdddail/article/details/38057563

C#之自己定義的implicit和explicit轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.