Can I use a ternary operator with two functions, each returning a void?

In C #, you can use the built-in if condition without a value, in other words, return void?

public void FuncReturningVoid ()
{
    return;
}

public void AnotherFuncReturningVoid()
{
    return;
}

public void Test ()
{
    int a = 1;
    int b = 2;

    // I whish I could to do this:
    a == b ? FuncReturningVoid() : AnotherFuncReturningVoid();

    //would be the same...
    if (a == b)
    {
        FuncReturningVoid();
    }
    else
    {
        AnotherFuncReturningVoid();
    }
}
+4
source share
1 answer

No. Impossible. These are compilation errors:

  • As an operator, you can use only assignment, call, increment, decrement, wait, and new object expressions.
  • The type of conditional expression cannot be determined because there is no implicit conversion between 'void' and 'void'
+6
source

Source: https://habr.com/ru/post/1543566/


All Articles