Conversion operator (non-primitive type conversion), Operator

Source: Internet
Author: User

Conversion operator (non-primitive type conversion), Operator

When the source and target types are not primitive types, CLR cannot compile and convert them by itself.

In the following example, convert Rational (Rational number type) to string or int.

A Conversion operator is a method that converts an object from one type to another. You can use special syntax to define the method for changing operators.

CLR requires that the overload methods of the conversion operators must be public and static. C # either the parameter type or the return type must be the same as the type used to define the conversion method.

// Defines a public constructor with only one parameter. When this parameter is set, an instance of the source type // defines the public instance method ToXxx without parameters (l is similar to ToSting) // each method loads an instance that defines the type of this method into the Xxx type public sealed class Rational {// constructs a Rational public Rational (int num) by an int) {}// is implicitly constructed by int and returns a Rational public static implicit operator Rational (int num) {return new Rational (num );} // construct a Rational public Rational (single s) {} by a Single implicitly and return a Rational public static implicit operator Rational (Single num) {return new Rational (num);} // convert Rational into an int public Int32 ToInt32 () {return new Int32 ();} // return an Int32 public static explicit operator Int32 (Rational r) {return r. toInt32 () ;}// convert Rational into a Single public Single ToSingle () {return new Single ();} // returns a Single public static explicit operator Single (Rational r) {return r. toSingle ();}}

  

Namespace Type constructor {class Program {static void Main (string [] args) {int a = 5; Single B = 8; Rational rint = new Rational (); rational bsigle = new Rational (B); Rational r1 = 5; // int is implicitly converted to rational; Rational r2 = 3.14f; // single is implicitly converted to Rational r3 = 4; rational r4 = 6; Console. writeLine (rint. getType (); // The output is the Rational Console. writeLine (bsigle. getType (); // The output is the Rational Console. writeLine (r3.ToInt32 (). getType (); // The output is System. int32 Console. writeLine (r4.ToSingle (). getType (); // The output is System. single Console. readKey ();}}}

The implicit keyword in c # Tells the compiler to call methods in order to generate code, without explicit transformation in the source code. On the contrary, explicit tells the compiler to call methods only when explicit transformation is discovered.

After the implicit and explicit keywords, specify the operator keyword to tell the compiler that this method is a conversion operator. After operator, specify the type to replace the object with, and specify the type to replace in parentheses.

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.