13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

8 CHAPTER 1 The chang<strong>in</strong>g face of <strong>C#</strong> developmentThe properties now don’t have any code (or visible variables!) associated with them, andwe’re build<strong>in</strong>g the hard-coded list <strong>in</strong> a very different way. With no “name” and “price”variables to access, we’re forced to use the properties everywhere <strong>in</strong> the class, improv<strong>in</strong>gconsistency. We now have a private parameterless constructor for the sake of the newproperty-based <strong>in</strong>itialization. In this example, we could actually have removed the publicconstructor completely, but it would make the class less useful <strong>in</strong> the real world.Figure 1.1 shows a summary of how our Product type has evolved so far. I’ll <strong>in</strong>cludea similar diagram after each task, so you can see the pattern of how <strong>C#</strong> 2 and 3 improvethe code.<strong>C#</strong> 1Read-only propertiesWeakly typed collections<strong>C#</strong> 2Private property "setters"Strongly typed collections<strong>C#</strong> 3Automatically implementedpropertiesEnhanced collection andobject <strong>in</strong>itializationFigure 1.1 Evolution of the Product type, show<strong>in</strong>g greater encapsulation, stronger typ<strong>in</strong>g, and easeof <strong>in</strong>itialization over timeSo far the changes are relatively m<strong>in</strong>imal. In fact, the addition of generics (theList syntax) is probably the most important part of <strong>C#</strong>2, but we’ve onlyseen part of its usefulness so far. There’s noth<strong>in</strong>g to get the heart rac<strong>in</strong>g yet, but we’veonly just started. Our next task is to pr<strong>in</strong>t out the list of products <strong>in</strong> alphabetical order.That shouldn’t be too hard…1.1.2 Sort<strong>in</strong>g products by nameThe easiest way of display<strong>in</strong>g a list <strong>in</strong> a particular order is to sort the list and then runthrough it display<strong>in</strong>g items. In .NET 1.1, this <strong>in</strong>volved us<strong>in</strong>g ArrayList.Sort, and <strong>in</strong>our case provid<strong>in</strong>g an IComparer implementation. We could have made the Producttype implement IComparable, but we could only def<strong>in</strong>e one sort order that way, andit’s not a huge stretch to imag<strong>in</strong>e that we might want to sort by price at some stage aswell as by name. List<strong>in</strong>g 1.4 implements IComparer, then sorts the list and displays it.List<strong>in</strong>g 1.4 Sort<strong>in</strong>g an ArrayList us<strong>in</strong>g IComparer (<strong>C#</strong> 1)class ProductNameComparer : IComparer{public <strong>in</strong>t Compare(object x, object y){Product first = (Product)x;Product second = (Product)y;return first.Name.CompareTo(second.Name);}}...ArrayList products = Product.GetSampleProducts();products.Sort(new ProductNameComparer());Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!