ebook img

How to Code .NET PDF

231 Pages·2006·1.564 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview How to Code .NET

CYAN YELLOW MAGENTA BLACK PANTONE 123 CV BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN.NET Companion eBook Author of How to Code .NET: Tips and Tricks for Coding Available Ajax Patterns and Best Practices .NET 1.1 and .NET 2.0 Applications Effectively Ajax and REST Recipes:A H Problem-Solution Approach Dear Reader, Foundations of Object- Oriented Programming Like you, I am a coder, architect, and developer. People who are coders, archi- o How to Using .NET 2.0 Patterns tects, or developers strive to do their best, and if given the choice they will always do something correctly. Of course, this begs the question: Why do we w have so many bugs in our code? I think the main reason for buggy code is that we are all short on time. We don’t have the luxury of investigating new Framework features fully or exploring innovative new techniques as thoroughly as we would like, because we’re all t watching the clock. That means our code has bugs—the new Framework feature o we implemented doesn’t work quite as expected, and the new best practice we Code .NET put in place doesn’t seem to work the same way for every input. These bugs are frustrating and can often be very difficult to solve. C This book is a response to that problem. In it I have investigated and recorded my experiences of a wide range of .NET Framework features. They’re arranged in simple, bite-sized sections dedicated to problem solving, informing you of o little-known functionality and keeping you up to date with the latest design thinking. It’s a road map to your more effective use of the .NET Framework. d For example, the .NET Framework 2.0 introduced the yield keyword. On the face of it, this is a really cool new piece of functionality that we’d all like to use. e But what’s it really like? Is it buggy? Is it going to be the future of all iterators? This book digs into these questions and more to provide you with the answers Tips and Tricks for Coding .NET 1.1 that you need. . Christian Gross N and .NET 2.0 Applications Effectively E T Join online discussions: forums.apress.com SOURCECODEONLINE Companion eBook FOR PROFESSIONALS BY PROFESSIONALS™ www.apress.com See last page for details Christian Gross ISBN 1-59059-744-3 on $10 eBook version 90000 G r o s s Shelve in .NET 6 89253 59744 6 9 781590 597446 User level: Beginner–Intermediate this print for content only—size & color not accurate 7" x 9-1/4" / CASEBOUND / MALLOY (0.625 INCH BULK -- 232 pages -- 60# Thor) 7443FM.qxd 9/21/06 10:46 PM Page i How to Code .NET Tips and Tricks for Coding .NET 1.1 and .NET 2.0 Applications Effectively Christian Gross 7443FM.qxd 9/21/06 10:46 PM Page ii How to Code .NET: Tips and Tricks for Coding .NET 1.1 and .NET 2.0 Applications Effectively Copyright © 2006 by Christian Gross All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-744-6 ISBN-10 (pbk): 1-59059-744-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Ewan Buckingham Technical Reviewer: Jason Lefebvre Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Richard Dal Porto Copy Edit Manager: Nicole Flores Copy Editors: Candace English, Nicole Abramowitz Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Gunther Compositor: Gina Rexrode Proofreader: Linda Seifert Indexer: Michael Brinkman Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.comin the Source Code/ Download section. 7443FM.qxd 9/21/06 10:46 PM Page iii Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii ■ CHAPTER 1 Testing Your Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 .NET Runtime- and Framework-Related Solutions . . . . . . . 31 ■ CHAPTER 3 Text-Related Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 ■ CHAPTER 4 C# Coding Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 iii 7443FM.qxd 9/21/06 10:46 PM Page iv 7443FM.qxd 9/21/06 10:46 PM Page v Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii ■ CHAPTER 1 Testing Your Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Quick Notes About TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Getting Started with TDD and NUnit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Writing Tests Using Contexts and Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Writing Tests for Code Pieces That Have No Tests or Few Tests. . . . . . . . 11 Writing Tests for Code Pieces That Don’t Give Information Back . . . . . . . 19 Verifying the Correctness of an Object Instance Without Having Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 ■ CHAPTER 2 .NET Runtime- and Framework-Related Solutions . . . . . . . 31 Keeping Value Types and Reference Types Straight . . . . . . . . . . . . . . . . . . 31 Using Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Versioning Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Loading and Unloading Assemblies Dynamically. . . . . . . . . . . . . . . . . . . . . 47 Loading Assemblies Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Loading and Unloading Assemblies Dynamically. . . . . . . . . . . . . . . . 52 Implementing GetHashCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Thinking of .NET Generics as Black Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . 72 Figuring Out What Generic Methods Do. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Using the new and class Keywords with .NET Generics. . . . . . . . . . . . . . . 82 ■ CHAPTER 3 Text-Related Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Converting a String to an Array and Vice Versa . . . . . . . . . . . . . . . . . . . . . . 85 Parsing Numbers from Buffers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Processing Plain-Vanilla Numbers in Different Cultures. . . . . . . . . . 89 Managing the Culture Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 v 7443FM.qxd 9/21/06 10:46 PM Page vi vi ■ CONTENTS When to Use StringBuilder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Finding a Piece of Text Within a Text Buffer . . . . . . . . . . . . . . . . . . . . . . . . 101 Always Implement ToString. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Using a Disposable Type to Find Multiple Text Pieces and Iterate the Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Making ToString Generate Structured Output. . . . . . . . . . . . . . . . . . . . . . . 110 ■ CHAPTER 4 C# Coding Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 What Does the Yield Keyword Really Generate? . . . . . . . . . . . . . . . . . . . . 117 Using Inheritance Effectively. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Implementing Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Naming Conventions for a Namespace,a Class,and an Interface. . . . . 135 Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Class and Interface Identifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Understanding the Overloaded Return Type and Property. . . . . . . . . . . . 139 Nullable Types:A Null Is Not Always a Null. . . . . . . . . . . . . . . . . . . . . . . . . 145 Abstract-Class Bridge-Pattern Variation . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Nested Private-Class Bridge-Pattern Variation. . . . . . . . . . . . . . . . . . . . . . 151 Dealing with Marker Interfaces or Base Classes. . . . . . . . . . . . . . . . . . . . 153 Editing Text Using the Command Pattern. . . . . . . . . . . . . . . . . . . . . . 154 Marker Interfaces and Their Dependencies . . . . . . . . . . . . . . . . . . . 156 How Marker Interfaces Dependencies Are Implemented. . . . . . . . 157 A Null Value Is Not Always a Null State . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 The Essentials of the Factory Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 The Classical Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 More Sophisticated Factory Implementations . . . . . . . . . . . . . . . . . 173 Don’t Expose a Class’s Internal State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Designing Consistent Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Immutable Types Are Scalable Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Understanding and Using Functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 The Comparer Functor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 The Closure Functor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 The Predicate Functor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 The Transformer Functor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Functors in Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Avoiding Parameters That Have No Identity . . . . . . . . . . . . . . . . . . . . . . . . 205 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 7443FM.qxd 9/21/06 10:46 PM Page vii About the Author Many people say that by looking at a person’s dog, you can tell what the person is like. Well, the picture of me is my dog Louys, an English Bulldog. And yes, my English Bulldog and I have many common characteristics. But what about my biography? It’s pretty simple: I am guy who has spent oodles of time strapped to a chair debugging and taking apart code. In fact, I really enjoy this business we call software development. I have ever since I learned how to peek and poke my first bytes. I have written various books, including Ajax and REST Recipes:A Problem-Solution Approach, Foundations of Object- Oriented Programming Using .NET 2.0 Patterns, and A Programmer’s Introduction to Windows DNA, all available from Apress. These days I enjoy coding and experimenting with .NET, as it is a fascinating environ- ment. .NET makes me feel like a kid opening a present on Christmas morning. You had an idea what the gift was, but you were not completely sure. And with .NET there is no relative giving you socks or a sweater. It’s excitement all the way! vii 7443FM.qxd 9/21/06 10:46 PM Page viii 7443FM.qxd 9/21/06 10:46 PM Page ix About the Technical Reviewer ■ JASON LEFEBVREis vice president and founding partner of Intensity Software, Inc. (http://www.intensitysoftware.com), which specializes in providing custom Microsoft .NET applications, IT consulting services, legacy system migration, and boxed software products to a rapidly growing set of clients. Jason has been using Microsoft .NET since its Alpha stages in early 2000 and uses Visual Studio and the Microsoft .NET Framework daily while creating solutions for Intensity Software’s clients. Jason has been a participating author for a number of books and has written numerous articles about Microsoft .NET-related topics. ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.