Ad Space — Top Banner

CS0029

C# Programming Language

Severity: Minor

What Does This Error Mean?

CS0029 means you tried to put a value of one type into a variable of a different type, and C# cannot do that automatically. For example: storing a double into an int variable, or putting a string into a DateTime variable. C# is a strongly-typed language — it only allows automatic conversions when there is no risk of data loss. The fix is usually an explicit cast, a conversion method, or changing the variable's type.

Affected Models

  • .NET Framework
  • .NET Core
  • .NET 5+
  • Visual Studio
  • Visual Studio Code
  • Rider

Common Causes

  • Assigning a floating-point number (double or float) to an integer variable without an explicit cast
  • Assigning a long value to an int variable — the value might not fit
  • Returning a value of the wrong type from a method
  • Trying to assign a derived class variable directly where a different derived class is expected
  • Mixing numeric types in an expression — for example adding an int and a decimal and assigning to int

How to Fix It

  1. Read the error: 'Cannot implicitly convert type X to Y'. Identify the source type (X) and the destination type (Y). This tells you exactly what types are in conflict.

    The types are named clearly. For example: 'Cannot implicitly convert type double to int' means a double value is being assigned to an int variable.

  2. If data loss is acceptable (for example converting double to int truncates the decimal part), use an explicit cast with parentheses.

    Example: int myInt = (int)myDouble; — the (int) prefix is the explicit cast. Note that 3.9 becomes 3, not 4.

  3. If you need proper rounding when converting from double to int, use Math.Round() before casting.

    Example: int myInt = (int)Math.Round(myDouble); — this rounds 3.9 to 4 before converting.

  4. For string-to-number conversions, use the type's Parse() or TryParse() method, not a cast.

    Example: int value = int.Parse(myString); or safer: int.TryParse(myString, out int value)

  5. If the types are completely unrelated, reconsider whether you are using the right type. Sometimes the real fix is to change the variable declaration to match what you actually have.

    If a method returns double, declare the receiving variable as double rather than int — unless you truly need an integer.

When to Call a Professional

CS0029 is a compile-time error you can always fix yourself. The error message tells you the source type and the target type. Decide whether you need an explicit cast, a conversion method, or a type change. Be careful with casts that truncate data — for example casting double 3.9 to int gives 3, not 4.

Frequently Asked Questions

Why does C# not just convert types automatically like some other languages?

C# is designed to prevent silent data loss. Converting double to int can lose the decimal part — for example 3.9 becomes 3. Converting long to int can lose magnitude — large numbers become incorrect values. By requiring explicit casts for these conversions, C# forces you to make a deliberate choice, preventing bugs from silent data truncation.

What conversions does C# allow automatically?

C# allows implicit conversions only when there is no risk of data loss. For example: int to long, int to double, float to double. These are called widening conversions — the destination type can hold all possible values of the source type. Narrowing conversions (double to int, long to int) always require an explicit cast.

When should I use Convert.ToInt32() instead of an explicit cast?

Convert.ToInt32() rounds the value (3.9 becomes 4), while (int) truncates it (3.9 becomes 3). Convert methods also handle null gracefully — Convert.ToInt32(null) returns 0 instead of crashing. Use explicit casts when performance matters and you understand the truncation behavior. Use Convert methods when you need consistent rounding or null safety.