What is the difference between an implicit and an explicit conversion operator?

C# Language Features Intermediate 2–3 Yrs3–5 Yrs Conceptual 1 min read

Quick Answer

An implicit conversion happens automatically with no cast — define public static implicit operator when the conversion always succeeds and loses no information (int → long, a Celsius → Temperature). An explicit conversion requires a cast — use it when the conversion can fail or lose data (long → int, double → int).

An implicit conversion needs no cast — it just happens — so you only define an implicit operator when the conversion can never fail and can't lose information, like a value object wrapping a string, or a small numeric type to a bigger one. An explicit conversion forces the caller to write a cast, which is what you want when the conversion might overflow or truncate. The general advice is: implicit for widening and lossless, explicit for narrowing, and honestly a named method is often clearer than either.

  • Overloading conversion operators can make code surprising — an implicit conversion firing where you didn't expect. Use sparingly, mainly for value objects (Money, EmailAddress) where it reads naturally.
  • Guideline: implicit for widening / lossless, explicit for narrowing / potentially throwing.
  • Prefer a named method (ToKilometres(), Parse()) when the intent isn't obvious.