Solved

C# return query

Posted on 2006-11-06
2
272 Views
Last Modified: 2010-04-16
I have noticed the use of this code in an application that I am developing can anybody tell me what it means when you have the ? and :

ie return (returnValue == 0 ? true : false);

Does it act like an if else statement??

Many thanks

Cragly
0
Comment
Question by:Cragly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
AngryBinary earned 500 total points
ID: 17881835
This is an abbreviated conditional syntax. The expression preceding the question mark is evaluated. If it is true, the expression preceding the colon is returned. Otherwise, the expression following the colon is returned. In other words,

return (returnValue == 0 ? true : false)

is the same as

if (returnValue == 0) {
    return true;
} else {
    return false;
}

Incidentally, this expression is mildly redundant, especially considering that this abbreviated syntax slightly reduces the codes readability. Even though I would consider the 'if/else' to be the preferred syntax, since it is easier to read, an equivalent shorthand expression would be:

return (returnValue == 0);

which would return true or false, depending on the value of returnValue.

Cheers,
Randall
0
 

Author Comment

by:Cragly
ID: 17881994
Cheers many thanks for your help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question