Home > Software design >  Override implicit conversion for existing types
Override implicit conversion for existing types

Time:02-24

In my code I am handling emails sometimes as string string emailA = "[email protected]". But most of the time an actual MailAddress object is passed around and handled. My question is can I implement an implicit conversion from string to MailAddress if I have no control over either type?

If this was an object implemented by me i could use this: public static implicit operator MailAddress(string email) => new MailAddress(email);

CodePudding user response:

No, that's currently not possible in C#.


You are not the only one who wants this feature: There's an open feature request in the C# language repository for "Extension Operators", which would allow you to define explicit and implicit conversion operators as extension methods.

  •  Tags:  
  • c#
  • Related