The Definitive Guide to C# “From using LINQ to covariance and from string formatting to optional arguments, Herb’s update covers all you need to know about using real-world C# 4.0.” ─Michael Howard, Microsoft Corporation Bestselling author Herb Schildt has updated and expanded his classic programming reference to cover C# 4.0--the latest release of this powerful, feature-rich language. Using carefully crafted explanations, insider tips, and hundreds of examples, C# 4.0: The Complete Reference offers in-depth details on all aspects of C#, including keywords, syntax, and core libraries. New C# 4.0 features, such as named and optional arguments, the dynamic type, expanded covariance and contravariance capabilities, and support for parallel programming with the Task Parallel Library (TPL) and PLINQ are also discussed. Perfect for beginners and professional C# programmers alike, this expert resource is written in the clear, crisp, uncompromising style that has made Herb the choice of millions worldwide. Begin mastering C# 4.0 today with help from this authoritative volume! Coverage includes: Data types and operators Control statements Classes and objects Constructors, destructors, and methods Interfaces, arrays, enumerations, and structures Method and operator overloading Inheritance and virtual methods Reflection and runtime type ID Exception handling Delegates, properties, events, and indexers Attributes Multithreading and the Task Parallel Library Generics LINQ and PLINQ Lambda expressions Anonymous types Extension methods Named and optional arguments Implicitly typed variables I/O, networking, and collections The preprocessor and much, much more
Author(s): Herbert Schildt
Edition: 1
Year: 2010
Language: English
Pages: 976
Contents......Page 8
Special Thanks......Page 22
Preface......Page 24
Part I: The C# Language......Page 28
C#’s Family Tree......Page 30
How the Common Language Runtime Works......Page 35
Managed vs. Unmanaged Code......Page 36
Object-Oriented Programming......Page 38
A First Simple Program......Page 40
Handling Syntax Errors......Page 50
A Second Simple Program......Page 51
Another Data Type......Page 53
Two Control Statements......Page 55
Using Code Blocks......Page 58
Semicolons, Positioning, and Indentation......Page 59
The C# Keywords......Page 60
Identifiers......Page 61
The .NET Framework Class Library......Page 62
C#’s Value Types......Page 64
Integers......Page 65
Floating-Point Types......Page 67
The decimal Type......Page 69
Characters......Page 70
The bool Type......Page 71
Some Output Options......Page 72
Literals......Page 75
A Closer Look at Variables......Page 78
The Scope and Lifetime of Variables......Page 81
Type Conversion and Casting......Page 84
Type Conversion in Expressions......Page 88
Arithmetic Operators......Page 92
Relational and Logical Operators......Page 96
The Assignment Operator......Page 100
The Bitwise Operators......Page 102
The ? Operator......Page 111
Spacing and Parentheses......Page 112
Operator Precedence......Page 113
The if Statement......Page 114
The switch Statement......Page 117
The for Loop......Page 121
The while Loop......Page 128
The do-while Loop......Page 130
Using break to Exit a Loop......Page 131
Using continue......Page 133
The goto......Page 134
Class Fundamentals......Page 138
How Objects Are Created......Page 143
Reference Variables and Assignment......Page 144
Methods......Page 145
Constructors......Page 155
The new Operator Revisited......Page 159
Garbage Collection and Destructors......Page 160
The this Keyword......Page 162
Arrays......Page 166
Multidimensional Arrays......Page 170
Jagged Arrays......Page 173
Assigning Array References......Page 175
Using the Length Property......Page 177
Implicitly Typed Arrays......Page 180
The foreach Loop......Page 181
Strings......Page 185
Controlling Access to Class Members......Page 194
Pass References to Methods......Page 201
Use ref and out Parameters......Page 205
Use a Variable Number of Arguments......Page 211
Return Objects......Page 214
Method Overloading......Page 217
Overload Constructors......Page 223
Object Initializers......Page 228
Optional Arguments......Page 229
Named Arguments......Page 233
The Main( ) Method......Page 235
Recursion......Page 237
Understanding static......Page 240
Static Classes......Page 245
Operator Overloading Fundamentals......Page 248
Handling Operations on C# Built-in Types......Page 255
Overloading the Relational Operators......Page 259
Overloading true and false......Page 261
Overloading the Logical Operators......Page 264
Conversion Operators......Page 270
Operator Overloading Tips and Restrictions......Page 274
Another Example of Operator Overloading......Page 275
Indexers......Page 280
Properties......Page 289
Use Access Modifiers with Accessors......Page 296
Using Indexers and Properties......Page 299
Inheritance Basics......Page 304
Member Access and Inheritance......Page 307
Constructors and Inheritance......Page 311
Inheritance and Name Hiding......Page 317
Creating a Multilevel Hierarchy......Page 320
When Are Constructors Called?......Page 323
Base Class References and Derived Objects......Page 324
Virtual Methods and Overriding......Page 329
Using Abstract Classes......Page 336
The object Class......Page 340
Interfaces......Page 346
Using Interface References......Page 351
Interface Properties......Page 353
Interface Indexers......Page 355
Interfaces Can Be Inherited......Page 357
Explicit Implementations......Page 358
Structures......Page 361
Enumerations......Page 367
The System.Exception Class......Page 372
Exception-Handling Fundamentals......Page 373
The Consequences of an Uncaught Exception......Page 376
Exceptions Let You Handle Errors Gracefully......Page 378
Using Multiple catch Clauses......Page 379
Catching All Exceptions......Page 380
Nesting try Blocks......Page 381
Throwing an Exception......Page 382
Using finally......Page 384
A Closer Look at the Exception Class......Page 386
Deriving Exception Classes......Page 389
Catching Derived Class Exceptions......Page 393
Using checked and unchecked......Page 395
C#’s I/O Is Built Upon Streams......Page 398
The Stream Classes......Page 399
Console I/O......Page 402
FileStream and Byte-Oriented File I/O......Page 407
Character-Based File I/O......Page 414
Redirecting the Standard Streams......Page 417
Reading and Writing Binary Data......Page 419
Random Access Files......Page 425
Using MemoryStream......Page 427
Using StringReader and StringWriter......Page 429
The File Class......Page 431
Converting Numeric Strings to Their Internal Representation......Page 433
Delegates......Page 438
Anonymous Functions......Page 447
Anonymous Methods......Page 448
Lambda Expressions......Page 452
Events......Page 458
Use Anonymous Methods and Lambda Expressions with Events......Page 468
.NET Event Guidelines......Page 469
Applying Events: A Case Study......Page 472
Namespaces......Page 476
The Preprocessor......Page 490
Assemblies and the internal Access Modifier......Page 496
Runtime Type Identification......Page 498
Reflection......Page 502
Using Reflection......Page 504
Attributes......Page 522
Three Built-in Attributes......Page 530
What Are Generics?......Page 534
A Simple Generics Example......Page 535
A Generic Class with Two Type Parameters......Page 541
Constrained Types......Page 542
Creating a Default Value of a Type Parameter......Page 561
Generic Structures......Page 562
Creating a Generic Method......Page 563
Generic Delegates......Page 566
Generic Interfaces......Page 568
Comparing Instances of a Type Parameter......Page 571
Generic Class Hierarchies......Page 575
Overriding Virtual Methods in a Generic Class......Page 579
Overloading Methods That Use Type Parameters......Page 580
Covariance and Contravariance in Generic Type Parameters......Page 582
How Generic Types Are Instantiated......Page 590
Final Thoughts on Generics......Page 591
19 LINQ......Page 592
LINQ Fundamentals......Page 593
Filter Values with where......Page 598
Sort Results with orderby......Page 599
A Closer Look at select......Page 603
Use Nested from Clauses......Page 607
Group Results with group......Page 608
Use into to Create a Continuation......Page 610
Use let to Create a Variable in a Query......Page 612
Join Two Sequences with join......Page 613
Anonymous Types......Page 616
Create a Group Join......Page 618
The Query Methods......Page 621
Deferred vs. Immediate Query Execution......Page 627
Expression Trees......Page 628
Extension Methods......Page 629
PLINQ......Page 631
Unsafe Code......Page 632
Nullable Types......Page 645
Partial Types......Page 650
Partial Methods......Page 651
Create a Dynamic Type with dynamic......Page 652
COM Interoperability......Page 656
Miscellaneous Keywords......Page 657
Part II: Exploring the C# Library......Page 664
The Members of System......Page 666
The Math Class......Page 668
The .NET Structures Corresponding to the Built-in Value Types......Page 673
The Array Class......Page 690
BitConverter......Page 707
Generating Random Numbers with Random......Page 708
Memory Management and the GC Class......Page 709
Object......Page 711
The IComparable and IComparable Interfaces......Page 712
The ICloneable Interface......Page 713
IFormatProvider and IFormattable......Page 715
IObservable and IObserver......Page 716
Strings in C#......Page 718
The String Class......Page 719
Formatting......Page 742
Using String.Format( ) and ToString( ) to Format Data......Page 746
Creating a Custom Numeric Format......Page 749
Formatting Date and Time......Page 753
Formatting Enumerations......Page 759
23 Multithreaded Programming, Part One......Page 762
Multithreading Fundamentals......Page 763
The Thread Class......Page 764
Determining When a Thread Ends......Page 769
Passing an Argument to a Thread......Page 772
Thread Priorities......Page 774
Synchronization......Page 777
Thread Communication Using Wait( ), Pulse( ), and PulseAll( )......Page 783
Using MethodImplAttribute......Page 787
Using a Mutex and a Semaphore......Page 789
Using Events......Page 796
The Interlocked Class......Page 799
Synchronization Classes Added by .NET 4.0......Page 800
Terminating a Thread Via Abort( )......Page 801
Suspending and Resuming a Thread......Page 805
Using the Main Thread......Page 806
Additional Multithreading Features Added by .NET 4.0......Page 807
Starting a Separate Task......Page 808
24 Multithreading, Part Two: Exploring the Task Parallel Library and PLINQ......Page 810
The Task Class......Page 811
Using Wait Methods......Page 816
Use a Lambda Expression as a Task......Page 819
Create a Task Continuation......Page 821
Returning a Value from a Task......Page 823
Cancelling a Task and Using AggregateException......Page 825
The Parallel Class......Page 828
Exploring PLINQ......Page 839
Collections Overview......Page 844
The Non-Generic Collections......Page 845
Storing Bits with BitArray......Page 867
The Generic Collections......Page 870
The Concurrent Collections......Page 896
Storing User-Defined Classes in Collections......Page 900
Implementing IComparable......Page 902
Using an IComparer......Page 905
Using StringComparer......Page 908
Accessing a Collection via an Enumerator......Page 909
Implementing IEnumerable and IEnumerator......Page 912
Using Iterators......Page 914
Collection Initializers......Page 920
The System.Net Members......Page 922
Uniform Resource Identifiers......Page 924
Internet Access Fundamentals......Page 925
Handling Network Errors......Page 931
The URI Class......Page 934
Accessing Additional HTTP Response Information......Page 935
MiniCrawler: A Case Study......Page 940
Using WebClient......Page 943
The XML Comment Tags......Page 948
Compiling Documentation Comments......Page 949
An XML Documentation Example......Page 950
Index......Page 952
A......Page 953
C......Page 954
D......Page 957
E......Page 958
F......Page 959
G......Page 960
I......Page 961
J......Page 963
M......Page 964
O......Page 966
P......Page 967
Q......Page 968
S......Page 969
T......Page 972
V......Page 974
W......Page 975
Z......Page 976