this print for content only—size & color not accurate spine = 0.969" 512 page countBeginning C# 2008: From Novice to Professional Dear Reader, This book is about showing you how to writ
Trang 1this print for content only—size & color not accurate spine = 0.969" 512 page count
Beginning C# 2008:
From Novice to Professional
Dear Reader, This book is about showing you how to write good code in C# 2008, even if you have never programmed before Writing good code can be a challenge—there are so many options, especially in C# 2008 So if you want to really get the most from a programming language, you need to know which features work best
in which situations and understand their strengths and weaknesses It is this understanding that makes the difference between coding and coding well
With this in mind, I have written Beginning C# 2008 to teach you how to use
the C# programming language to solve problems From the earliest chapters, and from the first introductory concepts that you’ll discover about C#, you will
be looking at real programming challenges and learning how C# can be used to solve them As you progress with me through the book, these problems become increasingly more involved and interesting, as I show you how C# features can interact to achieve the programming results you want
By the time you’ve finished reading Beginning C# 2008 and worked through
the sample exercises, you’ll be a confident and very competent C# programmer
You will still have many new explorations of the huge NET Framework API to look forward to in your programming, but you will have a firm foundation for your future career in C#, and you will know exactly where to find all the infor-mation you need to progress confidently with your C# projects
Christian Gross
Author of
How to Code NET
Ajax and REST Recipes
Ajax Patterns
and Best Practices
Foundations of
Object-Oriented Programming
Using NET 2.0 Patterns
A Programmer’s Introduction
to Windows DNA
US $39.99
Shelve in
Programming/C#
User level:
Beginner–Intermediate
Beginning
From Novice to Professional
MaGenTa Black
panTone 123 c
Christian Gross
Companion eBook Available
THE APRESS ROADMAP
Pro ASP.NET 3.5 with C# 2008
Pro LINQ: Object Relational Modelling in C# 2008
Pro WPF in C#
Accelerated C# 2008
Beginning C# 2008
Pro LINQ
Pro C# 2008 and the NET 3.5 Platform
www.apress.com
SOURCE CODE ONLINE
Companion eBook
See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-869-6 ISBN-10: 1-59059-869-5
9 781590 598696
5 3 9 9 9
Beginning
Trang 3Beginning C# 2008
From Novice to Professional
Christian Gross
Trang 4ISBN-10 (pbk): 1-59059-869-5
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: Christian Kenyeres
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Sofia Marchant
Copy Editor: Marilyn Smith
Associate Production Director: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor: Kinetic Publishing Services
Proofreader: Nancy Riddiough
Indexer: Broccoli Information Management
Artist: Kinetic Publishing Services
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 orders-ny@springer-sbm.com, or visit http://www.springeronline.com
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley,
CA 94705 Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com The information in this book is distributed on an “as is” basis, without warranty Although every precaution 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
Trang 5Some food for thought when writing software:
“A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.”
“The major difference between a thing that might go wrong and a thing that cannot possibly go wrong is that when a thing that cannot possibly go wrong goes
wrong it usually turns out to be impossible to get at or repair.”
—Douglas Adams, Mostly Harmless
Trang 6Translator class, 55 whitespace, 63–67 finding substrings, 64 trimming, 63
writing tests before writing code, 65–67 Translator class, 55
Translator.cs file, 55
TrimmingWhitespace( ) method, 63
truth tables, 269
try block, 121, 127–128
try keyword, 120
try/catch block, 134–135
TryParse( ) method, 71–72, 132
type attribute, 344
type casting, 173, 196–197
type command, 279
type constraint, 463–465
type method, 448
TypeName property identifier, 356
typeof(string) parameter, 345
types See also custom-defined types;
number-related types; string types custom, 289–293
Equals( ) method, 292–293 GetHashCode( ) method, 289–292 defining, 283–284
dynamically instantiating using instantiated types, 342–343 using singletons, 341–342 Type-safe, 35
■ U
uint type, 46
ulong type, 46
UML (Unified Modeling Language), 28
unboxing, 235
#undef hashcode, 159
underflow, 43
Unicode, 68
Unified Modeling Language (UML), 28
uniqueidentifier type, 402
Unstable, 352
UpgradeToWriterLock( ) method, 376
usability, 301
User assembly, 223
user interface, 31
user-defined types See custom-defined types
username, 395
ushort type, 46
Value type, 85 value types, 44–45, 235–236 constraints, 81–87 effects of copying data, 82–84 parameters, 85–87
reference types, 84–85 referencing using, 92–94 value variable, 145, 349 ValueType type, 321 variables, class library project, 21–23 verification, 150–153
conditional statements, 150–151 partial classes, 152–153
VerifyExchangeRate( ) method, 152 version numbers, dynamic loading of strongly named assemblies using, 351–354
adding assembly redirection to configuration files, 353–354 versioning assemblies, 352–353 VersionAssembly assembly, 353 versioning assemblies, 352–353 virtual base class method, 192 virtual keyword, 178, 192, 195 Visual C# Express, 1–26 application types, 4 class library project, 17–23 adding to solutions, 17–18 calling functionality, 19–20 constants, 21–23
defining references, 19 moving functionality, 18 variables, 21–23 console application project, 16–17 adding to solutions, 16
“hello, world”, 16 running, 17 setting startup project, 17 Data Source Configuration Wizard, 396 designing relational databases using, 395–403
adding tables, 399–403 configuring Data Source, 396–399 downloading, 2–3
installing, 3–4 navigating user controls of solutions, 13–15
.NET Framework, 23–25 overview, 1
Trang 7projects, 4 solutions, 4 Windows application project, 5–13 adding comments to, 12–13
“hello, world”, 9–11 renaming solutions, 6–7 running, 7–8
saving solutions, 7 viewing source code, 6 Visual Web Developer Express, 2
■ W
where statement, 463
while loop, 413
whitespace, 63–67
character-by-character comparison, 61–62 finding substrings, 64
responsibility, 57 trimming, 63 writing tests before writing code, 65–67 Windows application project, 5–13
adding comments to, 12–13
“hello, world”, 9–11 renaming solutions, 6–7 running, 7–8
saving solutions, 7 viewing source code, 6 Windows, setting languages/cultures in,
69–70 Windows.Forms Invoke( ) method, 383
Windows.Forms library, 380
Windows\System32 directory, 349
winners table, 402–403 word allo test, 66 worksheet variable, 320 Worksheet<> class, 314, 322, 327 Worksheet<BaseType> type, 304 Worksheet<double> type, 304 Worksheet<string> type, 304 worksheet.Calculate( ) method, 326 WorksheetIdentifiers class, 308 writing exception-safe code, 130–135 default state, 133–134
defensive coding, 130–133 processing warning errors, 134–135 writing tests for currency exchange application, 139–153 base classes, 140–141, 153 C# properties, 143–146 inheritance, 141–143, 147–148 scope modifiers, 148–150 structural code, 140 verification, 150–153
■ X
XML (Extensible Markup Language), 336 XML-based configuration files, 336–337 xsd file, 408
■ Y
_year data member, 186 year parameter, 186 yield keyword, 219–220, 231, 323 yield return statement, 220, 222
■I N D E X 487