E-Book, Englisch, 645 Seiten
Nash Accelerated C# 2010
1. ed
ISBN: 978-1-4302-2538-6
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
E-Book, Englisch, 645 Seiten
ISBN: 978-1-4302-2538-6
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
C# 2010 offers powerful new features, and this book is the fastest path to mastering them-and the rest of C#-for both experienced C# programmers moving to C# 2010 and programmers moving to C# from another object-oriented language. Many books introduce C#, but very few also explain how to use it optimally with the .NET Common Language Runtime (CLR). This book teaches both core C# language concepts and how to wisely employ C# idioms and object-oriented design patterns to exploit the power of C# and the CLR. This book is both a rapid tutorial and a permanent reference. You'll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You'll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way from the start? Comprehensively and concisely explains both C# 2008 and C# 2010 features Focuses on the language itself and on how to use C# 2010 proficiently for all .NET application development Concentrates on how C# features work and how to best use them for robust, high-performance code
Trey Nash is an escalation engineer at Microsoft working on the Windows operating systems as well as various other products. When he is not working feverishly within the bowels of the operating system, he is delivering training on .NET Platform debugging as well as user mode and kernel mode debugging on the Windows platform. Prior to working at Microsoft, he was a principal software engineer working on security solutions at Credant Technologies, a market-leading security software company. He also enjoined a stint at a large Bluetooth company developing Bluetooth solutions for the release of Microsoft Vista. Before that, he called Macromedia, Inc. home for five years. At Macromedia, he worked on a cross-product engineering team for several years, designing solutions for a wide range of products throughout the company, including Flash, Fireworks, and Dreamweaver. He specialized in COM/DCOM using C/C++/ATL until the .NET revolution. He's been glued to computers ever since he scored his first, a TI-99/4A, when he was a mere 13 years old. He astounded his parents by turning a childhood obsession into a decent-paying career, much to their dismay. Trey received his bachelor of science and his master of engineering degrees in electrical engineering from Texas A&M University. When he's not sitting in front of a computer, you can find him working in his garage, playing his piano, brushing up on a foreign language (Russian and Icelandic are the current favorites), or playing ice hockey.
Autoren/Hrsg.
Weitere Infos & Material
1;Title Page;2
2;Copyright Page;3
3;Dedication Page;4
4;Contents at a Glance;5
5;Table of Contents;7
6;About the Author;21
7;About the Technical Reviewer;22
8;Acknowledgments;23
9;Preface;24
9.1;About This Book;25
10;Chapter 1: C# Preview;28
10.1;Differences Between C# and C++;28
10.1.1;C#;28
10.1.2;C++;29
10.1.3;CLR Garbage Collection;30
10.2;Example of a C# Program;30
10.3;Overview of Features Added in C# 2.0;32
10.4;Overview of Features Added in C# 3.0;33
10.5;Overview of New C# 4.0 Features;34
10.6;Summary;34
11;Chapter 2: C# and the CLR;36
11.1;The JIT Compiler in the CLR;37
11.2;Assemblies and the Assembly Loader;38
11.2.1;Minimizing the Working Set of the Application;39
11.2.2;Naming Assemblies;39
11.2.3;Loading Assemblies;40
11.3;Metadata;40
11.4;Cross-Language Compatibility;42
11.5;Summary;42
12;Chapter 3: C# Syntax Overview;43
12.1;C# Is a Strongly Typed Language;43
12.2;Expressions;44
12.3;Statements and Expressions;46
12.4;Types and Variables;47
12.4.1;Value Types;49
12.4.1.1;Enumerations;50
12.4.1.2;Flags Enumerations;51
12.4.2;Reference Types;52
12.4.3;Default Variable Initialization;53
12.4.4;Implicitly Typed Local Variables;54
12.4.4.1;THE COMPLEXITY OF ADDING NEW KEYWORDS TO THE LANGUAGE;55
12.4.5;Type Conversion;56
12.4.5.1;Array Covariance;57
12.4.5.2;Boxing Conversion;57
12.4.6;as and is Operators;58
12.4.7;Generics;60
12.5;Namespaces;61
12.5.1;Defining Namespaces;62
12.5.2;Using Namespaces;63
12.6;Control Flow;65
12.6.1;if-else, while, do-while, and for;65
12.6.2;switch;65
12.6.3;foreach;66
12.6.4;break, continue, goto, return, and throw;67
12.7;Summary;67
13;Chapter 4: Classes, Structs, and Objects;68
13.1;Class Definitions;70
13.1.1;Fields;71
13.1.2;Constructors;74
13.1.3;Methods;74
13.1.3.1;Static Methods;75
13.1.3.2;Instance Methods;75
13.1.4;Properties;76
13.1.4.1;Declaring Properties;76
13.1.4.2;Accessors;78
13.1.4.3;Read-Only and Write-Only Properties;78
13.1.4.4;Auto-Implemented Properties;79
13.1.5;Encapsulation;81
13.1.6;Accessibility;84
13.1.7;Interfaces;86
13.1.8;Inheritance;87
13.1.8.1;Accessibility of Members;88
13.1.8.2;Implicit Conversion and a Taste of Polymorphism;88
13.1.8.3;Member Hiding;90
13.1.8.4;The base Keyword;93
13.1.9;sealed Classes;94
13.1.10;abstract Classes;95
13.1.11;Nested Classes;96
13.1.12;Indexers;99
13.1.13;partial Classes;101
13.1.14;partial Methods;102
13.1.15;Static Classes;104
13.1.15.1;THE SINGLETON PATTERN;105
13.1.16;Reserved Member Names;106
13.1.16.1;Reserved Names for Properties;106
13.1.16.2;Reserved Names for Indexers;106
13.1.16.3;Reserved Names for Destructors;106
13.1.16.4;Reserved Names for Events;107
13.2;Value Type Definitions;107
13.2.1;Constructors;107
13.2.2;The Meaning of this;109
13.2.3;Finalizers;112
13.2.4;Interfaces;112
13.3;Anonymous Types;113
13.4;Object Initializers;116
13.5;Boxing and Unboxing;119
13.5.1;When Boxing Occurs;123
13.5.2;Efficiency and Confusion;125
13.6;System.Object;126
13.6.1;Equality and What It Means;127
13.6.2;The IComparable Interface;128
13.7;Creating Objects;128
13.7.1;The new Keyword;128
13.7.1.1;Using new with Value Types;128
13.7.1.2;Using new with Class Types;128
13.7.2;Field Initialization;129
13.7.3;Static (Class) Constructors;131
13.7.4;Instance Constructor and Creation Ordering;134
13.8;Destroying Objects;138
13.8.1;Finalizers;138
13.8.2;Deterministic Destruction;139
13.8.3;Exception Handling;140
13.9;Disposable Objects;141
13.9.1;The IDisposable Interface;141
13.9.2;The using Keyword;143
13.10;Method Parameter Types;144
13.10.1;Value Arguments;145
13.10.2;ref Arguments;145
13.10.3;out Parameters;147
13.10.4;param Arrays;148
13.10.5;Method Overloading;148
13.10.6;Optional Arguments;149
13.10.7;Named Arguments;150
13.11;Inheritance and Virtual Methods;153
13.11.1;Virtual and Abstract Methods;154
13.11.2;override and new Methods;154
13.11.3;sealed Methods;156
13.11.4;A Final Few Words on C# Virtual Methods;157
13.12;Inheritance, Containment, and Delegation;157
13.12.1;Choosing Between Interface and Class Inheritance;157
13.12.2;Delegation and Composition vs. Inheritance;159
13.13;Summary;161
14;Chapter 5: Interfaces and Contracts;162
14.1;Interfaces Define Types;163
14.2;Defining Interfaces;164
14.2.1;INTERFACES DEFINE CONTRACTS;164
14.2.2;What Can Be in an Interface?;164
14.2.3;Interface Inheritance and Member Hiding;165
14.3;Implementing Interfaces;168
14.3.1;Implicit Interface Implementation;168
14.3.2;Explicit Interface Implementation;168
14.3.3;Overriding Interface Implementations in Derived Classes;170
14.3.4;Beware of Side Effects of Value Types Implementing Interfaces;175
14.3.4.1;PREFER THE CONVERT CLASS OVER ICONVERTIBLE;175
14.4;Interface Member Matching Rules;175
14.5;Explicit Interface Implementation with Value Types;179
14.6;Versioning Considerations;181
14.7;Contracts;182
14.7.1;Contracts Implemented with Classes;182
14.7.2;Interface Contracts;184
14.8;Choosing Between Interfaces and Classes;185
14.9;Summary;189
15;Chapter 6: Overloading Operators;190
15.1;Just Because You Can Doesn’t Mean You Should;190
15.2;Types and Formats of Overloaded Operators;190
15.3;Operators Shouldn’t Mutate Their Operands;192
15.4;Does Parameter Order Matter?;192
15.5;Overloading the Addition Operator;193
15.6;Operators That Can Be Overloaded;194
15.6.1;Comparison Operators;195
15.6.2;Conversion Operators;198
15.6.2.1;THE FLOATING POINT ENIGMA;201
15.6.3;Boolean Operators;201
15.7;Summary;204
16;Chapter 7: Exception Handling and Exception Safety;205
16.1;How the CLR Treats Exceptions;205
16.2;Mechanics of Handling Exceptions in C#;206
16.2.1;Throwing Exceptions;206
16.2.2;Changes with Unhandled Exceptions Starting with .NET 2.0;206
16.2.3;Syntax Overview of the try, catch, and finally Statements;207
16.2.4;Rethrowing Exceptions and Translating Exceptions;210
16.2.5;Exceptions Thrown in finally Blocks;213
16.2.6;Exceptions Thrown in Finalizers;213
16.2.7;Exceptions Thrown in Static Constructors;215
16.3;Who Should Handle Exceptions?;216
16.4;Avoid Using Exceptions to Control Flow;217
16.5;Achieving Exception Neutrality;217
16.5.1;Basic Structure of Exception-Neutral Code;218
16.5.2;Constrained Execution Regions;223
16.5.3;Critical Finalizers and SafeHandle;225
16.6;Creating Custom Exception Classes;230
16.7;Working with Allocated Resources and Exceptions;231
16.8;Providing Rollback Behavior;235
16.9;Summary;238
17;Chapter 8: Working with Strings;239
17.1;String Overview;239
17.2;String Literals;240
17.3;Format Specifiers and Globalization;241
17.3.1;Object.ToString, IFormattable, and CultureInfo;242
17.3.2;Creating and Registering Custom CultureInfo Types;243
17.3.3;Format Strings;245
17.3.4;Console.WriteLine and String.Format;246
17.3.5;Examples of String Formatting in Custom Types;247
17.3.6;ICustomFormatter;248
17.3.7;Comparing Strings;251
17.4;Working with Strings from Outside Sources;252
17.5;StringBuilder;254
17.6;Searching Strings with Regular Expressions;256
17.6.1;Searching with Regular Expressions;256
17.6.2;Searching and Grouping;258
17.6.3;Replacing Text with Regex;262
17.6.4;Regex Creation Options;264
17.7;Summary;266
18;Chapter 9: Arrays, Collection Types, and Iterators;267
18.1;Introduction to Arrays;267
18.1.1;Implicitly Typed Arrays;268
18.1.2;Type Convertibility and Covariance;271
18.1.3;Sortability and Searchability;272
18.1.4;Synchronization;273
18.1.5;Vectors vs. Arrays;273
18.2;Multidimensional Rectangular Arrays;275
18.3;Multidimensional Jagged Arrays;277
18.4;Collection Types;279
18.4.1;Comparing ICollection with ICollection;279
18.4.2;Collection Synchronization;281
18.4.3;Lists;282
18.4.4;Dictionaries;283
18.4.5;Sets;283
18.4.6;System.Collections.ObjectModel;284
18.4.7;Efficiency;286
18.5;IEnumerable, IEnumerator, IEnumerable, and IEnumerator;288
18.5.1;Types That Produce Collections;291
18.6;Iterators;292
18.6.1;Forward, Reverse, and Bidirectional Iterators;297
18.7;Collection Initializers;301
18.8;Summary;302
19;Chapter 10: Delegates, Anonymous Functions, and Events;303
19.1;Overview of Delegates;303
19.2;Delegate Creation and Use;304
19.2.1;Single Delegate;305
19.2.2;Delegate Chaining;306
19.2.3;Iterating Through Delegate Chains;308
19.2.4;Unbound (Open Instance) Delegates;309
19.3;Events;312
19.4;Anonymous Methods;316
19.4.1;Captured Variables and Closures;319
19.4.2;Beware the Captured Variable Surprise;321
19.4.3;Anonymous Methods as Delegate Parameter Binders;324
19.5;The Strategy Pattern;328
19.6;Summary;329
20;Chapter 11: Generics;330
20.1;Difference Between Generics and C++ Templates;331
20.2;Efficiency and Type Safety of Generics;332
20.2.1;GENERIC TYPE PLACEHOLDER NAMING CONVENTIONS;334
20.3;Generic Type Definitions and Constructed Types;334
20.3.1;Generic Classes and Structs;334
20.3.2;Generic Interfaces;337
20.3.3;Generic Methods;338
20.3.4;Generic Delegates;340
20.3.5;Generic Type Conversion;343
20.3.6;Default Value Expression;344
20.3.7;Nullable Types;346
20.3.8;Constructed Types Control Accessibility;348
20.3.9;Generics and Inheritance;348
20.4;Constraints;350
20.4.1;Constraints on Nonclass Types;355
20.5;Co- and Contravariance;355
20.5.1;Covariance;357
20.5.2;Contravariance;360
20.5.3;Invariance;362
20.5.4;Variance and Delegates;363
20.6;Generic System Collections;367
20.7;Generic System Interfaces;368
20.8;Select Problems and Solutions;370
20.8.1;Conversion and Operators within Generic Types;370
20.8.2;Creating Constructed Types Dynamically;380
20.9;Summary;381
21;Chapter 12: Threading in C#;383
21.1;Threading in C# and .NET;383
21.1.1;Starting Threads;384
21.1.1.1;Passing Data to New Threads;385
21.1.1.2;Using ParameterizedThreadStart;387
21.1.2;The IOU Pattern and Asynchronous Method Calls;388
21.1.3;States of a Thread;388
21.1.4;Terminating Threads;391
21.1.5;Halting Threads and Waking Sleeping Threads;393
21.1.6;Waiting for a Thread to Exit;394
21.1.7;Foreground and Background Threads;394
21.1.8;Thread-Local Storage;395
21.1.9;How Unmanaged Threads and COM Apartments Fit In;399
21.2;Synchronizing Work Between Threads;400
21.2.1;Lightweight Synchronization with the Interlocked Class;401
21.2.1.1;INTERLOCKED METHODS ON SMP SYSTEMS;403
21.2.2;SpinLock Class;407
21.2.3;Monitor Class;409
21.2.3.1;Beware of Boxing;413
21.2.3.2;Pulse and Wait;414
21.2.4;Locking Objects;418
21.2.4.1;ReaderWriterLock;419
21.2.4.2;ReaderWriterLockSlim;422
21.2.4.3;Mutex;423
21.2.5;Semaphore;424
21.2.6;Events;426
21.2.7;Win32 Synchronization Objects and WaitHandle;427
21.3;Using ThreadPool;429
21.3.1;Asynchronous Method Calls;430
21.3.2;Timers;438
21.4;Concurrent Programming;439
21.4.1;Task Class;440
21.4.2;Parallel Class;442
21.4.3;Easy Entry to the Thread Pool;447
21.5;Thread-Safe Collection Classes;448
21.6;Summary;448
22;Chapter 13: In Search of C# Canonical Forms;450
22.1;Reference Type Canonical Forms;450
22.1.1;Default to sealed Classes;451
22.1.2;Use the Non-Virtual Interface (NVI) Pattern;452
22.1.3;Is the Object Cloneable?;455
22.1.4;Is the Object Disposable?;461
22.1.5;Does the Object Need a Finalizer?;464
22.1.6;What Does Equality Mean for This Object?;471
22.1.6.1;Reference Types and Identity Equality;472
22.1.6.2;Value Equality;475
22.1.6.3;Overriding Object.Equals for Reference Types;475
22.1.7;If You Override Equals, Override GetHashCode Too;478
22.1.8;Does the Object Support Ordering?;482
22.1.9;Is the Object Formattable?;484
22.1.10;Is the Object Convertible?;488
22.1.11;Prefer Type Safety at All Times;490
22.1.12;Using Immutable Reference Types;494
22.2;Value Type Canonical Forms;497
22.2.1;Override Equals for Better Performance;498
22.2.2;Do Values of This Type Support Any Interfaces?;502
22.2.3;Implement Type-Safe Forms of Interface Members and Derived Methods;503
22.3;Summary;505
22.3.1;Checklist for Reference Types;506
22.3.2;Checklist for Value Types;507
23;Chaper 14: Extension Methods;509
23.1;Introduction to Extension Methods;509
23.1.1;How Does the Compiler Find Extension Methods?;510
23.1.2;Under the Covers;513
23.1.3;Code Readability versus Code Understandability;514
23.2;Recommendations for Use;515
23.2.1;Consider Extension Methods Over Inheritance;515
23.2.2;Isolate Extension Methods in Separate Namespace;516
23.2.3;Changing a Type’s Contract Can Break Extension Methods;517
23.3;Transforms;517
23.4;Operation Chaining;522
23.5;Custom Iterators;523
23.5.1;Borrowing from Functional Programming;525
23.6;The Visitor Pattern;531
23.7;Summary;535
24;Chaper 15: Lambda Expressions;536
24.1;Introduction to Lambda Expressions;536
24.1.1;Lambda Expressions and Closures;537
24.1.1.1;Closures in C# 1.0;540
24.1.1.2;Closures in C# 2.0;542
24.1.2;Lambda Statements;543
24.2;Expression Trees;543
24.2.1;Operating on Expressions;546
24.2.2;Functions as Data;547
24.3;Useful Applications of Lambda Expressions;548
24.3.1;Iterators and Generators Revisited;548
24.3.2;More on Closures (Variable Capture) and Memoization;552
24.3.3;Currying;557
24.3.4;Anonymous Recursion;559
24.4;Summary;560
25;Chaper 16: LINQ: Language Integrated Query;562
25.1;A Bridge to Data;563
25.1.1;Query Expressions;563
25.1.2;Extension Methods and Lambda Expressions Revisited;565
25.2;Standard Query Operators;566
25.3;C# Query Keywords;568
25.3.1;The from Clause and Range Variables;568
25.3.2;The join Clause;569
25.3.3;The where Clause and Filters;571
25.3.4;The orderby Clause;572
25.3.5;The select Clause and Projection;573
25.3.6;The let Clause;575
25.3.7;The group Clause;576
25.3.8;The into Clause and Continuations;579
25.4;The Virtues of Being Lazy;581
25.4.1;C# Iterators Foster Laziness;581
25.4.2;Subverting Laziness;582
25.4.3;Executing Queries Immediately;584
25.4.4;Expression Trees Revisited;585
25.5;Techniques from Functional Programming;585
25.5.1;Custom Standard Query Operators and Lazy Evaluation;585
25.5.2;Replacing foreach Statements;594
25.6;Summary;595
26;Chaper 17: Dynamic Types;596
26.1;What does dynamic Mean?;596
26.2;How Does dynamic Work?;599
26.2.1;The Great Unification;601
26.2.2;Call Sites;601
26.2.3;Objects with Custom Dynamic Behavior;604
26.2.4;Efficiency;606
26.2.5;Boxing with Dynamic;608
26.3;Dynamic Conversions;609
26.3.1;Implicit Dynamic Expressions Conversion;610
26.4;Dynamic Overload Resolution;611
26.5;Dynamic Inheritance;613
26.5.1;You Cannot Derive from dynamic;614
26.5.2;You Cannot Implement dynamic Interfaces;614
26.5.3;You Can Derive From Dynamic Base Types;616
26.6;Duck Typing in C#;618
26.7;Limitations of dynamic Types;621
26.8;ExpandoObject: Creating Objects Dynamically;621
26.9;Summary;626
27;Index;627




