Csharp/CSharp Tutorial/Operator Overload/Conversion Operator Overload
Содержание
There are a few restrictions to conversion operators
- Either the target type or the source type of the conversion must be a class that you create.
- You cannot redefine the conversion from double to int.
- You cannot define a conversion to or from Object.
- You cannot define both an implicit and an explicit conversion for the same source and target types.
- You cannot define a conversion from a base class to a derived class.
- You cannot define a conversion from or to an interface.
(Quote from C# The Complete Reference, Publisher: Osborne/McGraw-Hill, March 8, 2002, Language: English ISBN-10: 0072134852 ISBN-13: 978-0072134858)
Here are the other operators that cannot be overloaded.
- &&
- ||
- []
- ()
- new
- is
- sizeof
- typeof
- ?
- ->
- .
- =
8.4.Conversion Operator Overload 8.4.1. <A href="/Tutorial/CSharp/0160__Operator-Overload/Therearetwoformsofconversionoperatorsimplicitandexplicit.htm">There are two forms of conversion operators, implicit and explicit</a> 8.4.2. There are a few restrictions to conversion operators 8.4.3. <A href="/Tutorial/CSharp/0160__Operator-Overload/UserDefinedConversionsConversionLookup.htm">User-Defined Conversions: Conversion Lookup</a> 8.4.4. <A href="/Tutorial/CSharp/0160__Operator-Overload/UserDefinedConversions.htm">User-Defined Conversions</a>
There are two forms of conversion operators, implicit and explicit
The general form for each is shown here:
<source lang="csharp">public static operator implicit target-type(source-type v) {
return value;
}</source>
User-Defined Conversions
<source lang="csharp">using System; using System.Text; struct MyType {
private int value; public MyType(int value) { this.value = value; } public static explicit operator MyType(short value){ Console.WriteLine("public static explicit operator MyType(short value)"); return new MyType(); } public static implicit operator short(MyType myType){ Console.WriteLine("public static implicit operator short(MyType myType)"); return 0; } public static implicit operator string(MyType myType){ Console.WriteLine("public static implicit operator string(MyType myType)"); return "String value:"; }
} class MainClass {
public static void Main() { int s = 12; MyType numeral = new MyType(s); s = 165; numeral = (MyType) s; Console.WriteLine("as int: {0}", (int)numeral); Console.WriteLine("as string: {0}", (string)numeral); int s2 = numeral; }
}</source>
public static explicit operator MyType(short value) public static implicit operator short(MyType myType) as int: 0 public static implicit operator string(MyType myType) as string: String value: public static implicit operator short(MyType myType)
User-Defined Conversions: Conversion Lookup
<source lang="csharp">using System; public class MyType {
public static implicit operator YourType(MyType s) { Console.WriteLine("conversion here"); return(new YourType()); }
} public class YourType { } public class YourDerivedType: YourType {
} public class Test {
public static void Main() { MyType myType = new MyType(); YourType tb = (YourType) myType; }
}</source>
conversion here