Sunwen Tutorial----C # Advanced (ix)

Source: Internet
Author: User
Now what I'm going to say is the user-defined conversion in C # (user-defined Conversions), which uses the knowledge of the preceding struct, that is, the structure, forget it? Well, not forgetting it. We can see the usefulness of the structure from our following courses ( Just now I was wondering what it's for, hehe. Declaring a class with class, and struct declaration can be considered a type, yes, like the int,short,long of C # itself.

C # allows us to transform structures (structs) and classes (class), so we can define some transformations in them. However, C # stipulates that all conversion declarations must be selected in both display (explicit) and implicit (implicit). For example, When we use this statement,
int a=10;
System.Console.PRintln (a):
The conversion of the implicit transformation of int is used. If it is (String) A, it is called display. So, the difference between the explicit/implicit is whether to show it. Everyone must be confused now, until tomorrow I will write an example to analyze it clearly, to turn off the lights, I first step!

Oh, ~~~~~ finally got up, May 5 8:45. Here's an example, in this case, a type named RomanNumeral is declared, and then several conversions are performed on him.

001:using System;
003:struct RomanNumeral
004: {
005:public romannumeral (int value)
006: {
007:this.value = value;
009:static Public implicit Operator romannumeral (int value)
010: {
011:return new RomanNumeral (value);
013:static public explicit operator int (RomanNumeral roman)
014: {
015:return Roman.value;
017:static Public implicit operator string (RomanNumeral roman)
018: {
019:return ("Conversion not yet implemented");
021:private int value;
024:class Test
025: {
026:static public void Main ()
027: {
028:romannumeral numeral;
030:numeral = 10;
032://convert explicitly from numeral to int 033:console.writeline ((int) numeral);
035://Implicit conversion to string036:Console.WriteLine (numeral);
038://display to int, then display to short040:short s = (short) numeral;
042:console.writeline (s);
The output of this example is:

Conversion not yet implemented
Note the operator operator for 009 and 013, which is a conversion operator. static public explicit operator int (RomanNumeral roman), remembering this form, represents a transformation. Look at line No. 033, Since the conversion in the preceding int is declared as explicit, that is, it must be in parentheses when using this conversion.

An example is given below, which declares two structures, RomanNumeral and binarynumeral, and then converts between them.

001:using System;
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;
018:struct BinaryNumeral
019: {
020:public binarynumeral (int value) {this.value = value;}
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);}
029:private int value;
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);
The output of this example is:

Conversion not yet implemented
Note that line No. 039 is not converted directly from RomanNumeral to BinaryNumeral because there is no direct conversion provided. So first convert RomanNumeral to int, then to BinaryNumeral. The rest is the same as the example above. (At least I think so), if the above example is understood, the following is good.

The above is the Sunwen tutorial----C # Advanced (ix) content, more relevant content please pay attention to (!

  • 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: 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.