How C # Custom types are converted operator, and the difference between implicit (implicit) and explicit (display) declarations

Source: Internet
Author: User

MSDN Reference: Http://msdn.microsoft.com/zh-cn/library/s53ehcz3.aspx http://msdn.microsoft.com/zh-cn/library/ z5z9kes2.aspx the Http://msdn.microsoft.com/zh-cn/library/xhbhezf4.aspxoperator keyword to overload a built-in operator, or to provide a user-defined transformation in a class or struct declaration. It can define what conversions and conversions are used between different types of data. Operator can be used to define type conversions in 2 ways, implicit conversions (implicit) and display Transformations (explicit) are generally defined as copy code public static implicit target type (variable parameter of converted type) {return Target type result;} public static explicit target type (converted type variable argument) {return target type result;} Copy Code code case copy code using system;using system.collections.generic;using system.linq;using System.text;namespace Custom Reload {class program {static void Main (string[] args) {MyClass MC = 1;//generates MyClass Object Console.WriteLine (MC) by implicit reloading. Value); MyClass MC2 = new MyClass (2); Console.WriteLine (int) mc2);//Show conversion, call MyClass to int processing method Console.WriteLine (MC2);//implicit conversion, call MyClass to string processing}} class MyClass {private int value;//declares value private field public int value//declares read-only property {get {return value;}} public MyClass (int valu e)//constructor {this.value = value;} public static explicit operator INT (MyClass MC)//show declared MyClass to int class handlermethod {return mc.value;} public static implicit operator MyClass (int value)//implicitly declared int to MyClass class handling method {return new MyClass (Valu e); } public static implicit operator string (MyClass MC)//implicitly declared MyClass-to-string class processing method {return ("defined MyClass class string conversion result");}} Copy code Result "Note": 1. The conversion operator converts the source type to the target type. The source type provides the conversion operator. Unlike implicit conversions, an explicit conversion operator must be invoked by casting. If the conversion operation can result in an exception or loss of information, it should be marked as explicit. This prevents the compiler from silently invoking conversion operations that may have unforeseen consequences. 2. When using the implicit declaration of implicit, note that in the case of an unknown language, there are several implicit declarations that are converted from the current class to other types of data, there may be compile errors, due to implicit declaration, 2 can be called, and the compiler does not know which error to choose. For example, copy code using system;using system.collections.generic;using system.linq;using System.text;namespace Custom Reload {class program {static void Main (string[] args) {MyClass mc2 = new MyClass (2); Console.WriteLine (MC2);//error location, the compiler cannot select a valid type conversion method}}} class MyClass {private int value; public int value {get {return Valu E }} public MyClass (int value) {this.value = value,} public static implicit operator MyClass (int value) {return new MYCL (value); }///2 a method that implicitly declares the MyClass class conversion type public static implicit operator int (mYclass MC) {return mc.value;} public static implicit operator string (MyClass MC) {return ("defined MyClass class string conversion result");} Replication code Workaround: 1. Convert the output in Console.WriteLine () to clear the conversion target, such as Console.WriteLine (String) MC2, 2. Change one of the 2 implicit conversion methods to display, The compiler will call the default implicitly that copy code public static explicit operator INT (MyClass MC)//To display conversion {return mc.value;} public static implicit opera Tor string (MyClass MC) {return ("Definition of MyClass class string conversion result");} Copy Code

C # Custom types are converted in operator, and differ from implicit (implicit) and explicit (display) declarations

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.