How to Resolve Error "CS0029: Cannot implicitly convert type 'type' to 'type'" in C#
The Compiler Error CS0029 is a fundamental Type Mismatch error. The message states: "Cannot implicitly convert type 'TypeA' to 'TypeB'".
C# is a statically typed language, meaning variables are strictly defined by their data type. The compiler allows implicit conversions only when they are safe and no data loss can occur (e.g., converting an int to a long). If a conversion carries a risk of losing precision (like double to int) or if the types are fundamentally different (like string to int), the compiler refuses to do it automatically.
This guide explains how to handle these conversions manually using Casting, Parsing, or Conversion methods.
Understanding Implicit vs. Explicit Conversion
- Implicit (Automatic): Safe.
intfits insidedouble.Childfits insideParent. - Explicit (Manual): Potentially unsafe.
doubledoes not fit insideint(decimals are lost).Parentmight not beChild.
CS0029 appears when you try to perform an explicit scenario using implicit syntax.
Scenario 1: Numeric Conversions (Possible Data Loss)
You cannot assign a floating-point number (like double or float) to an integer variable because the decimal part would be truncated (lost). The compiler protects you from this accidental data loss.
Example of Mistake
public void Calculate()
{
double exactPrice = 19.99;
// ⛔️ Error CS0029: Cannot implicitly convert type 'double' to 'int'
int wholeNumberPrice = exactPrice;
}
Solution: Explicit Cast
If you are okay with losing the decimal precision, use a cast (int).
public void Calculate()
{
double exactPrice = 19.99;
// ✅ Correct: We explicitly tell the compiler "I know I will lose the .99"
int wholeNumberPrice = (int)exactPrice;
System.Console.WriteLine(wholeNumberPrice);
}
// Output: 19
Scenario 2: String to Other Types
Strings are text. Even if the text is "100", it is not a number in memory. You cannot simply assign a string to an int or bool.
Example of Mistake
public void ReadInput()
{
string input = "50";
// ⛔️ Error CS0029: Cannot implicitly convert type 'string' to 'int'
int amount = input;
}
Solution: Parsing
You cannot "cast" a string (e.g., (int)input is also invalid). You must Parse or Convert it.
public void ReadInput()
{
string input = "50";
// ✅ Correct: Parse the string content into a number
int amount = int.Parse(input);
// Or use Convert
int amount2 = System.Convert.ToInt32(input);
}
int.Parse will throw an exception at runtime if the string does not contain a valid number (e.g., "Hello"). Use int.TryParse for safer code.
Scenario 3: Nullable Types to Non-Nullable
A int? (Nullable Integer) can hold a number or null. A standard int can only hold a number. You cannot assign the nullable version to the standard version directly because if the value is null, the standard int would crash or be undefined.
Example of Mistake
public void ProcessID(int? optionalId)
{
// ⛔️ Error CS0029: Cannot implicitly convert type 'int?' to 'int'
int safeId = optionalId;
}
Solution: Handle the Null
You must decide what to do if the value is null. Typically, you use the null-coalescing operator (??) to provide a fallback.
public void ProcessID(int? optionalId)
{
// ✅ Correct: Use 'optionalId', but if it is null, use 0 instead.
int safeId = optionalId ?? 0;
// Alternative:
// int safeId = optionalId.GetValueOrDefault();
}
Scenario 4: Collections and Arrays
In C#, a List<T> and an Array T[] are different types. You cannot assign one directly to the other.
Example of Mistake
using System.Collections.Generic;
public void ProcessList()
{
List<string> namesList = new List<string> { "Alice", "Bob" };
// ⛔️ Error CS0029: Cannot implicitly convert type 'List<string>' to 'string[]'
string[] namesArray = namesList;
}
Solution: Conversion Methods
Use standard LINQ methods like .ToArray() or .ToList() to transform the collection.
using System.Collections.Generic;
using System.Linq; // Required for ToArray()
public void ProcessList()
{
List<string> namesList = new List<string> { "Alice", "Bob" };
// ✅ Correct: Create a new array from the list
string[] namesArray = namesList.ToArray();
}
Conclusion
CS0029 is the compiler telling you: "These shapes don't fit together automatically."
- Check Precision: If converting numbers, use a cast
(int)if you accept data loss. - Check Text: If converting strings, use
.Parse()orConvert. - Check Nulls: If converting nullable types, use
??to provide a default value. - Check Types: If converting collections, use
.ToList()or.ToArray().