Conversion operators and operators

Source: Internet
Author: User
/* ===================================================** TestOperator. cs * // conversion operator // heavy-duty operator ==================================== = */using System; public class Complex {private int x; public Complex (int x) {this. x = x ;}public int X {get {return x ;}set {x = value ;}/// + overload public static Complex operator + (Complex c1, complex c2) {return new Complex (c1.X + c2.X);} // public Complex Add (Complex c) {return (this + c );} // conversion operator // an int32 implicitly constructs a Complex object and returns the public static implicit operator Complex (Int32 numertor) {return new Complex (numertor );} // a Complex display returns an int32public static explicit operator Int32 (Complex c) {return c. X ;}} public class App {public static void Main (string [] args) {Complex c1 = new Complex (90); Complex c2 = new Complex (100 ); complex c3 = c1 + c2; Complex c4 = 2000; Console. writeLine (c1 + c2 ). x. toString (); Console. writeLine (c4.X. toString (); Console. read ();}}

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.