C # implicit explicit keywords (implicit and explicit data type conversion)

Source: Internet
Author: User

The implicit keyword is used to declare an implicit user-defined type conversion operator. (Explicit and vice versa) explicit is used to display and convert user-defined types.
Static Implicit operator target_type (source_type identifier ){......}
Implicit conversion can improve the readability of source code by eliminating unnecessary type conversion. However, because implicit conversions can occur without being specified, you must avoid unpleasant consequences.

In general, implicit conversion operators should never cause exceptions and never lose information, so that they can be safely used without being known. If the conversion operator cannot meet those conditions, mark it as explicit to display the conversion data.

Below are some simple examples found on the Internet

Example 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();        }    }

Example 2

// Basic data type to user-defined class distance {private int feet; private double inches; // default constructor public distance () {feet = 0; inches = 0.0 ;} // The constructor with a single parameter public distance (double metres) {double F; F = 3.28 * metres; this. feet = (INT) F; this. inches = 12 * (F-feet);} // implicitly constructs a distance public static implicit operator distance (double metres) {return New distance (metres );} // a distance explicitly returns a 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} feet {1} inches", this. feet, this. inches) ;}} class distancedemo {public static void main () {distance d1 = 1.25; console. writeline (D1); double D = (double) d1; console. writeline (d );}}

Example 3

using system; namespace hunts. Keywords {// define a Renminbi structure. The data type conversion syntax is the same for the public struct RMB structure and class {// note: the range of these numbers may not meet the actual use of 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} RMB {1} {2} points", Yuan, Jiao, Fen);} // some operations are 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) {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; // remember a donation in elementary school, I have contributed a dollar deposit in my pocket plus six cents and 13 cent coins: (r1 = new RMB (1, 6, 13 ); // In fact, at that time, other people had already handed in. They handed in a total of R2 = new RMB (46, 9, 3); // What I added is: r3 = R1 + R2; console. writeline ("R3 = {0}", r3.tostring (); // implicitly convert float F = R3; console. writeline ("float F = {0}", f); // explicitly converts r4 = (RMB) F; cons Ole. writeline ("r4 = {0}", r4.tostring (); // if no display conversion is performed, the error cs0266: implicit conversion of the "float" type to "hunts. keywords. RMB ". There is an explicit conversion (is forced conversion missing ?) Console. Read () ;}}


Source: http://www.cnblogs.com/chengxiaohui/articles/1914190.html

C # implicit explicit keywords (implicit and explicit data type conversion)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.