C# Error CS0173 – Type of conditional expression cannot be determined because there is no implicit conversion between ‘class1’ and ‘class2’

C# Compiler Error

CS0173 – Type of conditional expression cannot be determined because there is no implicit conversion between ‘class1’ and ‘class2’

Reason for the Error

You will receive this error when the conditional operator cannot cast implicitly.

For example, try compiling the below code snippet

public class Hello {
    public static void Main() {
        int input = 1;
        object output = (input == 0) ? null : null;
        return -1;
    }
}

This will result with the error CS0173.

error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between the parameters.

Solution

To fix this error, ensure that there is one and only one implicit conversion exists between either sides. The above code can be fixed by casting the type to object for both the class1 and class 2 as shown below.

public class Hello {
    public static void Main() {
        int input = 1;
        object output = (input == 0) ? (object)null : (object) null;
        return -1;
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...