C # ?: Expression

I have a function with several if ( THIS IS NOT A TOPICAL CODE )

if(n == 1) m = 1; if(n == 2) m = 2; if(n == 3) m = 3; 

Instead, I wanted to make them all in :: expression:

 (n == 1) ? m = 1; 

But this suggests that expecting ':'

Am I familiar with the expression ?: from C ++, where you can simply write:

 (n == 1) ? m = 1 : 0; 

But 0 is not required here. This is a ridiculous question, and I couldn’t even find the answer on google, as it ignores "?" Like a word.

ANSWER : Too bad that the answer was in the comments. There is no “do nothing” way in this expression, and I have to use if-else or switch. thanks.

+6
source share
8 answers

It looks like you are looking for:

 m = (n == 1) ? 1 : 0; 

What could you then cascade to:

 m = (n == 1) ? 1 : (n == 2) ? 2 : (n == 3) ? 3 : 0; 

Important (for me, anyway), aside:

Why are you asking about this? If so, because you think this form will be more efficient than a series of if , or switch , not . The C # compiler and the .net JIT compiler are really very smart and they will turn your code (hopefully!) Into its most optimal form. Write your code so that it is understandable to yourself or to the developer who needs to support it after how it can be. If the performance you get is unacceptable, try changing it, but measure to determine what works best (given that the new /.net compiler structures may well change what happens).

+14
source

finding a ternary operator in C # will give you relevant results.

usage example will be

 var m = n == 1 ? 1 : 0 
+2
source

May be:

 m = (n == 1) ? 1 : (n == 2) ? 2 : (n == 3) ? 3 : m; 

or

 m = n 

Edit: Simplified:

 variable2 = (variable1 == value) ? variable1 : variable2; 
+2
source

You can write:

 m = (n==1) ? 1 : m; 

But IMO, which is harder to read and uglier than the source code.

 (n == 1) ? m = 1 : 0; 

This is not allowed because C # does not allow arbitrary expressions as an operator. Method calls and assignments are allowed; most other expressions are not.

An operator is executed for its side effects, an expression for its value. Therefore, it is only natural that the outside of the statement has a side effect. ?: never has a side effect, so it is not allowed as an operator.

+1
source

Do you want to:

m = (n == 1) ? 1 : 0;

To nest them all, it would look like this:

m = (n == 1) ? 1 : (n == 2) ? 2 : (n == 3) ? 3 : 0;

But, as you can see, it is much easier to read and understand. This may help add extra brackets, but I think you better use the if-else tree.

+1
source
 m = (n == 1) ? 1 : m 

Means

M is 1 if n == 1, else m

Fyi ? called the Turner operator. Find on MSDN

Respectfully,

+1
source

Try the following:

 m = (n == 1) ? 1 : 0; 
0
source

This is not a problem that needs to be solved with the triple if / else statement - it is certainly an ideal candidate for the switch statement (and using a switch is likely to be much more efficient than using a sequence of triple statements)

If you want to transliterate the if statement into?: Then this is pretty simple:

 if ({condition}) {then-code}; else {else-code}; 

becomes

 {condition} ? {then-code} : {else-code}; 

The only limitation is that the then / else code is the only expression.

The main advantage ?: (with modern compilers) is that it can be built into instructions to significantly compress the source code - sometimes it can help in readability, and sometimes it just serves to obfuscate the meaning of the code - use this with caution.

0
source

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


All Articles