LearnThatStack Ace your next interview
C# · question
Question 11 of 112

Explain implicit and explicit type conversion.

beginner
← All C# questions
Re-explain

An implicit conversion happens automatically because the compiler knows it is safe, like int to double. An explicit conversion needs a cast, like (int)largeValue, because the value may not fit.

Implicit conversions go from a smaller type to a larger one, so the value never overflows. They are not always exact, though. Converting int to float or long to double can lose precision, while keeping the magnitude.

Explicit conversions go from larger to smaller types, or between types that are not directly compatible. They can lose data, as when a cast drops the decimal part of a double. In a checked context they can also throw an OverflowException.

The Convert class handles conversions a cast cannot do, like turning the string "123" into an int.

// Implicit conversion
int intValue = 123;
double doubleValue = intValue; // Automatic conversion
// Explicit conversion
double largeValue = 123.456;
int smallValue = (int)largeValue; // Manual cast, loses decimal part
// Using Convert class
string numberString = "123";
int convertedValue = Convert.ToInt32(numberString);
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • What happens when you cast a long that doesn't fit into an int, and how do you make that fail loudly? By default C# runs unchecked and keeps only the low bits, so you get a wrong number. A checked block or the overflow-check build option throws OverflowException.
  • What does (int)3.7 give you, and does Convert.ToInt32(3.7) give the same result? The cast truncates toward zero and gives 3. Convert rounds to the nearest integer, with ties going to the even number, so it gives 4.
  • The string comes from user input and might not be a number. Would you still use Convert.ToInt32? Convert throws FormatException on bad text and returns 0 for null. TryParse returns a bool, so bad input becomes a normal branch instead of an exception.
  • How would you let your own type, say a Money struct, convert to and from decimal? Declare public static implicit or explicit operator methods. Make one implicit only when the conversion can never fail or lose information.
  • How do casts work between a base class and a derived class? Derived to base is implicit. Base to derived needs a cast that throws InvalidCastException on the wrong type, so is or as can check first.

What you can say

  1. An implicit conversion happens on its own because the compiler knows it's safe, while an explicit one needs a cast because the value might not fit.
  2. Implicit conversions go from a smaller type to a larger one, like int to double, so the value can never overflow.
  3. They aren't always exact, though, since int to float or long to double keeps the magnitude but can lose precision.
  4. Explicit conversions go from larger to smaller types, or between types that aren't directly compatible, and they can lose data, like a cast from double to int dropping the decimal part.
  5. In a checked context an overflowing cast throws an OverflowException, and when a cast can't do the job, like turning the string "123" into an int, I use the Convert class.
  6. Writing a cast means I accept that data might be lost, so I only write one where I know the value fits or the loss doesn't matter.

Weak answers to avoid

  • Says implicit conversions never lose anything int to float and long to double are implicit and can still lose precision. Say they can't overflow, which is not the same as saying they're exact.
  • Thinks a cast to int rounds A cast from double to int truncates toward zero, so (int)2.9 is 2. If you need rounding, call Math.Round or Convert.ToInt32 and know which rule it uses.
  • Ignores overflow on narrowing casts In the default unchecked context, casting a too-large long to int gives a wrong number with no error. Mention the checked keyword and OverflowException.
  • Uses a cast to turn a string into a number (int)"123" doesn't compile. Strings need Convert.ToInt32, int.Parse or int.TryParse, and only TryParse handles bad text without throwing.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime