Skip to content

Commit 765e0cb

Browse files
CopilotBillWagnergewarren
authored
Update CS0173 documentation for C# 9 target-typed conditional expressions (#47885)
* Initial plan * Update CS0173 documentation for C# 9 target-typed conditional expressions Co-authored-by: BillWagner <[email protected]> * Update date and add periods to code comments as requested Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 4a965d2 commit 765e0cb

File tree

1 file changed

+74
-29
lines changed
  • docs/csharp/language-reference/compiler-messages

1 file changed

+74
-29
lines changed
Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Compiler Error CS0173"
33
title: "Compiler Error CS0173"
4-
ms.date: 08/14/2018
4+
ms.date: 08/07/2025
55
f1_keywords:
66
- "CS0173"
77
helpviewer_keywords:
@@ -10,28 +10,71 @@ ms.assetid: eb1797ad-bf62-4e2b-8922-bef4aff36954
1010
---
1111
# Compiler Error CS0173
1212

13-
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'.
1414

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.
1616

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.
1818

19-
## Example
19+
To resolve CS0173, you can:
2020

21-
The following examples generate compiler error CS0173:
21+
- Provide an explicit target type (available in C# 9+):
22+
23+
```csharp
24+
object result = condition ? value1 : value2; // Works in C# 9.0+
25+
```
26+
27+
- Use explicit casting:
28+
29+
```csharp
30+
var result = condition ? (object)value1 : (object)value2;
31+
```
32+
33+
- 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+
class Program
45+
{
46+
static void Main()
47+
{
48+
// CS0173: Type of conditional expression can't be determined
49+
// because there is no implicit conversion between 'int' and 'string'.
50+
var result = 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+
object result = true ? 100 : "ABC"; // OK in C# 9.0+
60+
61+
// Or use explicit casting (all versions).
62+
var result = true ? (object)100 : (object)"ABC";
63+
```
64+
65+
### Example 2: Class conversion example
66+
67+
The following example shows CS0173 with custom classes:
2268

2369
```csharp
2470
public class C {}
2571

2672
public class A
2773
{
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.
3075
//public static implicit operator A(C c)
3176
//{
32-
// A a = new A();
33-
// a = c;
34-
// return a;
77+
// return new A();
3578
//}
3679
}
3780

@@ -42,33 +85,35 @@ public class MyClass
4285
A a = new A();
4386
C c = new C();
4487

45-
// The following line causes CS0173 because there is no implicit
46-
// conversion from a to c or from c to a.
47-
object o = b ? a : c;
88+
// CS0173: No implicit conversion between A and C.
89+
var result = b ? a : c;
4890

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+
object result2 = b ? (object)a : (object)c;
5193

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+
object result3 = b ? a : c; // OK in C# 9.0+.
5496
}
55-
56-
public static void Main()
57-
{
58-
F(true);
59-
}
6097
}
6198
```
6299

100+
### Example 3: Version differences with nullable types
101+
102+
The behavior of conditional expressions has evolved across C# versions:
103+
63104
```csharp
64-
class M
105+
class Program
65106
{
66-
static int Main ()
107+
static void Main()
67108
{
68-
int X = 1;
69-
// The following line causes CS0173.
70-
object o = (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;
72114
}
73115
}
74116
```
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

Comments
 (0)