.net - Is there a benefit to overloading the explicit operator if implicit has been overloaded in C#? -


i working structure requires implicit operator against strings , came across basic question had not thought about.

public static implicit operator version (string value) {...} 

i can understand having explicit operator enforce casting cannot think of situation need if implicit operator overloaded. there one?

there isn't. in fact, cannot define both implicit explicit conversion operator same conversion. compile time error:

public class foo {     public static implicit operator foo(string value)     {         console.writeline("implicit");         return null;     }      public static explicit operator foo(string value)     {         console.writeline("explicit");         return null;     } } 

it gives error of:

duplicate user-defined conversion in type ...

if define implicit conversion can write out explicit conversion , use implicit conversion's code conversion, there no way of defining code implicit conversion , and explicit conversion different things.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -