Visual CSharp .NET Programming (2002).pdf

(7790 KB) Pobierz
Microsoft Word - Visual C.doc
Visual C# .NET Programming
Harold Davis
Associate Publisher: Richard Mills
Acquisitions Editor: Denise Santoro Lincoln
Developmental Editor: Tom Cirtin
Editor: Pete Gaughan
Production Editor: Mae Lum
Technical Editor: Matt Tagliaferri
Electronic Publishing Specialists: Rozi Harris, Bill Clark, Interactive Composition
Corporation
Proofreaders: Amey Garber, Nelson Kim, David Nash, Laurie O'Connell, Yariv Rabinovitch,
Nancy Riddiough
Indexer: Lynnzee Elze
Cover Designer: Caryl Gorska, Gorska Design
Cover Photographer: Carlog Navajas, Image Bank
Copyright © 2002 Harold Davis
World rights reserved. No part of this publication may be stored in a retrieval system,
transmitted, or reproduced in any way, including but not limited to photocopy, photograph,
magnetic, or other record, without the prior agreement and written permission of the
publisher.
Figures 2.1 , 2.2 , 8.4 , 10.6 , and 12.16 Copyright © 2002, Phyllis Davis. All rights reserved.
Library of Congress Card Number: 2002106412
ISBN: 0-7821-4046-7
SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc.
in the United States and/or other countries.
Screen reproductions produced with FullShot 99. FullShot 99 © 1991-1999 Inbit
Incorporated. All rights reserved FullShot is a trademark of Inbit Incorporated.
Internet screen shot(s) using Microsoft Internet Explorer 6 reprinted by permission from
Microsoft Corporation.
Microsoft, the Microsoft Internet Explorer logo, Windows, Windows NT, and the Windows
logo are either registered trademarks or trademarks of Microsoft Corporation in the United
States and/or other countries.
TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary
trademarks from descriptive terms by following the capitalization style used by the
manufacturer.
The author and publisher have made their best efforts to prepare this book, and the content is
based upon final release software whenever possible. Portions of the manuscript may be based
upon pre-release versions supplied by software manufacturer(s). The author and the publisher
make no representation or warranties of any kind with regard to the completeness or accuracy
of the contents herein and accept no liability of any kind including but not limited to
performance, merchantability, fitness for any particular purpose, or any losses or damages of
any kind caused or alleged to be caused directly or indirectly from this book.
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
For Phyllis, who makes the music in my life
Acknowledgments
When the music stops, an author alone is responsible for the book he or she has created. That
said, a book such as this is produced through the efforts of many people. Richard Mills and
Denise Santoro Lincoln originated this project and brought me into it. Tom Cirtin did a great
job of helping to birth this book, and contributed from his vast store of musical knowledge.
Mae Lum masterfully handled the logistics as the book became a full-fledged project. Pete
Gaughan copyedited this book and has substantially helped to make it something we can all
be proud of. Matt Tagliaferri provided technical review and helped save me from myself.
In addition to team Sybex, I would like to thank my friend and agent, Matt Wagner, and Bill
Gladstone, both of Waterside Productions.
I am thankful to Phyllis Davis, who contributed beyond the call of duty in a number of ways,
and to Martin Davis, who read several chapters in 'manuscript,' as they quaintly say, and made
many useful suggestions. And thanks to Chris Hopper, who helped with hardware.
Last, but not least, a standing ovation for Anders Hejlsberg and Scott Wiltamuth, without
whom there would be no C# to write about.
The quotation on the bottom of the front cover is taken from the thirty-fifth chapter of Lao
Tzu'sTao Te Ching, the classic work of Taoist philosophy. This particular verse is from the
translation byD. C. Lau (copyright 1963) and communicates a theme explored throughout the
book: true knowledge transcends the ordinary senses.
It is traditionally held that Lao Tzu lived in the fifth century B.C. in China, during the Chou
dynasty, but it is unclear whether he was actually a historical figure. It is said that he was a
teacher of Confucius. The concepts embodied in the Tao Te Ching influenced religious
thinking in the Far East, including Zen Buddhism in Japan. Many in the West, however, have
wrongly understood theTao Te Ching to be primarily a mystical work; in fact, much of the
advice in the book is grounded in a practical moral philosophy governing personal conduct.
Introduction
I dreamed that black-clad horsemen pursued me down a lonely road. The hoofs of their steeds
rang with urgent clanks on the paving stones. I turned to look at my pursuers and saw fiery
red-rimmed eyes fixed within deathly pale faces. A sword was raised, and as it swept down…
No, that's not the way it goes at all.
I dreamed of a city far in the future. Sentient machines performed all menial labor, so there
was plenty of time for science and art. But all was not well in paradise. Regimentation begat
alienation, and alienation begat a class of cyber-hackers who had dropped out of known
society and lived in caves far from the city.
That's a little closer, but we're not quite there yet! Let's try again.
I dreamed of a pure programming language, so sweet and tender, yet flexible and strong. This
language, named after a musical note, incorporated the best features of other languages and
also made available an extremely potent library of classes. You guessed it: the language is C#,
and the library of classes the .NET Framework. This dream is true!
This is a different kind of book about a programming language. The conventional thing is to
begin with syntax and semantics, proceed through user interfaces and object orientation, and
end with various applications. But why be conventional? This book does not do the standard
thing.
To some degree, a book is a compact between writer and reader. The reader will rightly be
disappointed if what they expected to find is missing. At the same time, no book can be
everything for everybody.
In this sense, the compact between writer and reader is analogous to the implementation of an
interface in a class. Everything is spelled out in the interface, so that there is no
misunderstanding about how to use an implementation of it.
I expect readers of this book to have some experience with programming, or at least be highly
intelligent. This is not a book for dummies. (Or, as Mel Brooks exhorted in a different
context, 'Be a smarty!')
However, your programming experience need not be with a language in the 'C' family-or even
with Java. C# represents a wonderful 'next step' for Visual Basic programmers. If you are a
VB programmer looking for new horizons, this book was written for you.
By the way, the one area that seems to trip VB programmers new to C# is type conversion. So
if you are a VB programmer new to C#, you might want to start with a look at the material
explaining type conversion in Chapter 6 , ' Zen and Now: The C# Language . '
I do not promise to be comprehensive or encyclopedic in my coverage of C# or the .NET
Framework. For one thing, no single book could ever keep this promise, as the field is so vast.
For another, online help is the best place for detailed answers to many questions-so, as
appropriate in this book, I refer you to help topics.
Internal tools such as the Object Browser reveal more information than any documentation
could-I show you how to make the best use of the Object Browser in Chapter 5 , ' Reflecting on
Classes .'
Finally, most serious programmers-or students of a programming language-have multiple
books about the language on their shelves: In other words, comprehensiveness is found in
libraries, and in online compendiums, not individual books.
So if I don't promise to be comprehensive, what commitments am I making?
First, regarding the code in the book: I've tried to provide examples that you will be able to
use in the real world, based on my experience as a developer. I've run and tested every
example in the book. Many of the examples should be usable in whole or part as they are
written.
C# is a graceful language. I've tried to write about it in an intelligent, elegant, and humorous
way.
I hope you enjoy this book. C# .NET is a powerful, exciting, easy-to-use programming
language. The primary goals of my book are to:
Share my excitement and joy in this aesthetically pleasing and productive tool.
Help you to understand the concepts involved in programming with C# and the .NET
Framework.
Help you easily produce the code that you need for real projects.
If you read through this book and follow the examples, you will learn a lot. In contrast to the
conventional structure of the programming language book, described earlier in this
introduction, the narrative structure of this book involves immersion. You'll learn by doing-
starting with creating a web service in the first few pages. It's only later that the nitty-gritty of
language syntax is covered in detail. The idea is that you'll be having so much fun by then that
the pain of mastering the details will be muted.
While we're on the subject of narrative structure-and, yes, Virginia, even computer books do
have narrative structure-let's talk about the musical part names of this book.
The Structure of This Book: About the Musical Part
Names
Since C# is a programming language named after a musical note, I thought it appropriate to
involve musical concepts when structuring this book. In keeping with this, I've named each
of the four parts of the book after movements in a classical composition. These movements-
prelude, allemande, courante, and gigue-primarily are found in Baroque music. Musical
scholars should note that I have not been compulsive about the accuracy or consistency of the
musical metaphor. The point really is the metaphor and how it relates to the structure of this
book and to programming in C#.
The structure of the book is essentially spiral, like a chambered nautilus shell or the pattern in
this volume's cover photograph of a Zen garden. By the end of the book, readers will be able
to comprehend and accomplish things that seemed shadowy and mysterious when they
plunged in at the beginning. Each of the four parts represents a different stage in this quest for
skills and understanding.
Part 1 : Prelude-Service with a Smile
In classical music, the prelude introduces the work. Often composed in a free-flowing style, it
sets the mood and mode for the rest of the work and is designed to pique the interest of the
audience. It can contain references to ideas that are delivered later-foreshadowings, a taste
of things to come. The themes in the prelude are not whole ideas but snippets, motifs- just
enough to whet the appetite and make the listener want more. These motifs are pre-echoes-
not déjà vu, which are vague memories of things already seen, but rather premonitions of
things to come. If you listen to the composition more than once, then in the prelude you
should be able to begin to hear the pattern of the entire piece.
At the same time that a prelude introduces the larger work; it is an organic unit in and of
itself, with a beginning, middle, and end. This cohesive mini-composition exists within the
larger whole and has its own sense of narrative conflict and resolution, point and
counterpoint, all reconciling in a conclusion that serves as an introduction.
Our prelude introduces the theme of the web service. Web services have been hailed by some
as revolutionary: a brand new kind of unit of executable code, fit for the distributed
environments of the Internet age.
A web service is not an end in and of itself. To actually do anything as a part of a program, it
must be used-or, put another way, 'consumed.'
It is also the case that this book is not 'about' web services; it is about programming in the C#
language and the .NET Framework.
Our prelude explores creating a web service, in Chapter 1 , ' Creating a Web Service ,' and
coding ASP.NET web applications to consume the web service, in Chapter 2 , 'Consuming the
Service on the Web,' as a dramatic way to jump into the topics that will form the pattern of the
composition that is this book. Keep your eyes and ears open for premonitions that reveal this
book's real themes: the best way to write C# code for clarity, and patterns and practice of
communication between objects.
Part II : Allemande-Striding Forward
The allemande is a movement of great substance that directly follows the prelude of a musical
suite and picks up where the prelude leaves off. It is stately in manner and can be highly
stylized. The allemande carries forward the mood prefigured in the prelude and introduces
gravity into the suite; but the prelude's free style gives way to the processional-like regularity
of the allemande.
The sentiments casually introduced in the prelude have become a stepping dance with reality-
and the allemande keeps it all moving. The meter is steady and so is the progress.
The allemande is striding forward without hesitation into the future, and the future is now.
Early allemandes come in three sections, or strains, that are related but not the same. The
second strain contrasts with the first strain. They resolve in the third and final section , which
paves the way for the next movement in the composition.
Zgłoś jeśli naruszono regulamin