FLMF039498
FLMF039498
Ian Griffiths
O'REILLY'
Beijing • Cambridge * Farnham • Kbln • Sebastopol • Tokyo
Table of Contents
Preface xvii
1. Introducing C# 1
Why C#? 1
Why Not C#? 3
C#'s Defining Features 5
Managed Code and the CLR 7
Generality Trumps Specialization 9
Asynchronous Programming 10
Visual Studio ' 10
Anatomy of a Simple Program 13
Adding a Project to an Existing Solution 15
Referencing One Project from Another 15
Writing a Unit Test 17
Namespaces 20
Classes 24
Program Entry Point 25
Unit Tests 26
Summary 27
2. Basic Coding in C# 29
Local Variables 30
Scope 35
Statements and Expressions 39
Statements 39
Expressions . 40
Comments and Whitespace 46
Preprocessing Directives 48
Compilation Symbols 48
#error and #warning 49
#line 50
#pragma 50
#region and fendregion * 51
Intrinsic Data Types 51
Numeric Types 52
Booleans 61
Strings and Characters 62
Object 62
Operators 62
Flow Control 68
Boolean Decisions with if Statements 68
Multiple Choice with switch Statements 70
Loops: while and do 72
C-Style for Loops 73
Collection Iteration with foreach Loops 75
Summary 76
3. Types 77
Classes 77
Static Members 80
Static Classes 82
Reference Types 83
Structs 89
When to Write a Value Type 93
Members 98
Fields 98
Constructors 100
Methods 108
Properties 114
Indexers 117
Operators 119
Events 122
Nested Types 122
Interfaces 124
Enums 126
Other Types 129
Anonymous Types 129
Partial Types and Methods 130
Summary ' 131
4. Generics 133
Generic Types 134
Table of Contents
Constraints 136
Type Constraints 137
Reference Type Constraints 139
Value Type Constraints 142
Multiple Constraints 142
Zero-Like Values 143
Generic Methods 144
Type Inference 145
Inside Generics 145
Summary 147
5. Collections 149
Arrays 149
Array Initialization 153
Variable Argument Count with the params Keyword 154
Searching and Sorting 155
Multidimensional Arrays 162
Copying and Resizing 166
List<T> 167
List and Sequence Interfaces 170
. Implementing Lists and Sequences 174
Iterators • 175
Collection<T> 179
ReadOnlyCollection<T> 180
Dictionaries 181
Sorted Dictionaries 184
Sets 185
Queues and Stacks 186
Linked Lists 187
Concurrent Collections 188
Tuples 189
Summary 190
6. Inheritance 191
Inheritance and Conversions 192
Interface Inheritance 194
Generics 195
Covariance and Contravariance 196
•System.Object 202
• The Ubiquitous Methods of object 202
Accessibility and Inheritance 203
Virtual Methods 204
Table of Contents
Abstract Methods 206
Sealed Methods and Classes 213
Accessing Base Members 214
Inheritance and Construction 215
Special Base Types ___ 220
Summary 221
8. Exceptions 265
Exception Sources 267
Exceptions from APIs 268
Exceptions from Your Code 270
Failures Detected by the Runtime 271
Handling Exceptions 272
Exception Objects 273
Multiple catch Blocks 274
Nested try Blocks 275
finally Blocks 277
Throwing Exceptions 278
Rethrowing Exceptions 279
Failing Fast 282
Exception Types 283
Custom Exceptions 285
Unhandled Exceptions 288
Debugging and Exceptions 290
Asynchronous Exceptions 292
Table of Contents
Summary 294
t
k
9. Delegates, Lambdas, and Events 295
Delegate Types 296
Creating a Delegate 297
Multicast Delegates 301
Invoking a Delegate 302
Common Delegate Types 305
Type Compatibility 306
Behind the Syntax 310
Inline Methods 313
Captured Variables 316
Lambdas and Expression Trees 323
Events 324
Standard Event Delegate Pattern 326
Custom Add and Remove Methods 327
Events and the Garbage Collector 330
Events Versus Delegates 332
Delegates Versus Interfaces 333
Summary 333
Table of Contents
Entity Framework 387
LINQ to SQL ^ 387
WCF Data Services Client " 388
Parallel LINQ (PLINQ) 388
LINQ to XML 388
Reactive Extensions 388
Summary 389
Table of Contents
ReplaySubject<T> 440
AsyncSubject<T> 441
Adaptation " 441
IEnumerable<T> 441
.NET Events __ 443
Asynchronous APIs 445
Timed Operations 447
Interval 447
Timer 449
Timestamp 449
Timelnterval 450
Throttle 451
Sample 451
Timeout 451
Windowing Operators 451
Delay 452
DelaySubscription 453
Summary 453
Table of Contents
Summary 486
Table of Contents
Copying 556
' Length 556
Disposal 558
Asynchronous Operation 559
Concrete Stream Types 559
Windows 8 and IRandomAccessStream ~~ 560
Text-Oriented Types 563
TextReader and TextWriter 564
Concrete Reader and Writer Types 566
Encoding 568
Files and Directories 572
FileStream Class 573
File Class 576
Directory Class 579
Path Class 580
Filelnfo, Directorylnfo, and FileSystemlnfo 582
Known Folders 583
Serialization 584
BinaryReader and BinaryWriter 585
CLR Serialization 585
Data Contract Serialization 589
XmlSerializer 592
Summary 593
Table of Contents
Layout 690
Properties 691
v
Panels 697
ScrollViewer 707
Layout Events 707
Controls . "^ 709
Content Controls 709
Slider and ScrollBar Controls 713
Progress Controls 713
List Controls 715
Control Templates 716
UserControls 720
Text 720
Displaying Text 721
Editing Text 723
Data Binding 724
Data Templates 728
Graphics 731
Shapes 731
Bitmaps 732
Media 733
Styles . 734
Summary 735
Table of Contents I xv
MVC 759
Typical MVC Project Layout 759
Writing Models * 766
Writing Views 768
Writing Controllers 770
Handling Additional Input *"** 772
Generating Action Links 775
Routing 775
Summary 780
Index 821