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.
public static implicit operator Meters(double value) => new(value); public static explicit operator double(Meters m) => m.Value;
Try it
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.
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
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 staticmethods, so they can be called, and are resolved, at compile time from the static types. They are not consulted byisoras, and not by a cast of anobject:(Email)someObjectthrowsInvalidCastExceptionunless the object really is anEmail. - The compiler never chains two user-defined conversions. It does allow one user-defined conversion with standard implicit conversions (for example
inttodouble) 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, offerTryParse-style methods instead. - The
checkedvariant (C# 11) runs in acheckedcontext, so that overflow can throw while the plain operator wraps around, as built-in numeric conversions do. - Nullable lifting applies: if
Celsiushas an implicit conversion fromdouble, thendouble?converts toCelsius?(anullstaysnull). - A static abstract interface member (
INumber<T>) lets generic code call conversions throughCreateChecked<T>()and friends.