什么是使用三元运营商,而不是如果那么点? [英] what is the point of using ternary operators instead of IF THEN?

查看:176
本文介绍了什么是使用三元运营商,而不是如果那么点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么让事情变得更加复杂?为什么这样做:

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



而不是这样的:

 如果txtNumerator.Text ={txtNumerator.Text =0;} 


解决方案

假设你想为零或txtNumerator.Text传递给方法M.你会怎么做呢?



你可以说:

 字符串参数; 
如果(txtNumerator.Text ==)
{
论点=0;
}
,否则
{
参数= txtNumerator.Text;
}
M(参数);

或者你也可以说

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



后者是更短,更易于阅读。



这里的更大的问题是,语句的是有用的他们的副作用的和的表达式的是为他们的值有用。如果你想要做的是两个副作用,这恰好控制,然后使用if语句。如果你想要做的是它的价值会从两种可能选择的控制,那么可以考虑使用条件表达式



更新:



珍妮问为什么不这样做呢?

 如果(txtNumerator.Text ==)
{
M(0);
}
,否则
{
M(txtNumerator.Text);
}

这很好,如果有只是一个条件检查。但是,如果有,说,四?现在有十六可能性,写了如果它就会变得混乱声明,至少可以说:

 如果(text1.Text ==)
{
如果(text2.Text ==)
{
如果(text3.Text ==)
{
如果(text4.Text ==)
{
M(1,2,3,4);
}
,否则
{
M(1,2,3,text4.Text);
}
}
,否则
{
如果(text4.Text ==)
{
M(1, 2,text3.Text,4);
}
,否则
{
M(1,2,text3.Text,text4.Text);
}
}
...大约五十多本线。



相反,你可以说:

  M(Text1.Text ==1?:Text1.Text,
Text2.Text ==2:Text2.Text,
Text3.Text ==3:Text3.Text,
Text4.Text ==4:??Text4.Text);


why make things more complex? why do this:

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

instead of this:

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

解决方案

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);

这篇关于什么是使用三元运营商,而不是如果那么点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆