Description
What is good code? Writing good code is really a question about what the code is trying to solve. (And good code is not to be confused with patterns because not all pieces of good code are patterns.) We debate about good code because there is not just a single piece of good code, but so many good pieces of code. And each good piece of code depends on the context in which it is used.How to Code .NET: Tips and Tricks for Coding .NET 1.1 and .NET 2.0 Applications Effectively provides solutions to certain problems. That is, specific problems. This book provides detailed, authoritative explanations of good .NET coding techniques. It's based on award-winning material that author Christian Gross has previously presented at conferences throughout the U.S. and Europe. What's more, the author is at the forefront of the .NET technology wave and an acknowledged expert on the subject of .NET coding style and techniques.
Table of Contents
* Testing Your Code
* .NET Runtime- and Framework-Related Solutions
* Text-Related Solutions
* C# Coding Solutions
CONTENTS:
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
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
Published
Oct 2006
Publisher
APRESS ACADEMIC
ISBN
9781590597446
Pages
216




Static Book Details Index Page - Click Here to go to Computer Manuals Website