03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

118 ❘ CHAPTER 5 Operators<br />

Exercises<br />

1. Can you use both the pre- and post-increment operators on the same variable as in ++x++<br />

If you can’t figure it out, try it and try to understand what Visual Studio tells you about it.<br />

2. Sometimes the conditional and null-coalescing operators can make the code confusing, particularly<br />

if their operands are complicated expressions. Rewrite the following code to use if<br />

statements instead of : and .<br />

amountLabel.ForeColor = (amount < 0) Color.Red : Color.Blue;<br />

Customer orderedBy = customer new Customer();<br />

3. In the section “Comparison Operators” the code for the overloaded == operator does not<br />

check whether both operands are null. Why does it not need to do that<br />

4. Create a subtraction operator for the Complex class described in this chapter.<br />

5. Create a Fraction class and define the * and / operators for it.<br />

6. Create a conversion operator to convert Fraction to double. Is this a widening or<br />

narrowing conversion<br />

7. Create a > operator for the Fraction class.<br />

8. Create an == operator for the Fraction class.<br />

9. Calculate the result of each of the following statements.<br />

a. 1 + 2 * 3 - 4 / 5<br />

b. 9 * 5 / 10<br />

c. 2 * 5 / 10<br />

d. 2 / 10 * 5<br />

e. 12 / 6 * 4 / 8<br />

10. Add parentheses to the following expressions to make them true.<br />

a. 4 * 4 - 4 / 4 + 4 = 19<br />

b. 4 * 4 - 4 / 4 + 4 = 16<br />

c. 4 * 4 - 4 / 4 + 4 = 11<br />

d. 4 * 4 - 4 / 4 + 4 = 4<br />

e. 4 * 4 - 4 / 4 + 4 = 0<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!