What is the usage of these operators?
Asked
Active
Viewed 965 times
2 Answers
11
Basically when you want to provide conversions between types. LINQ to XML provides good examples... There's an implicit conversion from string to XName, so you can write:
XName name = "element";
but there's an explicit conversion from XAttribute
to int
(and many other types) so you have to include a cast in your code:
int value = (int) element.Attribute("age");
Think very carefully before providing implicit conversions - they're rarely a good idea; LINQ to XML uses them to great effect, but they can be confusing. Even explicit user-defined conversions can surprise the unwary reader.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
Nearly most of the times I avoid using them as a library designer (unless I am sure they will not be misused and complained). – Lex Li Mar 04 '10 at 05:19
4
They are used when doing operator overloading. Here's a link to a MSDN article.

Richard Nienaber
- 10,324
- 6
- 55
- 66