How to Resolve Error "CS0173: Type of conditional expression cannot be determined because there is no implicit conversion" in C#
The Compiler Error CS0173 is a Type Inference error related to the Ternary Conditional Operator (? :). The message reads: "Type of conditional expression cannot be determined because there is no implicit conversion between 'TypeA' and 'TypeB'".
In C#, when you use the syntax condition ? A : B, the compiler must determine a single data type for the result. It attempts to do this by checking if A can be converted to B, or if B can be converted to A. If neither conversion is possible implicitly, the compiler stops and raises CS0173, even if both types share a common base class (ancestor).
This guide explains why this inference fails and how to fix it using explicit casts.
Understanding Ternary Type Inference
The compiler uses a strict set of rules to determine the type of condition ? TrueValue : FalseValue.
It looks at TrueValue (Type A) and FalseValue (Type B).
- If A = B, the result is A.
- If A implicitly converts to B, the result is B.
- If B implicitly converts to A, the result is A.
- If neither converts to the other: Error CS0173.
Critically, the compiler (when using var) generally does not automatically search for the "nearest common ancestor" (like object or a Base Class) unless you give it a hint.
Scenario 1: Incompatible Primitive Types
This is the most obvious cause. You are trying to return an int in one case and a string in the other.
Example of error
public void Process(bool returnNumber)
{
// ⛔️ Error CS0173: There is no implicit conversion between 'int' and 'string'.
// The compiler asks: "Is the result a number? Or text? I can't choose."
var result = returnNumber ? 10 : "Ten";
}
Solution: Standardize the Type
You must ensure both options return the same data type. Usually, this means converting the data to a common format (like string or object).
public void Process(bool returnNumber)
{
// ✅ Correct: Convert the integer to a string.
// Now both sides are 'string'.
string result = returnNumber ? 10.ToString() : "Ten";
System.Console.WriteLine(result);
}
Scenario 2: Sibling Classes (Common Base Class)
This is the classic Object-Oriented Programming trap. You have two classes, Cat and Dog. Both inherit from Animal. You assume the compiler will automatically infer Animal as the result. It often won't.
Example of error
public class Animal { }
public class Cat : Animal { }
public class Dog : Animal { }
public class Program
{
static void Main()
{
bool likesCats = true;
// ⛔️ Error CS0173: No implicit conversion between 'Cat' and 'Dog'.
// A Cat is not a Dog. A Dog is not a Cat.
// The compiler does not automatically default to 'Animal'.
var pet = likesCats ? new Cat() : new Dog();
}
}
Solution: Cast to Base Class
You must explicitly cast one (or both) of the operands to the base class. This enables the implicit conversion rule (e.g., Dog implicitly converts to Animal).
public class Program
{
static void Main()
{
bool likesCats = true;
// ✅ Correct: We explicitly cast one side to 'Animal'.
// Now the compiler sees 'Animal' vs 'Dog'.
// Since 'Dog' converts to 'Animal', the result is 'Animal'.
var pet = likesCats ? (Animal)new Cat() : new Dog();
System.Console.WriteLine(pet.GetType().Name);
}
}
Output:
Cat
Target-Typed Conditional (C# 9.0+):
If you define the variable type explicitly (instead of var), modern C# can often infer the type:
Animal pet = likesCats ? new Cat() : new Dog(); works.
CS0173 persists mainly when using var.
Scenario 3: Value Types and Null
A non-nullable value type (like int or double) cannot be null. If one side of the ternary is a number and the other is null, the compiler cannot convert null to int.
Example of error
public void CheckScore(bool hasScore)
{
// ⛔️ Error CS0173: No conversion between 'int' and '<null>'.
// 'int' cannot hold null.
var score = hasScore ? 100 : null;
}
Solution: Cast to Nullable
You must tell the compiler that the integer is actually a Nullable Integer (int?). null can be converted to int?.
public void CheckScore(bool hasScore)
{
// ✅ Correct: Cast the number to 'int?'.
// Now both sides are compatible with 'int?'.
var score = hasScore ? (int?)100 : null;
}
Conclusion
CS0173 asks you to provide a "common denominator" for your types.
- Check Primitives: Are you mixing numbers and strings? Convert them.
- Check Inheritance: Are you mixing
DogandCat? Cast one to(Animal). - Check Nulls: Are you mixing
intandnull? Cast theintto(int?).