Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# ternary operator giving error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

I have the following C# code -

using System;

class Program 
{
    static void Main()  
    {
        int number = 1;
        int isNumber10;
        (number==10)?(isNumber10=1):(isNumber10=0);
        Console.WriteLine(isNumber10);
    }
}

On compilation it gives me the error -

error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

In C I am used to the following code -

#include <stdio.h>
int main(){
    int isNumber10;
    int number = 1;
    (number==10)?(isNumber10=1):(isNumber10=0);
    printf("%d\n",isNumber10);
    return 0;
}

And this code is running perfectly.

Now, the two programs are exactly same. So why is it running in C but not in C#?

like image 634
puregeek Avatar asked Dec 21 '17 01:12

puregeek


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What is & operator in C?

The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


1 Answers

The ternary operator is an expression that cannot be used as a statement. In contrast, an assignment is an expression that can be promoted to a statement. (hence the error message referring to "assignment" expressions)

What you want is:

isNumber10 = number == 10 ? 1 : 0;

Here you are using the ?: operator as an expression that resolves to one of the two values (1 or 0) and is then assigned to the variable isNumber10.

To have a little fun with this, if you created this method:

public void M<T>(T value)
{
}

And you called it like:

M((number==10)?(isNumber10=1):(isNumber10=0));

It would work fine. The problem is just that the C# grammar does not allow most naked expressions to live in a context in which the value of the expression is not consumed. (Remember, the defining difference between an expression and a statement is that an expression produces an expression, but a statement does not) Some expressions are allowed outside of this guidance -- for example invoking a method that returns a value. These become in the technical parlance an "expression statement". And usefully, the only candidates for expressions that can be promoted to statements are exactly delineated by the error message in your question's title.

Most of us think of assignments as statements, but it is more fundamentally an expression. (it returns the value assigned while simultaneously performing the assignment). That's why that empty call to M will actually accomplish what you want. (not that it's very readable)

From your comment, I'll add this comment as part of my answer:

The only error of yours is the simple fact that the C# grammar doesn't allow it. It certainly could, but well, it does not. I'm reminded about how the when operator in SQL is an expression (meaning you can say set i = when x is null then 'A' else 'B') whereas in C# such a usage would be invalid (since the switch statement is not an expression -- it cannot return a value)

like image 145
Kirk Woll Avatar answered Sep 30 '22 23:09

Kirk Woll