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

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...