Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Conditional Operator Not a Statement?

Tags:

I have a simple little code fragment that is frustrating me:

HashSet<long> groupUIDs = new HashSet<long>(); groupUIDs.Add(uid)? unique++ : dupes++; 

At compile time, it generates the error:

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

HashSet.Add is documented to return a bool, so the ternary (?) operator should work, and this looks like a completely legitimate way to track the number of unique and duplicate items I add to a hash-set.

When I reformat it as a if-then-else, it works fine.

Can anyone explain the error, and if there is a way to do this as a simple ternary operator?

like image 944
abelenky Avatar asked Apr 06 '10 16:04

abelenky


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

According to the error message the ternary operator cannot be used as a statement. You would need to do something like this to turn it into an assignment:

int dummy = groupUIDs.Add(uid)? unique++ : dupes++; 

That being said, I'd recommend to just use if-then-else. It's less confusing because it doesn't involve the creation of "magic" dummy variables...

like image 170
sth Avatar answered Sep 30 '22 18:09

sth


As others have pointed out, the conditional operator is not a legal statement expression. (The legal statement expressions are assignments, calls, increments, decrements and constructions.)

However, there's a stylistic problem here as well. In my opinion, expressions should be useful for their values, and statements should be useful for their side effects. What you are running into is that you have an expression that is only useful for its side effect, and that is a bad code smell.

You have a side effect, so use a conditional statement rather than a conditional expression.

like image 32
Eric Lippert Avatar answered Sep 30 '22 17:09

Eric Lippert