Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 20th, 2011, 02:14 PM
Registered User
 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch11 - Try it out Ex5 - Comparer Default

Would like to make sure I understand the concept behind the line of code in the class PersonComparerName:

Code:
public static IComparer Default = new PersonComparerName();
As I understand it - it is for "ease of use" in that it allows a short cut to instantiating an instance of IComparer (based on an instance of PersonComparerName via polymorphism) in this case for use in the line in program.cs:

Code:
list.Sort(PersonComparerName.Default);
One could also eliminate that line (in PersonComparerName above) and then either do something in program.cs like:

Code:
IComparer PersonCompareNameIComparer = new PersonComparerName();
list.Sort(PersonCompareNameIComparer);
or even :

Code:
list.Sort(new PersonComparerName());
(they both seem to work for me anyway).

Thanks for any additional clarification or insights on this.

stuartho
 
Old December 29th, 2011, 05:51 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

if you use a "new PersonComparerName()" every time you're going to sort it that way you end up with multiple instances of PersonComparerName. If you are using "PersonComparerName.Default", only one instance of PersonComparerName is used.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel





Similar Threads
Thread Thread Starter Forum Replies Last Post
Database comparer hema123 VB How-To 0 November 5th, 2008 02:43 AM
ch11 msxml3 code prob humayunlalzad BOOK: Beginning XML 3rd Edition 1 May 19th, 2007 02:38 AM
Problem using mail() from Ch11 nodster Beginning PHP 2 May 14th, 2007 08:44 AM
Ch11 Exerc 3 Appendix A problem VictorVictor BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 February 16th, 2006 03:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.