Skip to content
Latchkey

C# "CS0029: Cannot implicitly convert type A to B" in CI

CS0029 is an assignment-side type error: the right-hand type cannot be implicitly converted to the left-hand type. It is closely related to CS1503 (which is the argument-passing form). The fix is an explicit conversion or correcting the declared type.

What this error means

Build fails with CS0029 naming the source and target types. Reproduces deterministically for the same code.

dotnet
Mapper.cs(18,24): error CS0029: Cannot implicitly convert type 'long' to 'int'

Common causes

No implicit conversion exists between the types

Assigning, for example, a long to an int or an object to a derived type requires an explicit cast the code omits.

The declared variable/return type is wrong

A method returns or a property holds a different type than the target was declared with.

How to fix it

Add an explicit conversion

  1. Cast or convert the value when a narrowing/explicit conversion is intended and safe.
  2. Use checked conversion if overflow matters.
  3. Rebuild.
.cs
int id = checked((int)longId);

Correct the declared type

  1. Change the variable/property/return type to match the actual value type.
  2. Rebuild.

How to prevent it

  • Keep declared types aligned with the values assigned to them.
  • Be explicit about narrowing conversions and overflow behavior.
  • Let analyzers flag risky implicit-conversion gaps.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →