Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4233926
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:17:00+00:00 2026-05-21T02:17:00+00:00

While playing around with new concepts, I came across the Ternary Operator and its

  • 0

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

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

This line gives me two errors:

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'

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 impossible.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:17:00+00:00Added an answer on May 21, 2026 at 2:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing around new c++ standard. I write a test to observe behavior of
i was playing around on our dev server for a while for a new
While playing around with regexps in Scala I wrote something like this: scala> val
There is an eval() function in Python I stumbled upon while playing around. I
I'm playing around with building a new web application using DB4O - piles of
I was playing around with a bunch of casting operators a while ago, which
While playing around with one-to-one associations in castle activerecord I stumbled upon the following
I was working on a project and, while playing around with the code, I
I'm new to Perl and playing around screen scraping and regex. I'm trying to
I'm new to OpenGL. I'm playing around with JOGL. I have a .obj model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.