How to Code NET Tips and Tricks for Coding NET 1 1 and NET 2 0 Applications Effectively 1st Edition Christian Gross - Quickly access the ebook and start reading today
How to Code NET Tips and Tricks for Coding NET 1 1 and NET 2 0 Applications Effectively 1st Edition Christian Gross - Quickly access the ebook and start reading today
com
https://ebookname.com/product/how-to-code-net-tips-and-
tricks-for-coding-net-1-1-and-net-2-0-applications-
effectively-1st-edition-christian-gross/
OR CLICK HERE
DOWLOAD EBOOK
https://ebookname.com/product/professional-c-6-and-net-
core-1-0-1st-edition-christian-nagel/
https://ebookname.com/product/database-programming-with-visual-
basic-net-and-ado-net-tips-tutorials-and-code-1st-edition-barker/
https://ebookname.com/product/pro-net-1-1-network-
programming-2nd-edition-christian-nagel/
https://ebookname.com/product/dino-nightmare-page-turners-
level-5-1st-edition-julian-thomlinson/
Pathfinder Kingmaker Bestiary Fifth Edition Jeremy
Corff
https://ebookname.com/product/pathfinder-kingmaker-bestiary-
fifth-edition-jeremy-corff/
https://ebookname.com/product/1587-a-year-of-no-significance-the-
ming-dynasty-in-decline-yale-up-1981-ray-huang/
https://ebookname.com/product/the-bourne-ultimatum-jason-bourne-
book-3-robert-ludlum/
https://ebookname.com/product/state-of-the-world-2007-our-urban-
future-linda-starke/
https://ebookname.com/product/family-life-and-individual-welfare-
in-post-war-europe-britain-and-italy-compared-1st-edition-
stefania-bernini-auth/
Eden 4 1st Edition J. Manoa
https://ebookname.com/product/eden-4-1st-edition-j-manoa/
CYAN YELLOW
MAGENTA BLACK
PANTONE 123 CV
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
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
watching the clock. That means our code has bugs—the new Framework feature
we implemented doesn’t work quite as expected, and the new best practice we
put in place doesn’t seem to work the same way for every input. These bugs are
Code .NET
frustrating and can often be very difficult to solve.
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
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.
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.
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
that you need.
Christian Gross
Tips and Tricks for Coding .NET 1.1
and .NET 2.0 Applications Effectively
forums.apress.com
SOURCE CODE ONLINE
Companion eBook FOR PROFESSIONALS BY PROFESSIONALS ™
www.apress.com
this print for content only—size & color not accurate 7" x 9-1/4" / CASEBOUND / MALLOY
7443FM.qxd 9/21/06 10:46 PM Page i
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.com in the Source Code/
Download section.
7443FM.qxd 9/21/06 10:46 PM Page iii
Contents at a Glance
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
iii
7443FM.qxd 9/21/06 10:46 PM Page iv
7443FM.qxd 9/21/06 10:46 PM Page v
Contents
vi ■CONTENTS
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
7443FM.qxd 9/21/06 10:46 PM Page vii
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
■JASON LEFEBVRE is 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
7443FM.qxd 9/21/06 10:46 PM Page x
7443FM.qxd 9/21/06 10:46 PM Page xi
Acknowledgments
T his book would not be complete without you, the reader. I came upon the idea for this
book after I realized that I had a number of “canned” solutions to problems that readers of my
articles, clients, or attendees of my conference sessions posed to me. For example, Andreas
Penzold, a reader of my materials, worked with me to figure out what you can expect of
GetHashCode and Equals.
xi
7443FM.qxd 9/21/06 10:46 PM Page xii
7443FM.qxd 9/21/06 10:46 PM Page xiii
Introduction
T he title of this book may seem odd; you probably already know how to write code in .NET.
But you can always benefit from knowing more. Coders, architects, and developers always
strive to do their best, and if given the choice to do something correctly or incorrectly they will
do it correctly. So why do we have so many bugs in our code? I could say, “Heck, it’s all the
managers making bonehead decisions.” It would be a popular answer, but it would not be
fair. We have bugs because humans and the communication between humans are imperfect.
The other major reason why code has bugs is that people do not have the time or energy
to pour resources into specific problems. When you are working on an application, you are
confronted with thousands of specific problems, and you have to assign a priority. This is
where this book is aimed. I take the time to investigate the specific problems and figure
out how to solve them. Your responsibility is to read the solutions and implement them as
appropriate.
This is not a patterns book, even though I reference patterns. It is not a book meant to solve
all problems, because like you I have to assign priority to the problems I want to solve. This book
is the first of a series, and subsequent volumes will solve more problems. This book aims to
look at a problem, feature, or fact and then figure out what that problem, feature, or fact implies.
As a quick example .NET 2.0 introduced the yield keyword. Cool use of technology, but
what does yield really imply? Is yield buggy? Is yield the future of all iterators? After reading
this book you’ll know all of yield’s implications and ramifications.
If you read this book and disagree with me, let me know why you disagree. Tell me what
you think I did wrong. Sometimes I will correct you, but other times, we’ll both learn some-
thing. Or if you want me to figure out a solution to a specific problem you are having, tell me.
If I end up writing about our discussion, I will credit you and give you a free copy of my next
book. Send your love or hate to [email protected].
Source Code
The source code is available in the Source Code/Download section of the Apress website
(http://www.apress.com). Additionally, you can visit http://www.devspace.com/codingdotnet
to download the code.
xiii
7443FM.qxd 9/21/06 10:46 PM Page xiv
Discovering Diverse Content Through
Random Scribd Documents
Birds came daily in greater numbers from the south and their
songs augmented the nameless urge that the south winds bore and
filled the desolate wilds with friendliness and goodwill. Before the
snows had completely disappeared, a layer of thick green grass
began carpeting the earth and myriads of delicate crocuses studded
the green with colour-illumined stars.
Long as the days were becoming, the colts found them all too
short for the full expression of the joy that spring was giving them.
Nights came altogether too soon and the vapoury light of early dawn
revealed them already romping over the plains, seeking to rid their
joints of the sleepy feeling that the long winter had given them. In
wide circles they ran, plunging through sloughs, jumping, kicking at
the air, pretending to bite each other in violent anger, stopping only
when hunger demanded it.
Changes met them wherever they looked. The earth itself and all
life upon it seemed to have become an endless play of the forces of
change. Just as each day was in itself a succession of changes,
white light merging into the tinted colours of evening, fading out in
night and breaking again into the colours and the light of a new day,
so one day was different from another and they felt themselves each
day changing from what they had been the day before.
Queen was only vaguely conscious of these changes in herself,
and in her companions, but one change was clearest of all. Most
easily perceptible of all, this change, in a way, represented them all.
It was the change which she one day realised was taking place in
the black colt. Something was very apparently happening to him. His
black hair fell rapidly, as she had realised her own hair was falling;
but the black colt was steadily growing less black, turning white as
night turns to day. When he was white enough to startle her, she
realised that henceforth he was to be white as his mother was. So
distracting was this change, however, that she sometimes looked at
him with the feeling that he was another colt, and in those rare
moments she experienced a peculiar depressive emotion, like the
feeling she had experienced when she was standing before her dead
mother, looking confusedly down upon her. Yet she knew that it was
he. There were fortunately other characteristics that remained
unchanged. In time, of course, she got quite used to the change in
his appearance; but she never forgot that he had been black. The
image of him, the picture that rose in her mind when she thought of
him and when he was not immediately before her, was a changeable
image which was black one moment and white the next.
If Queen had been in the habit of applying to every image in her
mind some name, she would have called him, “White-black.” Possibly
she might have added the word or the idea, “big,” for he was much
bigger than he had been; but, since that quality applied to all the
colts, she would probably have left that off.
By the varying degrees of this quality in the many colts, as well as
by the many other qualities she learned belonged to all or to each of
them, Queen knew one from the other. All through the long winter
her companionship had been restricted to the black colt and his
mother, but now, the common desires of youth brought the colts
together and led them in time to abandon the companionship of the
mares and the adult horses. Some of them went back every day to
their mothers for milk, but they all played by themselves and even at
night they rested in a group together, away from their mothers.
Though their mothers had their own social life and activities to
occupy them and did not mind the daily absence of their overgrown
foals, their maternal instincts, their anxiety over their erstwhile
babies, was still very great. In spite of this division of interests, in
spite of this habitual grouping, they lived near each other and at the
first sound or sign of danger, they gathered and fled in concert.
The old desire for her mother, the longing, the urge to go forth
and to seek, had lost what little definiteness it had had and had
turned into an impulse to go, which spasmodically welled up in
Queen and sent her loping over the plains without purpose. Always
as soon as he saw her start away, White-black loped after her and
always the rest of the colts followed. Sometimes the older horses
and mares, mistaking the escapade for a sign of danger, would lope
after them.
First happening occasionally, this game began to take place daily
and even several times a day. Just as the colts and other horses got
into the habit of following her, Queen acquired the habitual desire to
be followed.
It happened one morning that the big brown colt led the race.
Jealousy seized at the heart and mind of Queen and she exerted
herself to the very end of her strength to get ahead of him, as if her
life depended upon doing so. She puffed and snorted and pumped
away with her thin long legs, but could not even get abreast of him.
Behind her she could hear the milder snorting of White-black.
Suddenly she veered to the left. She was exhausted and intended
getting out of the way of the herd; but she felt White-black veering
with her and knew that the others were following him.
Quickly she seized the opportunity. She exerted herself with
renewed hope and sped on harder than ever and soon the brown
colt found himself alone. To the left was the whole herd racing
madly after Queen, in an ecstasy of motion. He turned and followed
them, trying hard to catch up, but realising that he had lost. On the
other hand Queen had discovered a trick whereby the newly
acquired leadership could be kept, and she meant to keep it.
Their food grew in abundance wherever they turned. The grass
was rich and juicy; wild plants, sweet and delightful to the taste,
grew abundantly on the hillsides; and water, cool and refreshing,
trembled in every hollow.
Plenty to eat and a great deal of exercise to sharpen the appetite
filled out all the depressions in Queen’s body and because she was
too active to be fat, she became delightfully plump. Her hair now
shorter was sleek and its gloss flashed in the sunlight. Her mane was
luxuriantly thick and wavy. Part of it came down between her ears
and over the white spot on her forehead, down to her eyes, giving
her magnificent head, with the imprint of sadness upon it, a touch of
queenliness that few queens possess.
We all love beauty without being able to say just what it is. The
colts felt a something about her which aroused in them a sort of
homage, spontaneous and unquestioned. White-black, strong and
good-natured, kept the other colts at a safe distance; but they
availed themselves of every chance to touch her, to graze where she
was grazing or to run alongside of her. Sometimes White-black
resented the attention some big fellow offered and started a quarrel
which resulted in his defeat. At such times he would assume the
attitude of one who had been convinced of being wrong. After all he
was yet too young to be serious in his love affairs and his affection
for Queen was due more to their having been reared together than
to anything else.
Queen loved them all, but she loved White-black most and every
colt knew it. Many a quarrel ended in his victory because of her
attitude rather than his strength, but he did not know that. Next to
him Queen favoured the white mare and next to her, the old sorrel
work-horse. White-black understood her love for his mother; but he
could not fathom her predilection for the old horse. For a long time,
when the old sorrel out of pure reminiscent fondness approached
Queen, White-black would lose his temper, kick at the old horse and
attempt to bite him; but where Queen sometimes allowed the colts
to fight it out between themselves, she invariably interfered in any
attempt to wrangle with the sorrel by taking part in it on his side. In
time, White-black learned to let him alone.
The lull of the summer began to creep into the long days, and
mosquitoes and nose-flies in vast numbers came to blight the
sweetness of the spring wilds. The mosquitoes, annoying as these
bloody little pests were, were not half so bad as the nose-flies. The
very sight of their long beaks and yellow backs would drive the colts
frantic. Grazing quietly, they would suddenly begin bobbing their
heads up and down and then start away over the plains as if
something frightful were after them.
This murderous pest always started an attack by buzzing around
the nose like a bee, then landing on the breast it would creep up the
neck till it reached the muzzle, where it would quietly settle down.
Puncturing a hole in the tender nose, it would insert its beak and
drink freely and unshakeably, then fly away leaving a hurt that
burned for hours. When they first appeared, the older horses,
knowing them, would keep their noses in the grass as they grazed,
or they would, when through grazing, gather in groups and rest their
chins firmly upon each other’s backs, thus giving the pest no chance
to creep up. In time the colts learned to protect themselves in the
same way.
When sultry spells were suddenly broken by gusts of unbridled
winds, which would carry the pests away, the colts would give
themselves over to eating and drinking and merrymaking.
There came a sultry spell in the early days of summer. Every chin
was resting upon some friend’s back. Tails switched ceaselessly and
feet stamped the ground with drowsy rhythm. The air was still. Not a
blade of grass moved. The silence was broken only by the nauseous
singing of mosquitoes and the monotonous droning of nose-flies.
Suddenly there came upon the still, warm air the tattoo of distant
hoof-beats. Two horsemen, coming up over a hill to the south, were
just in the act of separating with the obvious intention of coming
together on the other side of them, when Queen discovered them.
Instantly the group broke up, and colts and mares and horses mixed
in a noisy stampede.
When the older horses wearied of the race, they stopped to look
back anxiously at the pursuing riders; but Queen, in whom the fear
of man, dormant all winter, had now awakened with great intensity,
tore away to the north, snorting as she went, her tail at an angle
behind her, loping as fast as she could despite the heat and the
insects.
She came breathlessly to the summit of a rather high hill and
turned to look back. Some of the colts and some of the faster adults
were there with her, but the white mare and the old sorrel were not
there. Half a mile behind them she could see the riders, now facing
south; and beyond them she saw the part of the herd which they
had captured.
White-black was standing beside Queen when he suddenly
discovered the loss of his mother. Neighing loudly and distractedly,
he started down the hill after the men. Queen was afraid to go with
him, yet she did not want to let him go alone. She followed him,
calling to him as she went; but White-black persisted. When they got
within a quarter of a mile of the men, they saw one of them turn off
to the side and then turn backward. White-black then realised the
danger of continuing after them. Judging by horses he had known,
horses reared in barnyards, the man thought that it would be a
simple matter to get the rest of them, now that he had captured
some of them; but he was mistaken.
It was anything but a simple matter. Queen stopped so short that
one of the colts, following along behind, hurt himself, running into
her. With a stamp of her strong front leg, she turned north and once
more led the race for freedom.
All afternoon they ran as fast as their strength would allow. The
smell of man hung in the air before Queen’s nose, poisoning her
blood with hate of him. She had little time to question, yet her whole
soul, confused by fear and the urgent need to make distance, sought
the why of this two-legged creature, always breaking in upon their
peace and always hurting them.
At last they began to feel that no one was pursuing them and
stopped to investigate. There was not the faintest glimpse of
anything on hill or horizon and in the air there was no trace of man.
In the evening they fed about a slough and at night they slept on
the north side of it with their heads turned toward the south.
Early next morning White-black was seized again by an intense
longing for his mother and braving the terrors of captivity, he started
again in search of her. They were trotting and walking along
leisurely, searching the spaces constantly when they came upon a
hill from where they spied a number of horses galloping toward
them. They got frightened and turned back north, but soon stopped
again to ascertain who it was that was coming, and so these horses
gained upon them.
They proved to be three of the colts and a big mare who had
somehow broken free from the cunning little men. They were so
excited that they would not stop to sniff noses. While they passed
through the group they trotted, but as soon as they were on the
other side they broke away in a gallop. Queen and White-black and
all the rest caught the contagion of their fear, abandoned their
search for those who were lost to them and ran with the feeling that
danger of captivity had become imminent once more. And for almost
a week they continued their desultory flight.
When the fear of the little men creatures had lost some of its
intensity, White-black and Queen made several attempts to find the
white mare. Her form seemed to flash across the prairies like
patches of sunlight, seen only at the vanishing moment. Often they
called loud and long trying in vain to pierce the unknown and
waiting hopelessly for a reply.
But this, too, was the inevitable, and railing and fretting was no
solution. In time the hunger for his mother shrank back into the
depths of White-black’s limited soul and the full ardour of his love
fell to the lot of Queen. And Queen felt in the touch and the
presence of White-black a compensation for the aches in her soul,
which, like wounds, had healed, but had left their scars for life.
CHAPTER VI
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com