Defining Casting Rules (Cast Operator Overloading)


One of the most powerful features in C# is its ability to define casting rules. For example, you can define that your object is compatible with a type that isn't in the class's hierarchy. In the case of the SuperheroMouse class you could say that SuperheroMouse objects can be converted to Entrepreneur-Mouse types. Defining casting rules is done by adding a cast operator.

Cast operators come in two flavors: explicit cast operators and implicit cast operators. Choosing between them is just a matter of style. Remember that when an implicit cast is possible you can just make a variable of one type equal to a variable of a different type and the compiler takes care of the conversion. If you implement implicit cast operators, this means less typing for the users of the class but the potential for more confusion. With explicit casts the compiler allows the conversion only if the programmer does an explicit case; this means more typing but less confusion. This is known as the Law of Inverse Productivity: the less typing you do, the more confused your readers are. (I just made that up, in case you're wondering.)

To define an explicit cast operator:

  1. Inside a class type public static .

  2. Type explicit operator, or implicit operator .

  3. Type the name of the type you wish to convert to. For example: EntrepreneurMouse .

  4. Type an open parenthesis ( .

  5. Type the name of your class. For example: SuperheroMouse .

  6. Type the name of a variable as input parameter. For example: source .

  7. Type a close parenthesis ) .

  8. Type an open curly bracket { .

  9. Type the statements that process the conversion and return the value of the output type.

  10. Type a close curly bracket } ( Figure 7.28 ).

    Figure 7.28 Notice that the only thing SuperheroMouse and EntrepreneurMouse have in common is they are both derived from Mouse. Without the cast operator function that is highlighted, an explicit cast would be illegal.
     class Mouse {    public string Name;    public void EatCheese()    {    } } class EntrepreneurMouse : Mouse {    public void ChargeOutrageousPrices()    {    } } class SuperheroMouse : Mouse {    public void DefeatEvildoers()    {    }  public static explicit operator   EntrepreneurMouse(SuperheroMouse   source)  {      EntrepeneurMouse dest =      new EntrepreneurMouse();      dest.Name = source.Name;      return dest;    } } 

graphics/tick.gif Tips

  • Once you add an explicit operator to your class a developer can convert from the original type to the type specified in the operator as shown in Figure 7.29 .

    Figure 7.29 The only way to use the explicit cast operator is to do an explicit cast. Incidentally, C# doesn't use the explicit cast operator you define if you try to cast using the as operator.
     SuperheroMouse mightyMouse = new SuperheroMouse(); EntrepreneurMouse mmWithAgent =  (EntrepreneurMouse)  mightyMouse; 
  • You can also define explicit operators that can convert from other types into the type of your class ( Figure 7.30 ).

    Figure 7.30 The difference between this code example and the previous one is that in the previous example we're defining a conversion from our type to EntrepreneurMouse . In this example we are defining a conversion from EntrepreneurMouse to SuperheroMouse . You can also choose to combine both functions.
     class SuperheroMouse : Mouse {    public void DefeatEvildoers()    {    }  public static explicit operator   SuperheroMouse(EntrepreneurMouse source)  {      SuperheroMouse dest = new SuperheroMouse();      dest.Name = source.Name;      return dest;    } } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net