Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 558 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
558
Dung lượng
2,63 MB
Nội dung
Programming C#
Jesse Liberty
Publisher: O'Reilly
First Edition July 2001
ISBN: 0-596-00117-7, 680 pages
The goal of C# is to serve as a high-performance language for .NET development—one that is simple,
safe, object-oriented, and Internet-centric. ProgrammingC# teaches this new language in a way that
experienced programmers will appreciate—by grounding its application firmly in the context of
Microsoft's .NET platform and the development of desktop and Internet applications.
2
Programming C#
3
Preface 11
About This Book 11
How the Book Is Organized 11
Who This Book Is For 13
C# Versus Visual Basic .NET 13
C# Versus Java 14
C# versus C++ 14
Conventions Used in This Book 14
Support 15
We'd Like to Hear from You 15
Acknowledgements 16
Part I: The C# Language 17
Chapter 1. C# and the .NET Framework 17
1.1 The .NET Platform 17
1.2 The .NET Framework 17
1.3 Compilation and the MSIL 19
1.4 The C# Language 19
Chapter 2. Getting Started:"Hello World" 21
2.1 Classes, Objects, and Types 21
2.2 Developing "Hello World" 26
Just In Time Compilation 29
2.3 Using the Visual Studio .NET Debugger 29
Chapter 3. C# Language Fundamentals 33
3.1 Types 33
The Stack and the Heap 34
3.2 Variables and Constants 36
WriteLine( ) 36
3.3 Expressions 42
3.4 Whitespace 42
3.5 Statements 43
Statement Blocks 46
All Operators Are Not Created Equal 47
Whitespace and Braces 53
3.6 Operators 56
Short-Circuit Evaluation 61
3.7 Namespaces 63
3.8 Preprocessor Directives 65
Chapter 4. Classes and Objects 69
4.1 Defining Classes 69
4.2 Creating Objects 73
4.3 Using Static Members 78
Static Methods to Access Static Fields 82
4.4 Destroying Objects 82
How Finalize Works 82
4.5 Passing Parameters 84
4.6 Overloading Methods and Constructors 89
4
4.7 Encapsulating Data with Properties 91
4.8 Readonly Fields 94
Chapter 5. Inheritance and Polymorphism 97
5.1 Specialization and Generalization 97
About the Unified Modeling Language 97
5.2 Inheritance 99
5.3 Polymorphism 102
5.4 Abstract Classes 107
5.5 The Root of all Classes: Object 110
5.6 Boxing and Unboxing Types 112
5.7 Nesting Classes 114
Chapter 6. Operator Overloading 117
6.1 Using the operator Keyword 117
6.2 Supporting Other .NET Languages 118
6.3 Creating Useful Operators 118
6.4 Logical Pairs 118
6.5 The Equals Operator 118
6.6 Conversion Operators 119
Chapter 7. Structs 125
7.1 Defining Structs 125
7.2 Creating Structs 127
Chapter 8. Interfaces 131
Mix Ins 131
8.1 Implementing an Interface 131
8.2 Accessing Interface Methods 141
8.3 Overriding Interface Implementations 147
8.4 Explicit Interface Implementation 150
Chapter 9. Arrays, Indexers, and Collections 159
9.1 Arrays 159
9.2 The foreach Statement 162
9.3 Indexers 175
9.4 Collection Interfaces 182
9.5 Array Lists 187
9.6 Queues 197
9.7 Stacks 199
9.8 Dictionaries 202
Load Factor 204
Chapter 10. Strings and Regular Expressions 209
10.1 Strings 209
Delimiter Limitations 222
10.2 Regular Expressions 222
Chapter 11. Handling Exceptions 233
11.1 Throwing and Catching Exceptions 233
11.2 Exception Objects 241
11.3 Custom Exceptions 244
11.4 Rethrowing Exceptions 246
Chapter 12. Delegates and Events 251
12.1 Delegates 251
Programming C#
5
12.2 Events 268
Part II: Programming with C# 277
Chapter 13. Building Windows Applications 277
13.1 Creating a Simple Windows Form 278
13.2 Creating a Windows Form Application 289
13.3 XML Documentation Comments 309
13.4 Deploying an Application 311
Chapter 14. Accessing Data with ADO.NET 321
14.1 Relational Databases and SQL 321
14.2 The ADO.Net Object Model 324
14.3 Getting Started with ADO.NET 325
14.4 Using ADO Managed Providers 328
14.5 Working with Data-Bound Controls 330
14.6 Changing Database Records 340
14.7 ADO.NET and XML 353
Chapter 15. ProgrammingWeb Applications with Web Forms 355
15.1 Understanding Web Forms 355
15.2 Creating a Web Form 358
15.3 Adding Controls 361
15.4 Data Binding 362
15.5 Responding to Postback Events 369
15.6 ASP.NET and C# 371
Chapter 16. Programming Web Services 373
16.1 SOAP, WSDL, and Discovery 373
16.2 Building a Web Service 374
WSDL and Namespaces 375
16.3 Creating the Proxy 379
Part III: C# and the .NET CLR 385
Chapter 17. Assemblies and Versioning 385
17.1 PE Files 385
17.2 Metadata 385
17.3 Security Boundary 385
17.4 Versioning 385
17.5 Manifests 386
17.6 Multi-Module Assemblies 387
17.7 Private Assemblies 395
17.8 Shared Assemblies 395
Public Key Encryption 397
Chapter 18. Attributes and Reflection 401
18.1 Attributes 401
18.2 Intrinsic Attributes 401
18.3 Custom Attributes 403
18.4 Reflection 407
18.5 Reflection Emit 416
Chapter 19. Marshaling and Remoting 437
19.1 Application Domains 438
19.2 Context 446
19.3 Remoting 448
6
Chapter 20. Threads and Synchronization 457
20.1 Threads 457
20.2 Synchronization 465
20.3 Race Conditions and Deadlocks 474
Chapter 21. Streams 477
21.1 Files and Directories 477
21.2 Reading and Writing Data 487
21.3 Asynchronous I/O 493
21.4 Network I/O 497
21.5 Web Streams 513
21.6 Serialization 516
21.7 Isolated Storage 523
Chapter 22. Programming .NET and COM 527
22.1 Importing ActiveX Controls 527
22.2 Importing COM Components 534
22.3 Exporting .NET Components 541
22.4 P/Invoke 543
22.5 Pointers 545
Appendix A. C# Keywords 551
Colophon 558
Programming C#
7
Programming C#
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
I: The C# Language
1. C# and the .NET Framework
1.1 The .NET Platform
1.2 The .NET Framework
1.3 Compilation and the MSIL
1.4 The C# Language
2. Getting Started:"Hello World"
2.1 Classes, Objects, and Types
2.2 Developing "Hello World"
2.3 Using the Visual Studio .NET Debugger
3. C# Language Fundamentals
3.1 Types
3.2 Variables and Constants
3.3 Expressions
3.4 Whitespace
3.5 Statements
3.6 Operators
3.7 Namespaces
3.8 Preprocessor Directives
4. Classes and Objects
4.1 Defining Classes
4.2 Creating Objects
4.3 Using Static Members
4.4 Destroying Objects
4.5 Passing Parameters
4.6 Overloading Methods and Constructors
4.7 Encapsulating Data with Properties
4.8 Readonly Fields
5. Inheritance and Polymorphism
5.1 Specialization and Generalization
5.2 Inheritance
8
5.3 Polymorphism
5.4 Abstract Classes
5.5 The Root of all Classes: Object
5.6 Boxing and Unboxing Types
5.7 Nesting Classes
6. Operator Overloading
6.1 Using the operator Keyword
6.2 Supporting Other .NET Languages
6.3 Creating Useful Operators
6.4 Logical Pairs
6.5 The Equals Operator
6.6 Conversion Operators
7. Structs
7.1 Defining Structs
7.2 Creating Structs
8. Interfaces
8.1 Implementing an Interface
8.2 Accessing Interface Methods
8.3 Overriding Interface Implementations
8.4 Explicit Interface Implementation
9. Arrays, Indexers, and Collections
9.1 Arrays
9.2 The foreach Statement
9.3 Indexers
9.4 Collection Interfaces
9.5 Array Lists
9.6 Queues
9.7 Stacks
9.8 Dictionaries
10. Strings and Regular Expressions
10.1 Strings
10.2 Regular Expressions
11. Handling Exceptions
11.1 Throwing and Catching Exceptions
11.2 Exception Objects
11.3 Custom Exceptions
11.4 Rethrowing Exceptions
12. Delegates and Events
12.1 Delegates
12.2 Events
II: Programming with C#
Programming C#
9
13. Building Windows Applications
13.1 Creating a Simple Windows Form
13.2 Creating a Windows Form Application
13.3 XML Documentation Comments
13.4 Deploying an Application
14. Accessing Data with ADO.NET
14.1 Relational Databases and SQL
14.2 The ADO.Net Object Model
14.3 Getting Started with ADO.NET
14.4 Using ADO Managed Providers
14.5 Working with Data-Bound Controls
14.6 Changing Database Records
14.7 ADO.NET and XML
15. ProgrammingWeb Applications with Web Forms
15.1 Understanding Web Forms
15.2 Creating a Web Form
15.3 Adding Controls
15.4 Data Binding
15.5 Responding to Postback Events
15.6 ASP.NET and C#
16. Programming Web Services
16.1 SOAP, WSDL, and Discovery
16.2 Building a Web Service
16.3 Creating the Proxy
III: C# and the .NET CLR
17. Assemblies and Versioning
17.1 PE Files
17.2 Metadata
17.3 Security Boundary
17.4 Versioning
17.5 Manifests
17.6 Multi-Module Assemblies
17.7 Private Assemblies
17.8 Shared Assemblies
18. Attributes and Reflection
18.1 Attributes
18.2 Intrinsic Attributes
18.3 Custom Attributes
18.4 Reflection
18.5 Reflection Emit
19. Marshaling and Remoting
19.1 Application Domains
19.2 Context
10
19.3 Remoting
20. Threads and Synchronization
20.1 Threads
20.2 Synchronization
20.3 Race Conditions and Deadlocks
21. Streams
21.1 Files and Directories
21.2 Reading and Writing Data
21.3 Asynchronous I/O
21.4 Network I/O
21.5 Web Streams
21.6 Serialization
21.7 Isolated Storage
22. Programming .NET and COM
22.1 Importing ActiveX Controls
22.2 Importing COM Components
22.3 Exporting .NET Components
22.4 P/Invoke
22.5 Pointers
A. C# Keywords
Colophon
[...]... from it in C# 1.4 The C# Language The C# language is disarmingly simple, with only about 80 keywords and a dozen built-in datatypes, but C# is highly expressive when it comes to implementing modern programming concepts C# includes all the support for structured, component-based, object-oriented programming that one expects of a modern language built on the shoulders of C++ and Java The C# language... grandparents, you can easily see in C# the influence of Java, C++, Visual Basic (VB), and other languages The focus of this book is the C# language and its use as a tool for programming on the NET platform In my primers on C++,[1] I advocate learning the language first, without regard to Windows or Unix programming With C# that approach would be pointless You learn C# specifically to create NET applications;... manuscript and saved me from a series of embarrassing errors and omissions I am deeply grateful 16 ProgrammingC# Part I: The C# Language Chapter 1 C# and the NET Framework The goal of C# is to provide a simple, safe, modern, object-oriented, Internet-centric, highperformance language for NET development C# is a new language, but it draws on the lessons learned over the past three decades In much the... introduction should get you started You can answer many programming questions by writing short demonstration programs and examining them in the debugger A good debugger is, in some ways, the single most powerful teaching tool for a programming language 31 32 ProgrammingC# Chapter 3 C# Language Fundamentals Chapter 2 demonstrates a very simple C# program Nonetheless, there is sufficient complexity in.. .Programming C# Preface Every 10 years or so a new approach to programming hits like a tsunami In the early 1980s, the new technologies were Unix, which could be run on a desktop, and a powerful new language called C, developed by AT&T The early 90's brought Windows and C++ Each of these developments represented a sea change in the way you approached programming .NET and C# are the next... of object-oriented programming In C# everything pertaining to a class declaration is found in the declaration itself C# class definitions do not require separate header files or Interface Definition Language (IDL) files Moreover, C# supports a new XML style of inline documentation that greatly simplifies the creation of online and print reference documentation for an application C# also supports interfaces,... book does not consider C# in a vacuum but places the language firmly in the context of Microsoft's NET platform and in the development of desktop and Internet applications [1] See Sams Teach Yourself C++ in 21 Days, also by JesseLiberty This chapter introduces both the C# language and the NET platform, including the NET Framework 1.1 The NET Platform When Microsoft announced C# in July 2000, its unveiling... namespaces and a short tutorial on the C# precompiler Although C# is principally concerned with the creation and manipulation of objects, it is best to start with the fundamental building blocks: the elements from which objects are created These include the built-in types that are an intrinsic part of the C# language as well as the syntactic elements of C# 3.1 Types C# is a strongly typed language In a... the C# primitive types to the underlying NET type ensures that objects created in C# can be used interchangeably with objects created in any other language compliant with the NET CLS, such as VB NET 33 Each type has a specific and unchanging size Unlike with C++, a C# int is always 4 bytes because it maps to an Int32 in the NET CLS Table 3-1 lists the built-in value types offered by C# Table 3-1 C#. .. book concentrates on the C# language Part II details how to write NET programs, and Part III describes how to use C# with the NET Common Language Runtime library Part I Chapter 1, introduces you to the C# language and the NET platform Chapter 2 demonstrates a simple program, to provide a context for what follows, and introduces you to the Visual Studio IDE and a number of C# language concepts Chapter .
Programming C#
Jesse Liberty
Publisher: O'Reilly
First Edition July 2001
ISBN: 0-596-00117-7, 680 pages
The goal of C# is to serve. P/Invoke 543
22.5 Pointers 545
Appendix A. C# Keywords 551
Colophon 558
Programming C#
7
Programming C#
Preface
About This Book
How the