The----of the Sunwen Tutorial C # Advanced 9

Source: Internet
Author: User
Tutorial Sunwen Tutorial----C # Advanced
Nine
Mrfat@china.com
Hello, I am Sunwen, it is May 4 23:15, the lights out in 15 minutes. So hurry up and start the day and continue tomorrow.

Now I would like to say is the user-defined conversion in C # (user-defined conversions), which used the struct of the previous knowledge, is the structure of ah, forget it? Good, not forgotten. We can see the usefulness of the structure from the following courses ( Just now I was thinking about what it's used for, hehe. A class is declared with class, and a struct declaration can be regarded as a type, yes, as in C # 's own int,short,long.

C # allows us to transform structs (struct) and classes (class) so that we can define some transformations. 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):
You use the implicit conversion tostring of Int. If it is (String) A, it is called a display. Therefore, the difference between the explicit/implicit is whether to show it. We must be confused now, wait until tomorrow I write out the example to analyze it clearly, the lights out, I go 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 implemented on him.

M://Userconversions\conversion.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://Conversion 033:console.writeline ((int) numeral) from numeral to int explicitly;
034:
035://implicitly converted to String036:Console.WriteLine (numeral);
037:
038://show to int, then display to short040:short s = (short) numeral;
041:
042:console.writeline (s);
043:
044:}
045:}
The output of this example is:

10
Conversion not yet implemented
10
Note the operator operator for 009 and 013, which is a conversion operator. static public explicit operator int (RomanNumeral Roman), remember this form and it represents a transformation. Look at line No. 033, Because in the front int this transformation is declared as explicit, that is to say, so when using this transformation, parentheses must be used.

Here's an example that declares two structures, RomanNumeral and binarynumeral, and then converts between them.

M://Userconversions\structconversion.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 of this example is:

10
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 convert RomanNumeral to int and then binarynumeral. The rest of the stuff is the same as the example above. (At least I think so), if the above example is understood, the following is good.

OK, and then finished a section, learned so much, how do we feel, welcome to communicate with me, mrfat@china.com

Next page


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.