Method Call using Ternary Operator

别等时光非礼了梦想. 提交于 2019-11-26 17:49:11

the ternary operator is used to return values and those values must be assigned. Assuming that the methods doThis() and doThat() return values, a simple assignment will fix your problem.

If you want to do what you are trying, it is possible, but the solution isn't pretty.

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

This returns an Action delegate which is then Invoked by the parenthesis. This is not a typical way to achieve this.

Ternary operator must return something. A typical usage is like this:

int x = (a > b) ? a : b;

If you try something like

a + b;

The compiler will complain.

(a > b) ? a - b : b - a;

is basically a shortcut for either "a - b" or "b - a", which are not legitimate statements on their own.

If you really want to invoke void methods in a conditional operator, you can use delegates:

(something ? new Action(DoThis) : DoThat)();

If the methods take parameters, this will become more complicated.
You can either put lambda expressions in the conditional or use Action<T>.

However, this is a very dumb thing to do.

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

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

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()!");

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.

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

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.

skarmats

You should be able to do this, though:

        int a = 5;
        int b = 10;

        var func = a == b ? (Action)doThis : (Action)doThat; // decide which method

        func(); // call it

Not that it's really that useful though.

The conditional operator is an expression that returns a value.
You cannot use it with functions that return void.

Instead, you should use a normal if.

.NET doesn't (easily) support (a readable version of) this for a reason. It's very jankity and makes your code hard to read. Logic trees should be relatively easy to traverse. If I were to walk in to a job and all the code they had used ternary for assigning values, calling methods, etc. I think I would just walk out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!