C # advanced tutorial (9) User-Defined Conversion

Source: Internet
Author: User

User-Defined Conversions in C)

Now I want to talk about User-Defined Conversions in C #. The previous struct knowledge is used as the structure. Have you forgotten it? Okay, just forget it. from the following courses, we can see the usefulness of the structure (I was just wondering what it is for, huh, huh ). the class declaration is a class, and the struct declaration can be regarded as a type, right, that is, the int, short, and long types that come with C.

In C #, we can convert the structure (struct) and class, so we can define some conversions in it. however, C # specifies that all the conversion statements must be selected in the explicit and implicit statements. for example, when we use this statement
Int a = 10;
System. Console. println ():
Int implicit conversion toString is used. if it is (String) a, it is called display. therefore, the difference between explicit and implicit lies in whether it is displayed. everyone is confused now. I will write the example tomorrow and analyze it again. The light will be turned off. I will take a step first!


Oh ~~~~~ Finally, at, May 5, the following example is provided. In this example, a type named RomanNumeral is declared and several conversions are implemented for it.

000: // UserConversionsconversion. cs
001: using System;
002:
003: struct RomanNumeral
004 :{
005: public RomanNumeral (int value)
006 :{
007: this. value = value;
008 :}
009: static public implicit operator RomanNumeral (int value)
010 :{
011: return new RomanNumeral (value );
012 :}
013: static public explicit operator int (RomanNumeral roman)
014 :{
015: return roman. value;
016 :}
017: static public implicit operator string (RomanNumeral roman)
018 :{
019: return ("Conversion not yet implemented ");
020 :}
021: private int value;
022 :}
023:
024: class Test
025 :{
026: static public void Main ()
027 :{
028: RomanNumeral numeral;
029:
030: numeral = 10;
031:
032: // explicitly convert from numeral to int 033: Console. WriteLine (int) numeral );
034:
035: // implicitly converted to string036: Console. WriteLine (numeral );
037:
038: // convert the display to int, and then convert the display to objective 040: short s = (short) numeral;
041:
042: Console. WriteLine (s );
043:
044 :}
045 :}
In this example, the sub-output is:

10
Conversion not yet implemented
10
Note the operator operators of and 013. It is a conversion operator. static public explicit operator int (RomanNumeral roman). Remember this form and it represents a conversion. let's look at the second line, because the int conversion is declared as explicit, that is, display, so we must use parentheses when using this conversion.

Here is another example. The example declares two structures: RomanNumeral and BinaryNumeral, and then converts them.

000: // UserConversionsstructconversion. cs
001: using System;
002:
003: struct RomanNumeral
004 :{
005: public RomanNumeral (int value) {this. value = value ;}
006: static public implicit operator RomanNumeral (int value)
007: {return new RomanNumeral (value );}
008: static public implicit operator
009: RomanNumeral (BinaryNumeral binary)
010: {return new RomanNumeral (int) binary );}
011: static public explicit operator int (RomanNumeral roman)
012: {return roman. value ;}
013: static public implicit operator string (RomanNumeral roman)
014: {return ("Conversion not yet implemented ");}
015: private int value;
016 :}
017:
018: struct BinaryNumeral
019 :{
020: public BinaryNumeral (int value) {this. value = value ;}
021:
022: static public implicit operator BinaryNumeral (int value)
023: {return new BinaryNumeral (value );}
024: static public implicit operator string (BinaryNumeral binary)
025: {return ("Conversion not yet implemented ");}
026: static public explicit operator int (BinaryNumeral binary)
027: {return (binary. value );}
028:
029: private int value;
030 :}
031:
032: class Test
033 :{
034: static public void Main ()
035 :{
036: RomanNumeral roman;
037: roman = 10;
038: BinaryNumeral binary;
039: binary = (BinaryNumeral) (int) roman;
040: roman = binary;
041: Console. WriteLine (int) binary );
042: Console. WriteLine (binary );
043 :}
044 :}
The output in this example is:

10
Conversion not yet implemented
Note that row 039th is not directly converted from RomanNumeral to BinaryNumeral because no direct conversion is provided. so first convert RomanNumeral to int and then BinaryNumeral. the rest of things are the same as the above example (at least I think so). If the above example is understood, the following will be fine.

OK. After finishing another section, I learned so much. How do you feel? please contact me.Stream, mrfat@china.com

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.