What is the difference between explicit and implicit type casts?

This is a little tricky because the "cast" syntax in C# actually does a range of different things (cast, primitive convert, bespoke convert, etc)

In an implicit cast, there is an obvious reference-preserving conversion between the two:

List<int> l = new List<int>();
IList<int> il = l;

The compiler can prove that this is safe just from static analysis (List<int> is always an IList<int>)

With an explicit cast, either you are telling the compiler that you know more than it does - "please believe me, but check anyway":

List<int> l = new List<int>();
IList<int> il = l;
List<int> l2 = (List<int>)il;

Although this cast is possible, the compiler won't accept that all IList<int>s are actually List<int> - so we must tell it to let it by.


In an implicit primitive conversion (providedby the language spec), it is generally assumed that there is a safe, non-risky, non-lossy (caveat: see Jon's comment) conversion:

int i = 1;
float f = i;

With an explicit primitive conversion, it is likely that the conversion could lose data, or is non-obvious:

float f = 1;
int i = (int)f;

With bespoke operators, all bets are off, and you'd have to look at the documentation. It could be a reference-cast, or it could be anything. It may follow similar rules to primitive conversions (example: decimal), or it could do anything randomly:

XNamespace ns = "http://abc/def"; // implicit
XAttribute attrib = GetAttrib();
int i = (int)attrib; // explicit (extracts text from attrib value and
                     // parses to an int)

Both of these run custom code that is context-specific.


int i = 2;

float a = i;        // Implicit
float b = (float)i; // Explicit

What's the difference between the President of the United States and the President of Canada?

Since there is no President of Canada, it's hard to answer the question. The right thing to do is to push back and ask for clarification of the question. By "the President of Canada", does the questioner mean the Queen (ceremonial head of state), the Governor General (who can veto bills) or the Prime Minister (who effectively acts as the executive), or something else? Hard to say without clarification.

And even with clarification, it's a vague question. What differences do you want to know about?

Since there is no such thing as an "implicit cast" in C# it is hard to answer your question. In C#, casting is an operator. So I'll push back on it.

Did you mean to ask "what's the difference between an explicit conversion and an implicit conversion?" Or did you mean to ask about the semantics of the cast operator? Or the difference between the cast operator and other type conversion operators? Or situations in which cast operators can be "implicitly" inserted into your code by the compiler? (For example, the foreach loop and the += operator can both implicitly insert an invisible cast.)

Can you clarify the question? What two things are you asking for comparison of, and what sorts of differences are you interested in?

You might consider reading the "Conversions" chapter of the C# specification. Odds are good that any question you have about conversions are answered there.

Tags:

C#