Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The implicit and explicit keywords

Reference for the C# implicit and explicit keywords: define user-defined conversion operators, and decide when a conversion needs a cast or may throw.

implicit and explicit declare user-defined conversion operators. They teach the compiler how to turn one of your types into another type, or another type into yours. An implicit conversion happens automatically wherever the target type is expected. An explicit conversion requires a cast, (Target)value, which signals "this might lose information or fail".

Choose implicit only when the conversion always succeeds and loses nothing, like int to long. Choose explicit when it can throw or lose data, like long to int.

C#
public static implicit operator Meters(double value) => new(value);
public static explicit operator double(Meters m) => m.Value;

Try it

C# Implicit and explicit conversions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Conversions are static and ignore the run-time type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static implicit operator TargetType(SourceType value) => ...;
public static explicit operator TargetType(SourceType value) => ...;
public static explicit operator checked TargetType(SourceType value) => ...; // C# 11
Part Description
implicit / explicit Whether the compiler may apply the conversion without a cast.
TargetType The type produced. Either this or SourceType must be the declaring type.
SourceType The parameter type.

Errors: you cannot define a conversion to or from an interface (CS0552), to or from object, or between a type and its own base class or derived class (CS0553). You cannot declare both an implicit and an explicit conversion with the same signature (CS0557). Calling an explicit conversion without a cast fails with CS0266.

Usage notes

  • Conversion operators are public static methods, so they can be called, and are resolved, at compile time from the static types. They are not consulted by is or as, and not by a cast of an object: (Email)someObject throws InvalidCastException unless the object really is an Email.
  • The compiler never chains two user-defined conversions. It does allow one user-defined conversion with standard implicit conversions (for example int to double) on either side.
  • Implicit conversions can make overload resolution and code reading harder. Reserve them for obviously equivalent representations, such as a strongly typed ID that wraps an integer.
  • A conversion that throws (invalid range, failed validation) should be explicit. If failing is routine, offer TryParse-style methods instead.
  • The checked variant (C# 11) runs in a checked context, so that overflow can throw while the plain operator wraps around, as built-in numeric conversions do.
  • Nullable lifting applies: if Celsius has an implicit conversion from double, then double? converts to Celsius? (a null stays null).
  • A static abstract interface member (INumber<T>) lets generic code call conversions through CreateChecked<T>() and friends.