logo资料库

Programming C# 5.0-Building Windows 8 无水印pdf.pdf

第1页 / 共884页
第2页 / 共884页
第3页 / 共884页
第4页 / 共884页
第5页 / 共884页
第6页 / 共884页
第7页 / 共884页
第8页 / 共884页
资料共884页,剩余部分请下载后查看
Cover
Copyright
Table of Contents
Preface
Who This Book Is For
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
1. Introducing C#
Why C#?
Why Not C#?
C#'s Defining Features
Managed Code and the CLR
Generality Trumps Specialization
Asynchronous Programming
Visual Studio
Anatomy of a Simple Program
Adding a Project to an Existing Solution
Referencing One Project from Another
Writing a Unit Test
Namespaces
Classes
Program Entry Point
Unit Tests
Summary
2. Basic Coding in C#
Local Variables
Scope
Statements and Expressions
Statements
Expressions
Comments and Whitespace
Preprocessing Directives
Compilation Symbols
#error and #warning
#line
#pragma
#region and #endregion
Intrinsic Data Types
Numeric Types
Booleans
Strings and Characters
Object
Operators
Flow Control
Boolean Decisions with if Statements
Multiple Choice with switch Statements
Loops: while and do
C-Style for Loops
Collection Iteration with foreach Loops
Summary
3. Types
Classes
Static Members
Static Classes
Reference Types
Structs
When to Write a Value Type
Members
Fields
Constructors
Methods
Properties
Indexers
Operators
Events
Nested Types
Interfaces
Enums
Other Types
Anonymous Types
Partial Types and Methods
Summary
4. Generics
Generic Types
Constraints
Type Constraints
Reference Type Constraints
Value Type Constraints
Multiple Constraints
Zero-Like Values
Generic Methods
Type Inference
Inside Generics
Summary
5. Collections
Arrays
Array Initialization
Variable Argument Count with the params Keyword
Searching and Sorting
Multidimensional Arrays
Copying and Resizing
List
List and Sequence Interfaces
Implementing Lists and Sequences
Iterators
Collection
ReadOnlyCollection
Dictionaries
Sorted Dictionaries
Sets
Queues and Stacks
Linked Lists
Concurrent Collections
Tuples
Summary
6. Inheritance
Inheritance and Conversions
Interface Inheritance
Generics
Covariance and Contravariance
System.Object
The Ubiquitous Methods of object
Accessibility and Inheritance
Virtual Methods
Abstract Methods
Sealed Methods and Classes
Accessing Base Members
Inheritance and Construction
Special Base Types
Summary
7. Object Lifetime
Garbage Collection
Determining Reachability
Accidentally Defeating the Garbage Collector
Weak References
Reclaiming Memory
Garbage Collector Modes
Accidentally Defeating Compaction
Forcing Garbage Collections
Destructors and Finalization
Critical Finalizers
IDisposable
Optional Disposal
Boxing
Boxing Nullable
Summary
8. Exceptions
Exception Sources
Exceptions from APIs
Exceptions from Your Code
Failures Detected by the Runtime
Handling Exceptions
Exception Objects
Multiple catch Blocks
Nested try Blocks
finally Blocks
Throwing Exceptions
Rethrowing Exceptions
Failing Fast
Exception Types
Custom Exceptions
Unhandled Exceptions
Debugging and Exceptions
Asynchronous Exceptions
Summary
9. Delegates, Lambdas, and Events
Delegate Types
Creating a Delegate
Multicast Delegates
Invoking a Delegate
Common Delegate Types
Type Compatibility
Behind the Syntax
Inline Methods
Captured Variables
Lambdas and Expression Trees
Events
Standard Event Delegate Pattern
Custom Add and Remove Methods
Events and the Garbage Collector
Events Versus Delegates
Delegates Versus Interfaces
Summary
10. LINQ
Query Expressions
How Query Expressions Expand
Supporting Query Expressions
Deferred Evaluation
LINQ, Generics, and IQueryable
Standard LINQ Operators
Filtering
Select
SelectMany
Ordering
Containment Tests
Specific Items and Subranges
Aggregation
Set Operations
Whole-Sequence, Order-Preserving Operations
Grouping
Joins
Conversion
Sequence Generation
Other LINQ Implementations
Entity Framework
LINQ to SQL
WCF Data Services Client
Parallel LINQ (PLINQ)
LINQ to XML
Reactive Extensions
Summary
11. Reactive Extensions
Rx and .NET Versions
Fundamental Interfaces
IObserver
IObservable
Publishing and Subscribing with Delegates
Creating an Observable Source with Delegates
Subscribing to an Observable Source with Delegates
Sequence Builders
Empty
Never
Return
Throw
Range
Repeat
Generate
LINQ Queries
Grouping Operators
Join Operators
SelectMany Operator
Aggregation and Other Single-Value Operators
Concat Operator
Rx Query Operators
Merge
Windowing Operators
The Scan Operator
The Amb Operator
DistinctUntilChanged
Schedulers
Specifying Schedulers
Built-in Schedulers
Subjects
Subject
BehaviorSubject
ReplaySubject
AsyncSubject
Adaptation
IEnumerable
.NET Events
Asynchronous APIs
Timed Operations
Interval
Timer
Timestamp
TimeInterval
Throttle
Sample
Timeout
Windowing Operators
Delay
DelaySubscription
Summary
12. Assemblies
Visual Studio and Assemblies
Anatomy of an Assembly
.NET Metadata
Resources
Multifile Assemblies
Other PE Features
Type Identity
Loading Assemblies
Explicit Loading
The Global Assembly Cache
Assembly Names
Strong Names
Version
Culture
Processor Architecture
Portable Class Libraries
Packaged Deployment
Windows 8 UI–Style Apps
ClickOnce and XBAP
Silverlight and Windows Phone Apps
Protection
Summary
13. Reflection
Reflection Types
Assembly
Module
MemberInfo
Type and TypeInfo
MethodBase, ConstructorInfo, and MethodInfo
ParameterInfo
FieldInfo
PropertyInfo
EventInfo
Reflection Contexts
Summary
14. Dynamic Typing
The dynamic Type
dynamic and Interoperability
Silverlight and Scriptable Objects
Dynamic .NET Languages
Inside Dynamic
Restrictions on the dynamic Type
Custom Dynamic Objects
ExpandoObject
Limitations of dynamic
Summary
15. Attributes
Applying Attributes
Attribute Targets
Compiler-Handled Attributes
CLR-Handled Attributes
Defining and Consuming Custom Attributes
Attribute Type
Retrieving Attributes
Summary
16. Files and Streams
The Stream Class
Position and Seeking
Flushing
Copying
Length
Disposal
Asynchronous Operation
Concrete Stream Types
Windows 8 and IRandomAccessStream
Text-Oriented Types
TextReader and TextWriter
Concrete Reader and Writer Types
Encoding
Files and Directories
FileStream Class
File Class
Directory Class
Path Class
FileInfo, DirectoryInfo, and FileSystemInfo
Known Folders
Serialization
BinaryReader and BinaryWriter
CLR Serialization
Data Contract Serialization
XmlSerializer
Summary
17. Multithreading
Threads
Threads, Variables, and Shared State
The Thread Class
The Thread Pool
Thread Affinity and SynchronizationContext
Synchronization
Monitors and the lock Keyword
SpinLock
Reader/Writer Locks
Event Objects
Barrier
CountdownEvent
Semaphores
Mutex
Interlocked
Lazy Initialization
Other Class Library Concurrency Support
Tasks
The Task and Task Classes
Continuations
Schedulers
Error Handling
Custom Threadless Tasks
Parent/Child Relationships
Composite Tasks
Other Asynchronous Patterns
Cancellation
Parallelism
The Parallel Class
Parallel LINQ
TPL Dataflow
Summary
18. Asynchronous Language Features
Asynchronous Keywords: async and await
Execution and Synchronization Contexts
Multiple Operations and Loops
Returning a Task
Applying async to Nested Methods
The await Pattern
Error Handling
Validating Arguments
Singular and Multiple Exceptions
Concurrent Operations and Missed Exceptions
Summary
19. XAML
XAML-Based Frameworks
WPF
Silverlight
Windows Phone 7
Windows Runtime and Windows 8 UI–Style Apps
XAML Basics
XAML and XML Namespaces
Generated Classes and Codebehind
Child Elements
Property Elements
Event Handling
Threading
Layout
Properties
Panels
ScrollViewer
Layout Events
Controls
Content Controls
Slider and ScrollBar Controls
Progress Controls
List Controls
Control Templates
UserControls
Text
Displaying Text
Editing Text
Data Binding
Data Templates
Graphics
Shapes
Bitmaps
Media
Styles
Summary
20. ASP.NET
Razor
Expressions
Flow Control
Code Blocks
Explicitly Indicated Content
Page Classes and Objects
Using Other Components
Layout Pages
Start Pages
Web Forms
Server-Side Controls
Expressions
Code Blocks
Standard Page Objects
Page Classes and Objects
Using Other Components
Master Pages
MVC
Typical MVC Project Layout
Writing Models
Writing Views
Writing Controllers
Handling Additional Input
Generating Action Links
Routing
Summary
21. Interoperability
Calling Native Code
Marshaling
32-bit and 64-bit
Safe Handles
Security
Platform Invoke
Calling Convention
Text Handling
Entry Point Name
COM-Style Return Values
Win32 Error Handling
COM
RCW Lifetime
Metadata
Scripting
Windows Runtime
Metadata
Windows Runtime Types
Buffers
Unsafe Code
C++/CLI and the Component Extensions
Summary
Index
About the Author
Programming C# 5.0 Ian Griffiths
Programming C# 5.0 by Ian Griffiths Copyright © 2013 Ian Griffiths. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Rachel Roumeliotis Production Editor: Kristen Borg Copyeditor: Rachel Monaghan Proofreader: Linley Dolby Indexer: Ellen Troutman Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Rebecca Demarest October 2012: First Edition Revision History for the First Edition: 2012-10-10 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449320416 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming C# 5.0, the image of an African crowned crane, and related trade dress are trade­ marks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade­ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-32041-6 [LSI]
I dedicate this book to my excellent wife Deborah, and to my wonderful daughter, Hazel, who arrived while this book was a work in progress.
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 v
#line 50 #pragma 50 #region and #endregion 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 vi | Table of Contents
分享到:
收藏