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.

668 ❘ APPENDIX A Solutions to Exercises<br />

5. You could include the following using directives to define the aliases. (You could use different<br />

names for the aliases.)<br />

using Ord = OrderTracker.OrderTools.OrderClasses;<br />

using Ful = OrderTracker.CustomerTools.Fulfillment;<br />

Code in the Algorithms namespace could then use the following code to define objects with<br />

those aliases.<br />

public Ord.Order order1;<br />

public Ful.Order order2;<br />

6. You could define an alias for the OrderTracker.CustomerTools.Fulfillment class as in<br />

Exercise 5. The code could use the other Order class without any namespace or alias. The<br />

following code shows how the Customer class could define both kinds of objects.<br />

public Order order1;<br />

public Ful.Order order2;<br />

7. The Billing namespace doesn’t define any classes with names that are used elsewhere in<br />

the hierarchy, so you can add the following using directive.<br />

using OrderTracker.CustomerTools.Billing;<br />

Now all code can use the Invoice class without including any namespace information.<br />

8. The most obvious improvement would be to rename the classes so that there are no duplicate<br />

names. Perhaps you could change the name of the Fulfillment namespace’s Order<br />

class to Shipment or PackingList (depending on the purpose of that class).<br />

A second improvement would be to flatten the namespace hierarchy. The hierarchy doesn’t<br />

actually help developers keep the code separate. You can do that just as easily by placing<br />

different pieces of code in separate modules.<br />

Often namespace hierarchies arise because pieces of an application are implemented in different<br />

libraries. The libraries have different namespaces to protect them from name collisions<br />

with the other libraries. In this example, however, it’s unlikely that each namespace represents<br />

a separate library. For example, it’s unlikely that the Fulfillment and Billing code can<br />

work without the Customer class, so they probably weren’t developed as separate libraries.<br />

A final simplification might be to merge the two Order classes into a single class. Whether<br />

that would be better than leaving them as two separate classes depends on how much they<br />

overlap. If they are practically the same class, then merging them would simplify the application.<br />

If they serve different purposes, then it may be better to keep them as separate classes.<br />

Chapter 14<br />

1. The PalindromeChecker example program uses the following code to do this. The key code<br />

is highlighted in bold.<br />

private void palindromeTextBox_TextChanged(object sender, EventArgs e)<br />

{<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!