4

I'm learning C# now. The tutorial doesn't make it clear when to use keyword implicit or explicit when overloading conversion operators.

The example it provides is like this:

When Class1 contains a field of type int and Class2 contains a field of type double, we should define an explicit conversion from Class2 to Class1, and an implicit conversion from Class1 to Class2.

The tutorial doesn't say what will happen if I use the wrong keyword.

But if Class1 contains a complex subclass and Class2 contains a different subclass, which keyword should I use between implicit and explicit? Can anyone gives a clear explanation? Thanks a lot.

dav_i
  • 27,509
  • 17
  • 104
  • 136
Zhu Shengqi
  • 3,632
  • 3
  • 24
  • 29
  • 2
    I don't see this as an exact duplicate. One question is why is float to double implicit and double to float explicit conversion. While the linked question's answers are of value to the questioner, the answers aren't exact. – IS4 Jan 06 '15 at 13:32
  • 1
    yes the "duplicate" is really bad ! – mybirthname Jan 06 '15 at 13:32
  • I agree. I wouldn't say it's a duplicate. The other question explains how to do it and this one is about when it might be usefull. – t3chb0t Jan 06 '15 at 13:33

1 Answers1

1

Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

Explicit conversions (casts):

Explicit conversions require a cast operator. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons. Typical examples include numeric conversion to a type that has less precision or a smaller range, and conversion of a base-class instance to a derived class.

Check the bold texts in this explanation. Here is the detail article in MSDN

Here is little code example:

// Create a new derived type.
Giraffe g = new Giraffe();

// Implicit conversion to base type is safe.
Animal a = g;

// Explicit conversion is required to cast back
// to derived type. Note: This will compile but will
// throw an exception at run time if the right-side
// object is not in fact a Giraffe.
Giraffe g2 = (Giraffe) a;
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • 1
    I see. It's used to remind the developers to use conversion between user-defined types wisely, and an enforcement of explicit conversion is telling the developer "the conversion you are using may result to losing original data". Thanks a lot – Zhu Shengqi Jan 06 '15 at 13:40
  • @ZhuShengqi:_the conversion you are using may result to losing original data_ this might be true but it doesn't have to. As I don't have the oportunity to post my own answer I'll try to explain the point if few words here. You would use the implicit operation for example if you want to create a helper class for convenience sake like a better Color class that can be serialized and deserialized unlike the .net struct and to make it possible to use it everywhere you would normaly use the Color struct you can extend your BetterColor to be able to implicitly castable to/from the Color. – t3chb0t Jan 06 '15 at 13:44
  • @t3chb0t : Thanks a lot. You mean that the implicit conversion can also make the derived class more reusable in refining an old project and other circumstances. Thank you very much! – Zhu Shengqi Jan 06 '15 at 13:55