Release TeamOR
[x] .NET
Preface ...............................................................................
About This Book .............................................................
How the Book Is Organized ............................................
Who This Book Is For .....................................................
C# Versus Visual Basic .NET .........................................
C# Versus Java ..............................................................
C# versus C++ ................................................................
Conventions Used in This Book ......................................
Support ...........................................................................
We’d Like to Hear from You ............................................
Acknowledgements .........................................................
2
2
2
5
5
5
5
6
6
7
7
Part I: The C# Language ...................................................
8
Chapter 1. C# and the .NET Framework ..........................
The .NET Platform ..........................................................
The .NET Framework .....................................................
Compilation and the MSIL ..............................................
The C# Language ...........................................................
8
8
9
10
11
Chapter 2. Getting Started:"Hello World" .......................
Classes, Objects, and Types ..........................................
Developing "Hello World" ................................................
12
12
17
Just In Time Compilation .................................................
Using the Visual Studio .NET Debugger .........................
20
20
Chapter 3. C# Language Fundamentals .........................
Types ..............................................................................
22
23
The Stack and the Heap ...................................................
Variables and Constants .................................................
24
26
WriteLine( ) ........................................................................
Expressions ....................................................................
Whitespace .....................................................................
Statements ......................................................................
26
32
32
33
Statement Blocks ..............................................................
36
All Operators Are Not Created Equal ..............................
37
Whitespace and Braces....................................................
Operators ........................................................................
43
46
Short-Circuit Evaluation ...................................................
Namespaces ...................................................................
Preprocessor Directives ..................................................
51
53
54
Chapter 4. Classes and Objects ......................................
Defining Classes .............................................................
Creating Objects .............................................................
Using Static Members .....................................................
57
58
62
67
Static Methods to Access Static Fields ..........................
Destroying Objects .........................................................
70
70
How Finalize Works ..........................................................
Passing Parameters .......................................................
Overloading Methods and Constructors .........................
Encapsulating Data with Properties ................................
Readonly Fields ..............................................................
71
73
77
80
82
Chapter 5. Inheritance and Polymorphism .....................
Specialization and Generalization ..................................
83
84
About the Unified Modeling Language ...........................
Inheritance ......................................................................
Polymorphism .................................................................
Abstract Classes .............................................................
The Root of all Classes: Object ......................................
Boxing and Unboxing Types ...........................................
Nesting Classes ..............................................................
Nesting Classes ..............................................................
Using the operator Keyword ...........................................
Supporting Other .NET Languages ................................
Creating Useful Operators ..............................................
Logical Pairs ...................................................................
The Equals Operator ......................................................
Conversion Operators .....................................................
84
86
89
94
97
99
101
102
104
105
105
105
105
106
Chapter 7. Structs ............................................................. 111
Defining Structs .............................................................. 111
Creating Structs .............................................................. 113
Chapter 8. Interfaces ........................................................ 117
Mix Ins ................................................................................ 117
Implementing an Interface .............................................. 117
Accessing Interface Methods .......................................... 127
Overriding Interface Implementations ............................. 133
Explicit Interface Implementation .................................... 136
Chapter 9. Arrays, Indexers, and Collections ................
Arrays .............................................................................
The foreach Statement ...................................................
Indexers ..........................................................................
Collection Interfaces .......................................................
Array Lists .......................................................................
Queues ...........................................................................
Stacks .............................................................................
Dictionaries .....................................................................
144
144
148
160
168
172
182
184
187
Load Factor ....................................................................... 188
Chapter 10. Strings and Regular Expressions ............... 192
Strings ............................................................................. 192
Delimiter Limitations ........................................................ 205
Regular Expressions ....................................................... 205
Chapter 11. Handling Exceptions ....................................
Throwing and Catching Exceptions ................................
Exception Objects ...........................................................
Custom Exceptions .........................................................
Rethrowing Exceptions ...................................................
214
215
223
225
227
Chapter 12. Delegates and Events .................................. 230
Delegates ........................................................................ 231
Events ............................................................................. 248
Chapter 13. Building Windows Applications.................. 256
Creating a Simple Windows Form .................................. 257
Creating a Windows Form Application ............................ 268
It’s Turtles, All the Way Down .......................................... 274
XML Documentation Comments ..................................... 288
Chapter 14. Accessing Data with ADO.NET ...................
Relational Databases and SQL ......................................
The ADO.Net Object Model ............................................
Getting Started with ADO.NET .......................................
Using ADO Managed Providers ......................................
Working with Data-Bound Controls .................................
289
290
293
294
297
300
Changing Database Records .......................................... 309
ADO.NET and XML ........................................................ 322
Chapter 15. ProgrammingWeb Applications with
Web Forms ........................................................................
Understanding Web Forms .............................................
Creating a Web Form .....................................................
Adding Controls ..............................................................
Data Binding ...................................................................
Responding to Postback Events .....................................
ASP.NET and C# ............................................................
322
322
325
328
330
337
339
Chapter 16. Programming Web Services ........................ 339
SOAP, WSDL, and Discovery ......................................... 339
Building a Web Service ................................................... 340
WSDL and Namespaces ................................................... 342
Creating the Proxy .......................................................... 346
Part III: C# and the .NET CLR ........................................... 349
Chapter 17. Assemblies and Versioning ........................
PE Files ..........................................................................
Metadata .........................................................................
Security Boundary ..........................................................
Versioning .......................................................................
Manifests ........................................................................
Multi-Module Assemblies ................................................
Private Assemblies .........................................................
Shared Assemblies .........................................................
349
349
349
350
350
350
352
359
359
Public Key Encryption ...................................................... 361
Chapter 18. Attributes and Reflection .............................
Attributes .........................................................................
Intrinsic Attributes ...........................................................
Custom Attributes ...........................................................
Reflection ........................................................................
Reflection Emit ................................................................
364
364
365
366
370
379
Chapter 19. Marshaling and Remoting ...........................
Application Domains .......................................................
Context ...........................................................................
Remoting ........................................................................
400
401
409
411
Programming C#
sizeof
An operator that returns the size in bytes of a struct.
stackalloc
An operator that returns a pointer to a specified number of value types allocated on the
stack.
static
A type member modifier that indicates that the member applies to the type rather than an
instance of the type.
string
A predefined reference type that represents an immutable sequence of Unicode characters.
struct
A value type that combines data and functionality in one unit.
switch
A selection statement that allows a selection of choices to be made based on the value of a
predefined type.
this
A variable that references the current instance of a classor struct.
throw
A jump statement that throws an exception when an abnormal condition has occurred.
true
A Boolean literal.
try
A statement that provides a way to handle an exception or a premature exit in a statement
block.
typeof
An operator that returns the type of an object as a System.Type object.
uint
A four-byte unsigned integral datatype.
page 514
Programming C#
ulong
An eight-byte unsigned integral datatype.
unchecked
A statement or operator that prevents arithmetic bounds checking on an expression.
unsafe
A method modifier or statement that permits pointer arithmetic to be performed within a
particular block.
ushort
A two-byte unsigned integral datatype.
using
Specifies that types in a particular namespace can be referred to without requiring their fully
qualified type names. The using statement defines a scope. At the end of the scope the object
is disposed.
value
The name of the implicit variable set by the set accessor of a property.
virtual
A class method modifier that indicates that a method can be overridden by a derived class.
void
A keyword used in place of a type for methods that don't have a return value.
volatile
Indicates that a field may be modified by the operating system or another thread.
while
A loop statement to iterate a statement block while an expression at the start of each
iteration evaluates to false.
page 515