[ Stack Overflow ] c# – What is the difference between explicit and implicit type casts?

c# – What is the difference between explicit and implicit type casts? – Stack Overflow

What is the difference between explicit and implicit type casts?

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.)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s