what is the point of using ternary operators inste

2020-07-07 18:20发布

why make things more complex? why do this:

txtNumerator.Text = 
     txtNumerator.Text == "" ? "0" : txtNumerator.Text;

instead of this:

if txtNumerator.Text="" {txtNumerator.Text="0";}

标签: c#
5条回答
我想做一个坏孩纸
2楼-- · 2020-07-07 18:42

Suppose you wanted to pass either zero or txtNumerator.Text to a method M. How would you do that?

You could say:

string argument;
if (txtNumerator.Text == "")
{
    argument = "0";
}
else
{
    argument = txtNumerator.Text;
}
M(argument);

Or you could say

M(txtNumerator.Text == "" ? "0" : txtNumerator.Text);

The latter is shorter and easier to read.

The larger point here is that statements are useful for their side effects and expressions are useful for their values. If what you want to do is control which of two side effects happens, then use an "if" statement. If what you want to do is control which value gets chosen from two possibilities, then consider using a conditional expression.

UPDATE:

Jenny asks why not just do this?

if (txtNumerator.Text == "")
{
    M("0");
}
else
{
    M(txtNumerator.Text);
}

That's fine if there's just one condition to check. But what if there are, say, four? Now there are sixteen possibilities and writing the "if" statement for it gets messy to say the least:

if (text1.Text == "")
{
    if (text2.Text == "")
    {
        if (text3.Text == "")
        {
            if (text4.Text == "")
            {
                M("1", "2", "3", "4");
            }
            else
            {
                M("1", "2", "3", text4.Text);
            }
        }
        else
        {
            if (text4.Text == "")
            {
                M("1", "2", text3.Text, "4");
            }
            else
            {
                M("1", "2", text3.Text, text4.Text);
            }
        }
        ... about fifty more lines of this.

Instead, you can just say:

M(Text1.Text == "" ? "1" : Text1.Text,
  Text2.Text == "" ? "2" : Text2.Text,
  Text3.Text == "" ? "3" : Text3.Text,
  Text4.Text == "" ? "4" : Text4.Text);
查看更多
叼着烟拽天下
3楼-- · 2020-07-07 18:43

It's one expression, so you can use the result directly in an assignment or a function call without having to duplicate the context in which you're using it. That makes a lot of usage scenarios significantly cleaner to write and read. For example:

int abs = (x >= 0) ? x : -x;

versus

int abs;
if (x >= 0)
    abs = x;
else
    abs = -x;
查看更多
▲ chillily
4楼-- · 2020-07-07 18:43

If you go with the if-then construct, you end up with two assignments to the same variable, in two separate blocks. The ternary form has only one assignment. So there is no need to look at a second block to verify to yourself what both blocks are performing an assignment to the same variable. In that respect I think the ternary form reads better.

On the other hand if C# worked like Ruby and if was an expression, then you could do without the ternary operator and use if-else in that case:

txtNumerator.Text = if (txtNumerator.Text == "" ) "0"; else (txtNumerator.Text);

which I would prefer because then the different syntax of ?: could be dropped.

查看更多
太酷不给撩
5楼-- · 2020-07-07 18:52

It makes the code more readable in some people opinion, a lot of constructs in the language are syntactic sugar (think about do..while, while..do, and for(..)) and in the end of the day you choose whatever works for you (and your team).

I for example think that the above code should be implemented with an extension method:

txtNumerator.SetDefault("0");
查看更多
时光不老,我们不散
6楼-- · 2020-07-07 19:01

There are many standards guides that say not to use the ternary operator. However you could agre that all language feature are unnecessary except for goto and if. I use it when having a multitude of if then elses.

查看更多
登录 后发表回答