方法调用使用三元运算符 [英] Method Call using Ternary Operator

查看:138
本文介绍了方法调用使用三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然新概念玩弄,我遇到了三元运算符和它的美丽。用它玩了一会儿后,我决定测试一下它的极限。

While playing around with new concepts, I came across the Ternary Operator and it's beauty. After playing with it for a while, I decided to test its limits.

不过,我的乐趣很快结束了,当我不能得到code的某一行编译。

However, my fun was ended quickly when I couldn't get a certain line of code to compile.

int a = 5;
int b = 10;
a == b ? doThis() : doThat() 

    private void doThis()
    {
        MessageBox.Show("Did this");
    }
    private void doThat()
    {
        MessageBox.Show("Did that");
    }

这行给了我两个错误:

Error   1   Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Error   2   Type of conditional expression cannot be determined because there is no implicit conversion between 'void' and 'void'

我以前从未使用过三元运算符来决定调用哪个方法,我也不知道,如果它甚至有可能。我就像一个单行的想法如果else语句的方法调用。

I have never used a Ternary Operator to decide which method to be called, nor do I know if it is even possible. I just like the idea of a one-line If Else Statement for method calling.

我已经做了一些研究,我无法找到任何人这样做的任何的例子,所以我想我可能是希望的东西是无法提供的。

I have done a bit of research and I cannot find any examples of anyone doing this, so I think I might be hoping for something that is cannot provide.

如果这是可能的,请赐教我的种种劣迹,这是不可能的,有另一种方式?

If this is possible, please enlighten me in my wrong doings, and it isn't possible, is there another way?

推荐答案

为什么上面的语句不工作是由其他用户提供有效的没有回答我的问题,真正的原因。

The reason why the above statement does not work was provided by the other users and effectively did not answer my true question.

周围的一些更多的演出之后,我想通了,你可以使用该运营商做上述表示的,但它会导致一些不良code。

After playing around some more, I figured out that you CAN use this operator to do the above statement, but it results in some bad code.

如果我要改变上述声明;

If I were to change the above statement to;

int a = 5;
int b = 10;
int result = a == b ? doThis() : doThat(); 

private int doThis()
{
    MessageBox.Show("Did this");
    return 0;
}
private int doThat()
{
    MessageBox.Show("Did that");
    return 1;
}

这code编译并执行它应有的方式。但是,如果这些方法并没有原本打算返回任何东西,并在code引用的其他领域,你现在有每次调用这些方法来处理返回的对象。

This code will compile and execute the way it should. However, if these methods were not originally intended to return anything, and referenced other areas in the code, you now have to handle a return object each time to call these methods.

否则,您现在可以使用一个三元操作的单行法选择器,甚至知道它使用结果堪称下一行哪个方法。

Otherwise, you now can use a ternary operator for a one-line method chooser and even know which method it called in the next line using the result.

int result = a == b ? doThis() : doThat();

if (result == 0)
   MessageBox.Show("You called doThis()!");

现在,这个code是完全没有意义的,可以由若否则很容易做到,但我只是想知道,如果这是可以做到的,你必须做的就是它的工作是什么。

Now, this code is absolutely pointless and could be easily done by a If Else, but I just wanted to know if it could be done, and what you had to do to get it to work.

现在,我知道,你可以有效地在这些方法返回的任何类型的,这可能会变得有点更加有用。它可以被看作是坏的编码实践,但可能它从来就变得情况下非常有用。

Now that I know that you can effectively return any type in these methods, this might become a little more useful. It may be considered a "Bad Coding Practice" but might become very useful in situations it was never MEANT for.

您可以基于任何条件可以访问一个对象或另一个,并且可能是在code的一行非常有用的。

You could get access to one object or another based on any condition and that might be very useful in one line of code.

UserPrivileges result = user.Group == Group.Admin ? GiveAdminPrivileges() : GiveUserPrivileges();

private UserPrivileges GiveAdminPrivileges()
{
      //Enter code here
      return var;
}
private UserPrivileges GiveUserPrivileges()
{
      //Enter code here
      return var;
}

当然,这可以用IF语句完成,但我觉得用三元运算符用于其他用途,使编程的乐趣。现在,这可能不是一个IF Else语句高效,在这种情况下,我绝不会用这个。

Sure, this can be done by an If Statement, but I think that using the Ternary Operator for other uses makes programming fun. Now, this may not be as efficient as an If Else statement, in which case, I would never use this.

这篇关于方法调用使用三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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