logo资料库

C# in Depth, 4th Edition.pdf

第1页 / 共528页
第2页 / 共528页
第3页 / 共528页
第4页 / 共528页
第5页 / 共528页
第6页 / 共528页
第7页 / 共528页
第8页 / 共528页
资料共528页,剩余部分请下载后查看
C# in Depth
contents
foreword
preface
acknowledgments
about this book
Who should read this book
How this book is organized: A roadmap
About the code
Book forum
Other online resources
about the author
about the cover illustration
Part 1 C# in context
1 Survival of the sharpest
1.1 An evolving language
1.1.1 A helpful type system at large and small scales
1.1.2 Ever more concise code
1.1.3 Simple data access with LINQ
1.1.4 Asynchrony
1.1.5 Balancing efficiency and complexity
1.1.6 Evolution at speed: Using minor versions
1.2 An evolving platform
1.3 An evolving community
1.4 An evolving book
1.4.1 Mixed-level coverage
1.4.2 Examples using Noda Time
1.4.3 Terminology choices
Summary
Part 2 C# 2?5
2 C# 2
2.1 Generics
2.1.1 Introduction by example: Collections before generics
2.1.2 Generics save the day
2.1.3 What can be generic?
2.1.4 Type inference for type arguments to methods
2.1.5 Type constraints
2.1.6 The default and typeof operators
2.1.7 Generic type initialization and state
2.2 Nullable value types
2.2.1 Aim: Expressing an absence of information
2.2.2 CLR and framework support: The Nullable struct
2.2.3 Language support
2.3 Simplified delegate creation
2.3.1 Method group conversions
2.3.2 Anonymous methods
2.3.3 Delegate compatibility
2.4 Iterators
2.4.1 Introduction to iterators
2.4.2 Lazy execution
2.4.3 Evaluation of yield statements
2.4.4 The importance of being lazy
2.4.5 Evaluation of finally blocks
2.4.6 The importance of finally handling
2.4.7 Implementation sketch
2.5 Minor features
2.5.1 Partial types
2.5.2 Static classes
2.5.3 Separate getter/setter access for properties
2.5.4 Namespace aliases
2.5.5 Pragma directives
2.5.6 Fixed-size buffers
2.5.7 InternalsVisibleTo
Summary
3 C# 3: LINQ and everything that comes with it
3.1 Automatically implemented properties
3.2 Implicit typing
3.2.1 Typing terminology
3.2.2 Implicitly typed local variables (var)
3.2.3 Implicitly typed arrays
3.3 Object and collection initializers
3.3.1 Introduction to object and collection initializers
3.3.2 Object initializers
3.3.3 Collection initializers
3.3.4 The benefits of single expressions for initialization
3.4 Anonymous types
3.4.1 Syntax and basic behavior
3.4.2 The compiler-generated type
3.4.3 Limitations
3.5 Lambda expressions
3.5.1 Lambda expression syntax
3.5.2 Capturing variables
3.5.3 Expression trees
3.6 Extension methods
3.6.1 Declaring an extension method
3.6.2 Invoking an extension method
3.6.3 Chaining method calls
3.7 Query expressions
3.7.1 Query expressions translate from C# to C#
3.7.2 Range variables and transparent identifiers
3.7.3 Deciding when to use which syntax for LINQ
3.8 The end result: LINQ
Summary
4 C# 4: Improving interoperability
4.1 Dynamic typing
4.1.1 Introduction to dynamic typing
4.1.2 Dynamic behavior beyond reflection
4.1.3 A brief look behind the scenes
4.1.4 Limitations and surprises in dynamic typing
4.1.5 Usage suggestions
4.2 Optional parameters and named arguments
4.2.1 Parameters with default values and arguments with names
4.2.2 Determining the meaning of a method call
4.2.3 Impact on versioning
4.3 COM interoperability improvements
4.3.1 Linking primary interop assemblies
4.3.2 Optional parameters in COM
4.3.3 Named indexers
4.4 Generic variance
4.4.1 Simple examples of variance in action
4.4.2 Syntax for variance in interface and delegate declarations
4.4.3 Restrictions on using variance
4.4.4 Generic variance in practice
Summary
5 Writing asynchronous code
5.1 Introducing asynchronous functions
5.1.1 First encounters of the asynchronous kind
5.1.2 Breaking down the first example
5.2 Thinking about asynchrony
5.2.1 Fundamentals of asynchronous execution
5.2.2 Synchronization contexts
5.2.3 Modeling asynchronous methods
5.3 Async method declarations
5.3.1 Return types from async methods
5.3.2 Parameters in async methods
5.4 Await expressions
5.4.1 The awaitable pattern
5.4.2 Restrictions on await expressions
5.5 Wrapping of return values
5.6 Asynchronous method flow
5.6.1 What is awaited and when?
5.6.2 Evaluation of await expressions
5.6.3 The use of awaitable pattern members
5.6.4 Exception unwrapping
5.6.5 Method completion
5.7 Asynchronous anonymous functions
5.8 Custom task types in C# 7
5.8.1 The 99.9% case: ValueTask
5.8.2 The 0.1% case: Building your own custom task type
5.9 Async main methods in C# 7.1
5.10 Usage tips
5.10.1 Avoid context capture by using ConfigureAwait (where appropriate)
5.10.2 Enable parallelism by starting multiple independent tasks
5.10.3 Avoid mixing synchronous and asynchronous code
5.10.4 Allow cancellation wherever possible
5.10.5 Testing asynchrony
Summary
6 Async implementation
6.1 Structure of the generated code
6.1.1 The stub method: Preparation and taking the first step
6.1.2 Structure of the state machine
6.1.3 The MoveNext() method (high level)
6.1.4 The SetStateMachine method and the state machine boxing dance
6.2 A simple MoveNext() implementation
6.2.1 A full concrete example
6.2.2 MoveNext() method general structure
6.2.3 Zooming into an await expression
6.3 How control flow affects MoveNext()
6.3.1 Control flow between await expressions is simple
6.3.2 Awaiting within a loop
6.3.3 Awaiting within a try/finally block
6.4 Execution contexts and flow
6.5 Custom task types revisited
Summary
7 C# 5 bonus features
7.1 Capturing variables in foreach loops
7.2 Caller information attributes
7.2.1 Basic behavior
7.2.2 Logging
7.2.3 Simplifying INotifyPropertyChanged implementations
7.2.4 Corner cases of caller information attributes
7.2.5 Using caller information attributes with old versions of .NET
Summary
Part 3 C# 6
8 Super-sleek properties and expression-bodied members
8.1 A brief history of properties
8.2 Upgrades to automatically implemented properties
8.2.1 Read-only automatically implemented properties
8.2.2 Initializing automatically implemented properties
8.2.3 Automatically implemented properties in structs
8.3 Expression-bodied members
8.3.1 Even simpler read-only computed properties
8.3.2 Expression-bodied methods, indexers, and operators
8.3.3 Restrictions on expression-bodied members in C# 6
8.3.4 Guidelines for using expression-bodied members
Summary
9 Stringy features
9.1 A recap on string formatting in .NET
9.1.1 Simple string formatting
9.1.2 Custom formatting with format strings
9.1.3 Localization
9.2 Introducing interpolated string literals
9.2.1 Simple interpolation
9.2.2 Format strings in interpolated string literals
9.2.3 Interpolated verbatim string literals
9.2.4 Compiler handling of interpolated string literals (part 1)
9.3 Localization using FormattableString
9.3.1 Compiler handling of interpolated string literals (part 2)
9.3.2 Formatting a FormattableString in a specific culture
9.3.3 Other uses for FormattableString
9.3.4 Using FormattableString with older versions of .NET
9.4 Uses, guidelines, and limitations
9.4.1 Developers and machines, but maybe not end users
9.4.2 Hard limitations of interpolated string literals
9.4.3 When you can but really shouldn?t
9.5 Accessing identifiers with nameof
9.5.1 First examples of nameof
9.5.2 Common uses of nameof
9.5.3 Tricks and traps when using nameof
Summary
10 A sm?rg?sbord of features for concise code
10.1 Using static directives
10.1.1 Importing static members
10.1.2 Extension methods and using static
10.2 Object and collection initializer enhancements
10.2.1 Indexers in object initializers
10.2.2 Using extension methods in collection initializers
10.2.3 Test code vs. production code
10.3 The null conditional operator
10.3.1 Simple and safe property dereferencing
10.3.2 The null conditional operator in more detail
10.3.3 Handling Boolean comparisons
10.3.4 Indexers and the null conditional operator
10.3.5 Working effectively with the null conditional operator
10.3.6 Limitations of the null conditional operator
10.4 Exception filters
10.4.1 Syntax and semantics of exception filters
10.4.2 Retrying operations
10.4.3 Logging as a side effect
10.4.4 Individual, case-specific exception filters
10.4.5 Why not just throw?
Summary
Part 4 C# 7 and beyond
11 Composition using tuples
11.1 Introduction to tuples
11.2 Tuple literals and tuple types
11.2.1 Syntax
11.2.2 Inferred element names for tuple literals (C# 7.1)
11.2.3 Tuples as bags of variables
11.3 Tuple types and conversions
11.3.1 Types of tuple literals
11.3.2 Conversions from tuple literals to tuple types
11.3.3 Conversions between tuple types
11.3.4 Uses of conversions
11.3.5 Element name checking in inheritance
11.3.6 Equality and inequality operators (C# 7.3)
11.4 Tuples in the CLR
11.4.1 Introducing System.ValueTuple<...>
11.4.2 Element name handling
11.4.3 Tuple conversion implementations
11.4.4 String representations of tuples
11.4.5 Regular equality and ordering comparisons
11.4.6 Structural equality and ordering comparisons
11.4.7 Womples and large tuples
11.4.8 The nongeneric ValueTuple struct
11.4.9 Extension methods
11.5 Alternatives to tuples
11.5.1 System.Tuple<...>
11.5.2 Anonymous types
11.5.3 Named types
11.6 Uses and recommendations
11.6.1 Nonpublic APIs and easily changed code
11.6.2 Local variables
11.6.3 Fields
11.6.4 Tuples and dynamic don?t play together nicely
Summary
12 Deconstruction and pattern matching
12.1 Deconstruction of tuples
12.1.1 Deconstruction to new variables
12.1.2 Deconstruction assignments to existing variables and properties
12.1.3 Details of tuple literal deconstruction
12.2 Deconstruction of nontuple types
12.2.1 Instance deconstruction methods
12.2.2 Extension deconstruction methods and overloading
12.2.3 Compiler handling of Deconstruct calls
12.3 Introduction to pattern matching
12.4 Patterns available in C# 7.0
12.4.1 Constant patterns
12.4.2 Type patterns
12.4.3 The var pattern
12.5 Using patterns with the is operator
12.6 Using patterns with switch statements
12.6.1 Guard clauses
12.6.2 Pattern variable scope for case labels
12.6.3 Evaluation order of pattern-based switch statements
12.7 Thoughts on usage
12.7.1 Spotting deconstruction opportunities
12.7.2 Spotting pattern matching opportunities
Summary
13 Improving efficiency with more pass by reference
13.1 Recap: What do you know about ref?
13.2 Ref locals and ref returns
13.2.1 Ref locals
13.2.2 Ref returns
13.2.3 The conditional ?: operator and ref values (C# 7.2)
13.2.4 Ref readonly (C# 7.2)
13.3 in parameters (C# 7.2)
13.3.1 Compatibility considerations
13.3.2 The surprising mutability of in parameters: External changes
13.3.3 Overloading with in parameters
13.3.4 Guidance for in parameters
13.4 Declaring structs as readonly (C# 7.2)
13.4.1 Background: Implicit copying with read-only variables
13.4.2 The readonly modifier for structs
13.4.3 XML serialization is implicitly read-write
13.5 Extension methods with ref or in parameters (C# 7.2)
13.5.1 Using ref/in parameters in extension methods to avoid copying
13.5.2 Restrictions on ref and in extension methods
13.6 Ref-like structs (C# 7.2)
13.6.1 Rules for ref-like structs
13.6.2 Span and stackalloc
13.6.3 IL representation of ref-like structs
Summary
14 Concise code in C# 7
14.1 Local methods
14.1.1 Variable access within local methods
14.1.2 Local method implementations
14.1.3 Usage guidelines
14.2 Out variables
14.2.1 Inline variable declarations for out parameters
14.2.2 Restrictions lifted in C# 7.3 for out variables and pattern variables
14.3 Improvements to numeric literals
14.3.1 Binary integer literals
14.3.2 Underscore separators
14.4 Throw expressions
14.5 Default literals (C# 7.1)
14.6 Nontrailing named arguments (C# 7.2)
14.7 Private protected access (C# 7.2)
14.8 Minor improvements in C# 7.3
14.8.1 Generic type constraints
14.8.2 Overload resolution improvements
14.8.3 Attributes for fields backing automatically implemented properties
Summary
15 C# 8 and beyond
15.1 Nullable reference types
15.1.1 What problem do nullable reference types solve?
15.1.2 Changing the meaning when using reference types
15.1.3 Enter nullable reference types
15.1.4 Nullable reference types at compile time and execution time
15.1.5 The damn it or bang operator
15.1.6 Experiences of nullable reference type migration
15.1.7 Future improvements
15.2 Switch expressions
15.3 Recursive pattern matching
15.3.1 Matching properties in patterns
15.3.2 Deconstruction patterns
15.3.3 Omitting types from patterns
15.4 Indexes and ranges
15.4.1 Index and Range types and literals
15.4.2 Applying indexes and ranges
15.5 More async integration
15.5.1 Asynchronous resource disposal with using await
15.5.2 Asynchronous iteration with foreach await
15.5.3 Asynchronous iterators
15.6 Features not yet in preview
15.6.1 Default interface methods
15.6.2 Record types
15.6.3 Even more features in brief
15.7 Getting involved
Conclusion
appendix Language features by version
index
Symbols
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
C# in Depth-back
IN DEPTH FOURTH EDITION Jon Skeet FOREWORD BY ERIC LIPPERT M A N N I N G
Praise for the Third Edition “A must-have book that every .NET developer should read at least once.” —Dror Helper, Software Architect, Better Place “C# in Depth is the best source for learning C# language features.” —Andy Kirsch, Software Architect, Venga “Took my C# knowledge to the next level.” —Dustin Laine, Owner, Code Harvest “This book was quite an eye-opener to an interesting programming language that I have been unjustly ignoring until now.” —Ivan Todorovic´, Senior Software Developer AudatexGmbH, Switzerland “Easily the best C# reference I’ve found.” —Jon Parish, Software Engineer, Datasift “Highly recommend this book to C# developers who want to take their knowledge to pro status.” —D. Jay, Amazon reviewer Praise for the Second Edition “If you are looking to master C# then this book is a must-read.” —Tyson S. Maxwell, Sr. Software Engineer, Raytheon “We’re betting that this will be the best C# 4.0 book out there.” —Nikander Bruggeman and Margriet Bruggeman .NET consultants, Lois & Clark IT Services “A useful and engaging insight into the evolution of C# 4.” —Joe Albahari, Author of LINQPad and C# 4.0 in a Nutshell “This book should be required reading for all professional C# developers.” —Stuart Caborn, Senior Developer, BNP Paribas
ii “A highly focused, master-level resource on language updates across all major C# releases. This book is a must-have for the expert developer wanting to stay current with new features of the C# language.” —Sean Reilly, Programmer/Analyst Point2 Technologies “Why read the basics over and over again? Jon focuses on the chewy, new stuff!” —Keith Hill, Software Architect, Agilent Technologies “Everything you didn’t realize you needed to know about C#.” —Jared Parsons, Senior Software Development Engineer, Microsoft Praise for the First Edition “Simply put, C# in Depth is perhaps the best computer book I’ve read.” —Craig Pelkie, Author, System iNetwork “I have been developing in C# from the very beginning and this book had some nice surprises even for me. I was especially impressed with the excellent coverage of delegates, anonymous methods, covariance and contravariance. Even if you are a seasoned developer, C# in Depth will teach you something new about the C# language.... This book truly has depth that no other C# language book can touch.” —Adam J. Wolf, Southeast Valley .NET User Group “This book wraps up the author’s great knowledge of the inner workings of C# and hands it over to readers in a well-written, concise, usable book.” —Jim Holmes, Author of Windows Developer Power Tools “Every term is used appropriately and in the right context, every example is spot-on and con- tains the least amount of code that shows the full extent of the feature...this is a rare treat.” —Franck Jeannin, Amazon UK reviewer “If you have developed using C# for several years now, and would like to know the internals, this book is absolutely right for you.” —Golo Roden Author, Speaker, and Trainer for .NET and related technologies “The best C# book I’ve ever read.” —Chris Mullins, C# MVP
C# in Depth FOURTH EDITION JON SKEET FOREWORD BY ERIC LIPPERT M A N N I N G SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2019 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Development editor: Richard Wattenberger Technical development editor: Dennis Sellinger Review editor: Ivan Martinovic´ Production editor: Lori Weidert Copy editor: Sharon Wilkey Technical proofreader: Eric Lippert Typesetter and cover designer: Marija Tudor ISBN 9781617294532 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – SP – 24 23 22 21 20 19
This book is dedicated to equality, which is significantly harder to achieve in the real world than overriding Equals() and GetHashCode().
vi
contents xix foreword xvii preface acknowledgments about this book about the author about the cover illustration xxvii xx xxii xxvi PART 1 C# IN CONTEXT ............................................... 1 1 Survival of the sharpest 1.1 An evolving language 3 3 A helpful type system at large and small scales 4 ■ Ever more concise code 6 ■ Simple data access with LINQ 9 Asynchrony 10 ■ Balancing efficiency and complexity 11 Evolution at speed: Using minor versions 12 1.2 An evolving platform 13 1.3 An evolving community 14 1.4 An evolving book 15 Mixed-level coverage 16 ■ Examples using Noda Time 16 Terminology choices 17 vii
分享到:
收藏