You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Type of conditional expression cannot be determined because there is no implicit conversion between 'class1' and 'class2'
13
+
Type of conditional expression cannot be determined because there is no implicit conversion between 'type1' and 'type2'.
14
14
15
-
Conversions between classes are useful when you want objects of different classes to work with the same code. However, two classes that work together cannot have mutual and redundant conversions, or no implicit conversions. The types of `class1` and `class2` are determined independently, and the more general type is selected as the type of the conditional expression. For more information about how types are determined, see [Conditional operator cannot cast implicitly](https://stackoverflow.com/questions/2215745/conditional-operator-cannot-cast-implicitly/2215959#2215959).
15
+
This error occurs when the compiler can't determine the type of a conditional expression because the two possible return values have no implicit conversion between them. This can happen with classes, value types, or any other types where there's no common type that both can be implicitly converted to.
16
16
17
-
To resolve CS0173, verify that there is one and only one implicit conversion between `class1` and `class2`, regardless of which direction the conversion is in and regardless of which class the conversion is in. For more information, see [User-defined conversion operators](../operators/user-defined-conversion-operators.md) and [Built-in numeric conversions](../builtin-types/numeric-conversions.md).
17
+
Starting with C# 9, *target-typed conditional expressions* allow the compiler to use the target type (the type being assigned to) to resolve ambiguity in some cases. However, CS0173 still occurs when using `var` or when there's no target type to guide the compiler.
18
18
19
-
## Example
19
+
To resolve CS0173, you can:
20
20
21
-
The following examples generate compiler error CS0173:
21
+
- Provide an explicit target type (available in C# 9+):
22
+
23
+
```csharp
24
+
objectresult=condition?value1:value2; // Works in C# 9.0+
- Ensure there's an implicit conversion between the types by adding conversion operators or using compatible types.
34
+
35
+
For more information, see [User-defined conversion operators](../operators/user-defined-conversion-operators.md) and [Built-in numeric conversions](../builtin-types/numeric-conversions.md).
36
+
37
+
## Examples
38
+
39
+
### Example 1: CS0173 with `var` (all C# versions)
40
+
41
+
The following example generates CS0173 because `var` provides no target type for the compiler to use:
42
+
43
+
```csharp
44
+
classProgram
45
+
{
46
+
staticvoidMain()
47
+
{
48
+
// CS0173: Type of conditional expression can't be determined
49
+
// because there is no implicit conversion between 'int' and 'string'.
50
+
varresult=true?100:"ABC";
51
+
}
52
+
}
53
+
```
54
+
55
+
To fix this, provide an explicit type:
56
+
57
+
```csharp
58
+
// Fix: Use explicit target type (C# 9.0+).
59
+
objectresult=true?100:"ABC"; // OK in C# 9.0+
60
+
61
+
// Or use explicit casting (all versions).
62
+
varresult=true? (object)100: (object)"ABC";
63
+
```
64
+
65
+
### Example 2: Class conversion example
66
+
67
+
The following example shows CS0173 with custom classes:
22
68
23
69
```csharp
24
70
publicclassC {}
25
71
26
72
publicclassA
27
73
{
28
-
// The following code defines an implicit conversion operator from
29
-
// type C to type A.
74
+
// Uncomment to add implicit conversion from C to A.
30
75
//public static implicit operator A(C c)
31
76
//{
32
-
// A a = new A();
33
-
// a = c;
34
-
// return a;
77
+
// return new A();
35
78
//}
36
79
}
37
80
@@ -42,33 +85,35 @@ public class MyClass
42
85
Aa=newA();
43
86
Cc=newC();
44
87
45
-
// The following line causes CS0173 because there is no implicit
46
-
// conversion from a to c or from c to a.
47
-
objecto=b?a:c;
88
+
// CS0173: No implicit conversion between A and C.
89
+
varresult=b?a:c;
48
90
49
-
//To resolve the error, you can cast a and c.
50
-
//object o = b ? (object)a : (object)c;
91
+
//Fix: Cast to common base type.
92
+
objectresult2=b? (object)a: (object)c;
51
93
52
-
//Alternatively, you can add a conversion operator from class C to
53
-
// class A, or from class A to class C, but not both.
94
+
//Or in C# 9.0+, provide target type.
95
+
objectresult3=b?a:c; // OK in C# 9.0+.
54
96
}
55
-
56
-
publicstaticvoidMain()
57
-
{
58
-
F(true);
59
-
}
60
97
}
61
98
```
62
99
100
+
### Example 3: Version differences with nullable types
101
+
102
+
The behavior of conditional expressions has evolved across C# versions:
103
+
63
104
```csharp
64
-
classM
105
+
classProgram
65
106
{
66
-
staticintMain()
107
+
staticvoidMain()
67
108
{
68
-
intX=1;
69
-
// The following line causes CS0173.
70
-
objecto= (X==0) ?null:null;
71
-
return-1;
109
+
// This example shows how different C# versions handle the same code.
110
+
111
+
// In C# 8.0 and earlier: CS8957 (feature not available).
112
+
// In C# 9.0+: Compiles successfully.
113
+
object?result= (1==0) ?null:null;
72
114
}
73
115
}
74
116
```
117
+
118
+
> [!NOTE]
119
+
> Starting with C# 9, target-typed conditional expressions allow the compiler to use the assignment target's type to resolve type ambiguity. In earlier versions, you might see error CS8957 instead of CS0173 when using language features not available in the current language version.
0 commit comments