About the Book
Get the in-depth architectural
information you need about the hottest OOP language for Microsoft®
.NET—now updated for final release code.
Take a detailed look at the internal architecture
of the groundbreaking C# language with this architectural reference. It’s
fully updated with information about the Microsoft® .NET platform and
Microsoft Visual Studio® .NET. It’s packed with sample code and demo
applications to show you exactly how to develop with C#. You’ll explore
this advanced language and its design parameters and construction to gain
a complete understanding of how it works—and why it works that way. Topics
covered include:
C# CLASS FUNDAMENTALS
• Building C# applications and libraries
• The .NET type system
• Classes and structs
• Methods, properties, arrays, indexers,
and attributes
• Interfaces
WRITING CODE
• Expressions and operators
• Program flow control
• String handling and regular expressions
• File I/O with streams
• Error handling with exceptions
• Operator overloading and user-defined
conversions
• Delegates and event handlers
• Documentation with XML
ADVANCED C#
• Numerical processing and the Math class
• Collections and object enumeration
• Multithreaded programming
• Querying metadata with reflection
• Deterministic finalization and the Dispose
pattern
• Pinning and memory management
• Using COM from C# applications
• .NET components in unmanaged code
• Incorporating security
CD-ROM FEATURES:
• A fully searchable electronic copy of
the book
Related Books
Inside Microsoft® Visual Studio® .NET
2003
Microsoft® ADO.NET (Core Reference)
Table of Contents
Foreword xix
Introduction xxiii
PART 1 C# CLASS FUNDAMENTALS
1 Building C# Applications
and Libraries 3
"Hello,
World"—The Command-Line Version 4
Using the Command-Line Compiler 4
"Hello,
World" Code Walk-Through 7
One-Stop Programming 7
Namespaces 8
Classes and Members 10
The Main Method 11
The System.Console.WriteLine Method 11
Namespaces and the using Directive 12
Skeleton Code 13
Class Ambiguity 14
"Hello,
World"—The Visual Studio .NET Version 15
Building
and Running .NET Applications 18
Inside
"Hello, World" 20
Working
with Assemblies and Modules 24
Assembly Overview 25
Benefits of Assemblies 26
Building Assemblies 27
Creating Shared Assemblies 32
Working with the Global Assembly Cache 35
Summary
37
2 The .NET Type System
39
Everything
Is an Object 40
The Root of All Types: System.Object 41
Value
Types and Reference Types 43
Value Types 43
Reference Types 44
Boxing
and Unboxing 45
Converting from Value Types to Reference Types 45
Converting from Reference Types to Value Types 46
More Boxing Examples 48
Types
and Aliases 53
Casting
Between Types 54
CTS Benefits
56
Language Interoperability 57
Singly Rooted Object Hierarchy 57
Type Safety 58
Summary
58
3 Classes and Structs
59
Defining
Classes 59
Class
Members 60
Access
Modifiers 62
The Main
Method 63
Command-Line Arguments 64
Returning Values from Main 66
Multiple Main Methods 67
Constructors
68
Static Members and Instance Members 71
Constructor Initializers 73
Specifying Run-Time Information in a Constructor Initializer 78
Constants
vs. Read-Only Fields 81
Constants 81
Read-Only Fields 83
Inheritance
87
Multiple Interfaces 90
Sealed Classes 91
Defining
Structs in C# 92
Struct Usage 92
Guidelines to Using Structs 95
Summary
97
4 Methods
99
Value
and Reference Parameters 100
ref Method Parameters 101
out Method Parameters 105
Value
and Reference Parameters (Again) 109
Method
Overloading 113
Overloading Constructors 115
Inheritance and Overloading 118
Variable
Method Parameters 119
Virtual
Methods 122
Method Overriding 123
Polymorphism 124
new and virtual Methods 131
Calling Virtual Methods from Constructors 134
Static
Methods 136
Access to Class Members 137
Static Constructors 138
Summary
140
5 Properties, Arrays, and
Indexers 141
Properties
as Smart Fields 142
Defining and Using Properties 143
Inside Properties 145
Inheriting Properties 150
Advanced Use of Properties 155
Arrays
156
Declaring Arrays 156
Single-Dimensional Array Example 157
Multidimensional Arrays 158
Querying for Rank 160
Jagged Arrays 162
Treating
Objects Like Arrays by Using Indexers 164
Defining Indexers 165
Indexer Example 165
Inside Indexers 167
Design Guidelines 169
Summary
170
6 Attributes
171
Introducing
Attributes 172
Defining
Attributes 173
Querying
for Attributes 177
Class Attributes 177
Method Attributes 180
Field Attributes 182
Attribute
Parameters 184
Positional Parameters and Named Parameters 184
Common Mistakes with Named Parameters 186
Valid Attribute Parameter Types 187
The AttributeUsage
Attribute 187
Defining an Attribute Target 188
Single-Use and Multiuse Attributes 190
Specifying Inheritance Attribute Rules 191
Attribute
Identifiers 193
Predefined
Attributes 195
Conditional Attribute 197
Obsolete Attribute 199
CLSCompliant Attribute 200
DllImport and StructLayout Attributes 201
Assembly Attributes 203
Context
Attributes 205
Summary
212
7 Interfaces
213
Interface
Use 214
Declaring
Interfaces 215
Implementing
Interfaces 217
Querying for Implementation by Using is 219
Querying for Implementation by Using as 224
Interfaces vs. the Alternatives 227
Explicit
Interface Member Name Qualification 230
Name Hiding with Interfaces 230
Avoiding Name Ambiguity 233
Interfaces
and Inheritance 238
Combining
Interfaces 242
Summary
245
PART II WRITING CODE
8 Expressions and Operators
249
Operators
Defined 249
Operator
Precedence 250
How C# Determines Precedence 251
Left and Right Associativity 252
Practical Usage 253
C# Operators
253
Primary Expression Operators 253
Mathematical Operators 259
Numeric
Conversions 269
checked and unchecked Context 271
Bitwise
Operators 272
Relational Operators 276
Simple Assignment Operators 278
The Conditional Operator 282
Summary
284
9 Program Flow Control
285
Selection
Statements 285
The if Statement 285
The switch Statement 292
Iteration
Statements 301
The while Statement 301
The do/while Statement 302
The for Statement 305
The foreach Statement 310
Branching
with Jump Statements 313
The break Statement 313
The continue Statement 317
The Infamous goto Statement 319
The return Statement 326
Summary
327
10 String Handling and
Regular Expressions 329
Strings
330
String Formatting 333
Format Specifiers 334
Objects and ToString 338
Numeric String Parsing 339
Strings and DateTime 341
Encoding Strings 345
The StringBuilder Class 346
Splitting Strings 347
Extending Strings 348
String Interning 350
Regular
Expressions 353
Match and MatchCollection 358
Groups and Captures 361
String-Modifying Expressions 363
Regular Expression Options 367
Compiling Regular Expressions 368
Summary
372
11 File I/O with Streams
373
Stream
Classes 373
FileStream 375
StreamReader and StreamWriter 379
Memory and Buffered Streams 381
String Readers and Writers 384
Binary Readers and Writers 387
File
System Classes 389
Directory and DirectoryInfo 389
File and FileInfo 392
Parsing Paths 394
Nonconsole
Use of Streams 395
Windows OpenFileDialog 395
Reading Web Pages 397
Serialization
399
Serializing with BinaryFormatter 399
Serializing with SoapFormatter 403
Serializing with XmlSerializer 404
Implementing Iserializable 406
Summary
409
12 Error Handling with
Exceptions 411
Overview
of Exception Handling 411
Basic
Exception-Handling Syntax 413
Throwing an Exception 413
Catching an Exception 414
Rethrowing an Exception 418
Cleaning Up with finally 420
Retrying
Code 424
Comparing
Error-Handling Techniques 426
Benefits of Exception Handling Over Return Codes 427
Handling Errors in the Correct Context 429
Improving Code Readability 430
Throwing Exceptions from Constructors 432
Using
the System.Exception Class 432
Constructing an Exception Object 432
Using the StackTrace Property 436
Catching Multiple Exception Types 437
Deriving Your Own Exception Classes 438
Designing
Your Code with Exception Handling 441
Design Issues with the try Block 441
Design Issues with the catch Block 445
Summary
448
13 Operator Overloading
and User-Defined Conversions 449
Operator
Overloading 450
Operator Overloading Syntax 450
Operator Overloading Rules and Restrictions 451
Operator Overloading Examples 453
RGB Color Incrementing Example 456
Operator Overloading Design Guidelines 462
User-Defined
Conversions 464
User-Defined Conversion Syntax 465
User-Defined Conversion Rules and Restrictions 465
User-Defined Conversion Examples 465
Summary
479
14 Delegates and Event
Handlers 481
Using
Delegates as Callback Methods 481
Inside
Delegates 485
Defining
Delegates as Static Members 489
Creating
Delegates Only When Needed 492
Multicast
Delegates 495
Multicast Delegate Justification 501
Defining
Events with Multicast Delegates 504
Summary
509
15 Documentation with XML
511
Getting
Started 511
Adding Elements 514
Acceptable Code Constructs 514
Compiler-Generated
Element IDs 515
Fields, Properties, Events, and Indexers 516
Methods 518
Well-Formed
XML 523
Comment
Web Pages 525
Element
Tags and Attributes 526
The <exception> Tag and cref Attribute 528
The <c>, <code>, and <example> Tags 529
The <include> Tag 530
The <list> Tag 533
Custom
Formatting 534
XML and
Data 537
Summary
539
ADVANCED C#
16 Numerical Processing
and the Math Class 543
Numeric
Support in C# and .NET 543
Is the Decimal Type a Primitive? 545
Numeric Suffixes 547
A Numeric Type by Any Other Name. 547
More on Numeric Literals 549
Integral
Ranges and Overflow Rules 552
The Decimal
Type 557
The System.Math
Class 558
System.Math
Constants 558
Working with a Number's Sign 558
Minimum and Maximum Values 559
Methods for Rounding and Truncating 560
Summary
560
17 Collections and Object
Enumeration 563
Implementing
the Enumeration Interface 563
Using an Enumerator Object 568
Using the foreach Statement with Collections 569
Two Interfaces
for the Price of One 571
Constructing
Enumerator Objects 572
Creating
Versioned Enumerators 573
Combining
IEnumerable and IEnumerator 577
Protecting
Data While Allowing Enumeration 579
Value
Types 582
Performance Issues 583
Modified Collected Value Types 592
Summary
596
18 Multithreading and Asynchronous
Programming 597
Getting
Started with Threads 597
Basic
Thread-Handling Chores 599
Creating Threads and Thread Objects 599
Managing Thread Lifetimes 599
Destroying Threads 601
Scheduling Threads 605
Communicating
Data to a Thread 610
Thread
Safety and Synchronization 612
Protecting Code by Using the Monitor Class 613
Using Monitor Locks with the C# lock Statement 620
Synchronizing Code by Using the Mutex Class 622
Using
Delegates to Call Asynchronous Methods 624
Thread
Safety and the .NET Classes 630
Threading
Guidelines 630
When to Use Threads 631
When Not to Use Threads 632
Summary
632
19 Querying Metadata with
Reflection 635
The Reflection
API Hierarchy 635
The Type
Class 636
Retrieving
a Type Reference 636
Retrieving the Type of an Instance 637
Retrieving the Type from a Name 637
Interrogating Types 639
Working
with Assemblies and Modules 642
Iterating Through the Types of an Assembly 642
Listing an Assembly's Modules 648
Late
Binding with Reflection 650
Implementing
an Abstract Factory with Reflection 653
Dynamic
Code Generation 656
Summary
660
20 Pinning and Memory Management
661
Garbage
Collection 662
Overriding
Finalize 663
Overriding Finalize in Derived Classes 667
Forcing
Garbage Collection 670
The Dispose
Pattern 672
Other GC Features 675
The IDisposable
Interface 676
Derived Disposable Classes 678
Protecting Against Double Disposal 680
Language Support for Dispose 682
Garbage
Collector Generations 683
Weak
References 685
Unsafe
Code 687
Using Pointers in C# 688
Pinning
692
Pinning Array Elements 694
The Dereference Member Operator 699
Using stackalloc 700
Summary
702
21 Using COM from C# Applications
703
Where
COM Fits in Today's .NET World 704
Using
COM Components from C# 705
Creating an ATL Component 705
Consuming a COM Component from a .NET Application 707
Binding to and Using the COM Component 709
Dynamic Type Discovery with COM Components 711
Late Binding to COM Objects 713
Event
Handling 715
How COM Connection Points Work 715
Creating an ATL COM Component That Sources Events 717
Event Handling Using Delegates 719
Sinking Unmanaged COM Events in a .NET Application 722
Using
COM Collections 727
Creating a COM Collection Component Using ATL 727
Consuming COM Collections in a .NET Application 732
Enumerating Elements in a .NET Collection 734
Reusing COM Components in Managed Code 736
Containment 736
Aggregation 737
Reuse Through Mixed-Mode Inheritance 738
Reuse Through Mixed-Mode Containment 742
A .NET View on COM Threading Models and Apartments 743
Mapping
Method Keywords to IDL Attributes 745
Summary
746
22 Using .NET Components
in Unmanaged Code 749
Creating
and Using .NET Components via COM 749
Generating a Typelib from the Assembly and Registering the Assembly
751
Consuming the Component from a Visual Basic 6 Client 752
Shedding More Light on the COM Interop Wizardry 753
Snooping on the Generated Typelib 756
Using
Attributes to Tweak the Generated Typelib Metadata 763
Altering the Interface Type 763
Altering the GUID and the ProgID 765
Hiding Public Types from Exposure to COM 766
Altering the Marshaling Behavior for Types 767
Exception
Handling: .NET vs. COM 768
Handling
Events from .NET Components in Unmanaged Event Sinks 771
Creating a .NET Component that Sources Events 772
Handling Events in a Visual Basic Client Application 775
Thread
Affinity in .NET Components 776
Thread-Neutral Behavior When Accessed by Unmanaged COM-Aware Clients
777
Summary
782
23 Security
783
.NET
Security 783
Threats to Security 784
Verifiable
Type Safety 786
Code
Signing 791
Private Assemblies 792
Strong-Named Assemblies 794
The Global Assembly Cache 800
Delay-Signed Assemblies 802
Cryptographic
Services 803
Code
Access Security 807
Evidence 808
Security Policy 809
Configuring Security 814
CASpol 822
Imperative and Declarative CAS 825
Identity Permissions 827
Assembly Permission Requests 829
Role-Based
Security 831
PrincipalPermission Requests 833
Impersonation 834
Isolated
Storage 835
Summary
838
A MSIL Instruction Table
839
INDEX 855 |