[1146143818]Watson B. - C# 4.0 How-To.pdf
(
5645 KB
)
Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
BEN WATSON
C
#
4.0
HOW-TO
800 East 96th Street, Indianapolis, Indiana 46240 USA
C# 4.0 How-To
Copyright © 2010 by Pearson Education, Inc.
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical,
photocopying, recording, or otherwise, without written permission from the
publisher. No patent liability is assumed with respect to the use of the
information contained herein. Although every precaution has been taken in
the preparation of this book, the publisher and author assume no responsi-
bility for errors or omissions. Nor is any liability assumed for damages
resulting from the use of the information contained herein.
ISBN-13: 978-0-672-33063-6
ISBN-10: 0-672-33063-6
Library of Congress Cataloging-in-Publication Data
Watson, Ben, 1980–
C# 4.0 how-to / Ben Watson.
p. cm.
Includes index.
ISBN 978-0-672-33063-6 (pbk. : alk. paper) 1. C# (Computer program
language) I. Title.
QA76.73.C154W38 2010
005.13’3—dc22
Editor-in-Chief
Karen Gettman
Executive Editor
Neil Rowe
Acquisitions Editor
Brook Farling
Development Editor
Mark Renfrow
Managing Editor
Kristy Hart
Project Editor
Lori Lyons
Copy Editor
Bart Reed
Indexer
Brad Herriman
2010002735
Proofreader
Sheri Cain
Printed in the United States of America
First Printing March 2010
Technical Editor
Mark Strawmyer
Trademarks
All terms mentioned in this book that are known to be trademarks or
service marks have been appropriately capitalized. Sams Publishing cannot
attest to the accuracy of this information. Use of a term in this book
should not be regarded as affecting the validity of any trademark or service
mark.
Publishing
Coordinator
Cindy Teeters
Designer
Gary Adair
Warning and Disclaimer
Every effort has been made to make this book as complete and as accu-
rate as possible, but no warranty or fitness is implied. The information
provided is on an “as is” basis. The author and the publisher shall have
neither liability nor responsibility to any person or entity with respect to any
loss or damages arising from the information contained in this book.
Compositor
Nonie Ratcliff
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in
quantity for bulk purchases or special sales. For more information, please
contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
international@pearson.com
Contents at a Glance
Introduction
............................................................................................
1
Part I: C# Fundamentals
1
Type Fundamentals
.................................................................................
7
2
Creating Versatile Types
........................................................................
27
3
General Coding
....................................................................................
45
4
Exceptions
...........................................................................................
63
5
Numbers
..............................................................................................
77
6
Enumerations
.......................................................................................
99
7
Strings
...............................................................................................
109
8
Regular Expressions
...........................................................................
131
9
Generics
............................................................................................
139
Part II: Handling Data
10
Collections
.........................................................................................
155
11
Files and Serialization
.........................................................................
177
12
Networking and the Web
.....................................................................
201
13
Databases
.........................................................................................
237
14
XML
...................................................................................................
261
Part III: User Interaction
15
Delegates, Events, and Anonymous Methods
........................................
279
16
Windows Forms
..................................................................................
295
17
Graphics with Windows Forms and GDI+
..............................................
329
18
WPF
...................................................................................................
365
19
ASP.NET
............................................................................................
401
20
Silverlight
...........................................................................................
443
Part IV: Advanced C#
21
LINQ
..................................................................................................
461
22
Memory Management
.........................................................................
473
23
Threads, Asynchronous, and Parallel Programming
................................
491
24
Reflection and Creating Plugins
...........................................................
519
25
Application Patterns and Tips
..............................................................
529
26
Interacting with the OS and Hardware
..................................................
575
27
Fun Stuff and Loose Ends
...................................................................
597
A
Essential Tools
...................................................................................
621
Index
.................................................................................................
633
Table of Contents
Introduction 1
Overview of
C# 4.0 How-To
...................................................................
1
How-To Benefit from This Book
.............................................................
1
How-To Continue Expanding Your Knowledge
..........................................
3
Part I: C# Fundamentals
1 Type Fundamentals 7
Create a Class
.....................................................................................
8
Define Fields, Properties, and Methods
.................................................
9
Define Static Members
.......................................................................
10
Add a Constructor
..............................................................................
11
Initialize Properties at Construction
.....................................................
12
Use
const
and
readonly
...................................................................
13
Reuse Code in Multiple Constructors
..................................................
14
Derive from a Class
...........................................................................
14
Call a Base Class Constructor
............................................................
15
Override a Base Class’s Method or Property
........................................
16
Create an Interface
............................................................................
19
Implement Interfaces
.........................................................................
19
Create a Struct
..................................................................................
21
Create an Anonymous Type
.................................................................
22
Prevent Instantiation with an Abstract Base Class
................................
23
Interface or Abstract Base Class?
.......................................................
24
2 Creating Versatile Types 27
Format a Type with
ToString()
..........................................................
28
Make Types Equatable
........................................................................
32
Make Types Hashable with
GetHashCode()
.........................................
34
Make Types Sortable
..........................................................................
34
Give Types an Index
...........................................................................
36
Notify Clients when Changes Happen
..................................................
38
Overload Appropriate Operators
..........................................................
39
Convert One Type to Another
...............................................................
40
Prevent Inheritance
............................................................................
41
Allow Value Type to Be Null
.................................................................
42
Contents
v
3 General Coding 45
Declare Variables
...............................................................................
46
Defer Type Checking to Runtime (Dynamic Types)
.................................
47
Use Dynamic Typing to Simplify COM Interop
........................................
49
Declare Arrays
...................................................................................
50
Create Multidimensional Arrays
...........................................................
50
Alias a Namespace
............................................................................
51
Use the Conditional Operator (
?:
)
.......................................................
52
Use the Null-Coalescing Operator (
??
)
.................................................
53
Add Methods to Existing Types with Extension Methods
........................
54
Call Methods with Default Parameters
.................................................
55
Call Methods with Named Parameters
.................................................
56
Defer Evaluation of a Value Until Referenced
.......................................
57
Enforce Code Contracts
......................................................................
58
4 Exceptions 63
Throw an Exception
............................................................................
64
Catch an Exception
............................................................................
64
Catch Multiple Exceptions
...................................................................
65
Rethrow an Exception
.........................................................................
66
(Almost) Guarantee Execution with
finally
.........................................
67
Get Useful Information from an Exception
............................................
68
Create Your Own Exception Class
........................................................
70
Catch Unhandled Exceptions
...............................................................
72
Usage Guidelines
...............................................................................
76
5 Numbers 77
Decide Between
Float
,
Double
, and
Decimal
.....................................
78
Use Enormous Integers (
BigInteger
)
.................................................
79
Use Complex Numbers
.......................................................................
80
Format Numbers in a String
................................................................
82
Convert a String to a Number
.............................................................
86
Convert Between Number Bases
.........................................................
87
Convert a Number to Bytes (and Vice Versa)
........................................
89
Determine if an Integer Is Even
...........................................................
91
Determine if an Integer Is a Power of 2 (aka, A Single Bit Is Set)
..........
91
Determine if a Number Is Prime
..........................................................
91
Count the Number of 1 Bits
................................................................
92
Convert Degrees and Radians
.............................................................
93
Round Numbers
.................................................................................
93
Generate Better Random Numbers
......................................................
96
Generate Unique IDs (GUIDs)
..............................................................
97
Plik z chomika:
valdemaroff
Inne pliki z tego folderu:
[1146126419]Цветы и букеты.Легкий пэчворк.-Регина Бюлер,Арт-Родник,2008.rar
(35443 KB)
[1146126419]Цветы и букеты.Легкий пэчворк.-Регина Бюлер,Арт-Родник,2008.rar.!ut
(35443 KB)
[1146143786]Без бюджета.57 эффективных приемов маркетинга.-Игорь Манн,Манн,Иванов и Фербер,2009.pdf
(56115 KB)
[1146140307]Троянские сказания.-разные,Ленинград.Наука,1972.djvu.!ut
(10923 KB)
[1146140307]Троянские сказания.-разные,Ленинград.Наука,1972.djvu
(10923 KB)
Inne foldery tego chomika:
!08 - Военное дело
!Infanata 2010-12
«Борьба классов» - 1931-1936
08 - Военное дело
1
Zgłoś jeśli
naruszono regulamin