Differences between implicit and explicit (note)

Source: Internet
Author: User
Public class RMB {public int yuan = 0; Public RMB (INT yuan) {yuan = yuan;} public static RMB operator + (RMB Rmb1, RMB RMB2) {// type return new RMB (rmb1.yuan + rmb2.yuan);} public static implicit operator int (RMB) {// type 2: Implicit implements forced type conversion return RMB. yuan;} public static implicit operator RMB (INT yuan) {// three types: Implicit implements forced type conversion return new RMB (yuan );} public static explicit operator RMB (float yuan) {// type 4: explicit implements implicit type conversion. A forced type conversion is required externally. Return new RMB (INT) yuan );}}

Call

RMB RMB1 = new RMB (1); RMB RMB2 = new RMB (2); RMB RMB3 = RMB1 + RMB2; // a single int yuan = RMB3; // RMB RMB4 = 5; // three types: Implicit implements forced type conversion RMB RMB5 = (RMB) 5f; // four types: explicit implements implicit type conversion and requires forced type conversion outside

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.