Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about properties block

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello I am new to C# and I am going through some examples.

I ran across this example for passing data between forms and I am not quite sure with the extra stuff after the return (memberVariable)

Specifically the == 0 ? 0 : totalHoursDecimal / numOfChargeInteger; part? Is there another way to write this same line of code?

Thanks for the help in advanced.

Code:
        public decimal avgCostDecimal
        {
            get
            {
                return numOfChargeInteger == 0 ? 0 : totalHoursDecimal / numOfChargeInteger;
            }
        }
 
it's syntax sugar for
Code:
if(numberOfChargeInteger == 0)
{
   return 0;
}
return totalHoursDecimal / numberOfChargeInteger;

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
The ?: is called the conditional operator - here's a snippet from msdn:

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. The conditional operator is of the form
Code:
    condition ? first_expression : second_expression;


Hope it helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top