Csharp/CSharp Tutorial/Operator Overload/Conversion Operator Overload

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

There are a few restrictions to conversion operators

  1. Either the target type or the source type of the conversion must be a class that you create.
  2. You cannot redefine the conversion from double to int.
  3. You cannot define a conversion to or from Object.
  4. You cannot define both an implicit and an explicit conversion for the same source and target types.
  5. You cannot define a conversion from a base class to a derived class.
  6. 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)

  1. Here are the other operators that cannot be overloaded.

  2. &&
  3. ||
  4. []
  5. ()
  6. new
  7. is
  8. sizeof
  9. typeof
  10. ?
  11. ->
  12. .
  13. =

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:


public static operator implicit target-type(source-type v) { 
    return value; 
}

User-Defined Conversions

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;
    }
}
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

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;
    }
}
conversion here