Essential C# 4.0 is a well-organized,''no-fluff'' guide to all versions of C# for programmers at all levels of C# experience. This fully updated edition shows how to make the most of C# 4.0’s new features and programming patterns to write code that is simple, yet powerful.This edition contains two n
Essential C# 4.0 (3rd Edition) (Microsoft .NET Development Series)
✍ Scribed by Mark Michaelis
- Year
- 2010
- Tongue
- English
- Leaves
- 979
- Edition
- 3
- Category
- Library
No coin nor oath required. For personal study only.
✦ Synopsis
Essential C# 4.0 is a well-organized,“no-fluff” guide to all versions of C# for programmers at all levels of C# experience. This fully updated edition shows how to make the most of C# 4.0’s new features and programming patterns to write code that is simple, yet powerful. This edition contains two new chapters on parallel programming, multithreading, and concurrency, as well as extensive coverage of new C# 4.0 features: dynamic typing, variance, optional/named parameters, and many other new topics. Mark Michaelis covers the C# language in depth, illustrating key constructs with succinct, downloadable code examples. Graphical “mind maps” at the beginning of each chapter show what material is covered and how individual topics interrelate. Topics intended for beginners and advanced readers are clearly marked, and the book includes indexes of C# versions (2.0, 3.0, and 4.0), which make it easy for readers to reference topics specific to a given release of C#. Following an introduction to C#, readers learn about Best practices for object-oriented programming in C# C# primitive data types, value and reference types, implicitly typed variables, anonymous types, plus dynamic typing in C# 4.0 Methods and parameters–including extension methods, partial methods, and C# 4.0’s optional and named parameters Generics, concurrent collections, and custom collections with iterators Delegates, events, and lambda expressions Collection interfaces and standard query operators Query expressions and the tree expressions on which LINQ providers are based Reflection, attributes, and dynamic programming Parallel Query Processing with PLINQ Multithreaded programming with the Task Parallel Library Platform interoperability and unsafe code The Common Language Infrastructure that underlies C# Whether you’re just starting out, are an experienced developer moving to C#, or are a seasoned C# programmer seeking to master C# 4.0’s most valuable enhancements, Essential C# 4.0 will help you write high-quality, highly effective code.
✦ Table of Contents
0321694694......Page 1
Contents......Page 8
Contents of C# 4.0 Topics......Page 22
Figures......Page 24
Tables......Page 26
Foreword......Page 28
Preface......Page 32
Acknowledgments......Page 44
About the Author......Page 48
1 Introducing C#......Page 50
Hello, World......Page 51
Compiling and Running the Application......Page 52
C# Syntax Fundamentals......Page 53
Type Definition......Page 56
Main......Page 57
Statements and Statement Delimiters......Page 59
Whitespace......Page 60
Working with Variables......Page 61
Data Types......Page 62
Assigning a Variable......Page 63
Getting Input from the Console......Page 65
Writing Output to the Console......Page 67
Comments......Page 69
Managed Execution and the Common Language Infrastructure......Page 72
C# and .NET Versioning......Page 75
Common Intermediate Language and ILDASM......Page 76
Summary......Page 79
2 Data Types......Page 80
Integer Types......Page 81
Floating-Point Types (float, double)......Page 82
Decimal Type......Page 83
Literal Values......Page 84
Boolean Type (bool)......Page 89
Character Type (char)......Page 90
Strings......Page 92
null......Page 100
The void Nontype......Page 101
Value Types......Page 104
Reference Types......Page 105
Nullable Modifier......Page 106
Explicit Cast......Page 107
Type Conversion without Casting......Page 111
Arrays......Page 113
Declaring an Array......Page 114
Instantiating and Assigning Arrays......Page 115
Using an Array......Page 119
Strings as Arrays......Page 125
Common Errors......Page 127
Summary......Page 130
3 Operators and Control Flow......Page 132
Plus and Minus Unary Operators (+, -)......Page 133
Arithmetic Binary Operators (+, -, , /, %)......Page 134
Parenthesis Operator......Page 141
Assignment Operators (+=, -=, =, /=, %=)......Page 142
Increment and Decrement Operators (++, --)......Page 143
Introducing Flow Control......Page 147
if Statement......Page 151
Nested if......Page 152
Code Blocks ({})......Page 154
Scope and Declaration Space......Page 156
Boolean Expressions......Page 158
Relational and Equality Operators......Page 159
Logical Boolean Operators......Page 160
Conditional Operator (?)......Page 162
Null Coalescing Operator (??)......Page 163
Bitwise Operators (<<, >>, |, &, ^, ~)......Page 164
Shift Operators (<<, >>, <<=, >>=)......Page 165
Bitwise Operators (&, |, ^)......Page 166
Bitwise Complement Operator (~)......Page 169
The while and do/while Loops......Page 170
The for Loop......Page 173
The foreach Loop......Page 176
The switch Statement......Page 179
The break Statement......Page 181
The continue Statement......Page 184
The goto Statement......Page 186
C# Preprocessor Directives......Page 187
Excluding and Including Code (#if, #elif, #else, #endif)......Page 189
Emitting Errors and Warnings (#error, #warning)......Page 190
Turning Off Warning Messages (#pragma)......Page 191
Specifying Line Numbers (#line)......Page 192
Hints for Visual Editors (#region, #endregion)......Page 193
Summary......Page 194
4 Methods and Parameters......Page 198
Calling a Method......Page 199
Namespace......Page 201
Type Name......Page 203
Method Return......Page 204
Statement versus Method Call......Page 205
Declaring a Method......Page 206
Method Return Declaration......Page 208
The using Directive......Page 210
Aliasing......Page 213
Returns and Parameters on Main()......Page 214
Value Parameters......Page 217
Reference Parameters (ref)......Page 219
Output Parameters (out)......Page 220
Parameter Arrays (params)......Page 222
Recursion......Page 225
Method Overloading......Page 228
Optional Parameters......Page 231
Basic Error Handling with Exceptions......Page 235
Trapping Errors......Page 236
Reporting Errors Using a throw Statement......Page 245
Summary......Page 248
5 Classes......Page 250
Declaring and Instantiating a Class......Page 254
Declaring an Instance Field......Page 258
Accessing an Instance Field......Page 259
Instance Methods......Page 260
Using the this Keyword......Page 262
Access Modifiers......Page 269
Properties......Page 271
Declaring a Property......Page 272
Automatically Implemented Properties......Page 274
Naming Conventions......Page 276
Using Properties with Validation......Page 277
Read-Only and Write-Only Properties......Page 279
Access Modifiers on Getters and Setters......Page 280
Properties as Virtual Fields......Page 281
Properties and Method Calls Not Allowed as ref or out Parameter Values......Page 283
Constructors......Page 285
Declaring a Constructor......Page 286
Object Initializers......Page 288
Overloading Constructors......Page 290
Constructor Chaining: Calling another Constructor Using this......Page 292
Static Members......Page 296
Static Fields......Page 297
Static Methods......Page 300
Static Constructors......Page 302
Static Properties......Page 303
Static Classes......Page 304
Extension Methods......Page 305
const......Page 307
readonly......Page 308
Nested Classes......Page 309
Partial Classes......Page 311
Defining a Partial Class......Page 312
Partial Methods......Page 313
Summary......Page 316
6 Inheritance......Page 318
Derivation......Page 319
Casting between Base and Derived Types......Page 321
private Access Modifier......Page 324
protected Access Modifier......Page 325
Single Inheritance......Page 327
Overriding the Base Class......Page 330
virtual Modifier......Page 331
new Modifier......Page 335
base Member......Page 340
Constructors......Page 341
Abstract Classes......Page 342
All Classes Derive from System.Object......Page 348
Verifying the Underlying Type with the is Operator......Page 350
Conversion Using the as Operator......Page 351
Summary......Page 352
7 Interfaces......Page 354
Introducing Interfaces......Page 355
Polymorphism through Interfaces......Page 356
Interface Implementation......Page 361
Explicit Member Implementation......Page 363
Implicit Member Implementation......Page 364
Explicit versus Implicit Interface Implementation......Page 365
Interface Inheritance......Page 367
Multiple Interface Inheritance......Page 370
Extension Methods on Interfaces......Page 371
Implementing Multiple Inheritance via Interfaces......Page 372
Versioning......Page 375
Interfaces Compared with Classes......Page 377
Summary......Page 378
8 Value Types......Page 380
Structs......Page 381
Initializing structs......Page 385
Inheritance and Interfaces with Value Types......Page 387
Boxing......Page 388
Enums......Page 395
Type Compatibility between Enums......Page 398
Converting between Enums and Strings......Page 399
Enums as Flags......Page 400
Summary......Page 405
Overriding object Members......Page 406
Overriding GetHashCode()......Page 407
Overriding Equals()......Page 410
Operator Overloading......Page 418
Comparison Operators (==, !=, <, >, <=, >=)......Page 419
Binary Operators (+, -, *, /, %, &, |, ^, <<, >>)......Page 420
Unary Operators (+, -, !, ~, ++, --, true, false)......Page 422
Conversion Operators......Page 424
Referencing Other Assemblies......Page 426
Changing the Assembly Target......Page 427
Encapsulation of Types......Page 428
Defining Namespaces......Page 431
Namespace Alias Qualifier......Page 433
XML Comments......Page 434
Associating XML Comments with Programming Constructs......Page 435
Generating an XML Documentation File......Page 437
Garbage Collection......Page 439
Weak References......Page 440
Finalizers......Page 442
Deterministic Finalization with the using Statement......Page 444
Garbage Collection and Finalization......Page 447
Lazy Initialization......Page 449
Summary......Page 452
Multiple Exception Types......Page 454
Catching Exceptions......Page 456
General Catch Block......Page 458
Guidelines for Exception Handling......Page 460
Defining Custom Exceptions......Page 463
Summary......Page 468
11 Generics......Page 470
C# without Generics......Page 471
Using a Generic Class......Page 476
Defining a Simple Generic Class......Page 478
Benefits of Generics......Page 479
Type Parameter Naming Guidelines......Page 480
Generic Interfaces and Structs......Page 481
Defining a Constructor and a Finalizer......Page 483
Specifying a Default Value......Page 484
Multiple Type Parameters......Page 485
Arity in Abundance......Page 486
Nested Generic Types......Page 487
Constraints......Page 488
Interface Constraints......Page 491
Base Class Constraints......Page 493
struct/class Constraints......Page 494
Constructor Constraints......Page 495
Constraint Inheritance......Page 496
Generic Methods......Page 502
Type Inferencing......Page 503
Specifying Constraints......Page 504
Covariance and Contravariance......Page 506
Enabling Covariance with the out Type Parameter Modifier in C# 4.0......Page 507
Enabling Contravariance with the in Type Parameter Modifier in C# 4.0......Page 509
Support for Parameter Covariance and Contravariance in Arrays......Page 511
Generic Internals......Page 512
Instantiating Generics Based on Value Types......Page 513
Instantiating Generics Based on Reference Types......Page 514
Summary......Page 516
12 Delegates and Lambda Expressions......Page 518
Defining the Scenario......Page 519
Delegate Data Types......Page 521
Delegate Internals......Page 522
Defining a Delegate Type......Page 523
Instantiating a Delegate......Page 524
Anonymous Methods......Page 529
System-Defined Delegates: Func<>......Page 532
Statement Lambdas......Page 535
Expression Lambdas......Page 538
Outer Variables......Page 544
Expression Trees......Page 547
Summary......Page 555
13 Events......Page 556
Defining Subscriber Methods......Page 557
Defining the Publisher......Page 559
Hooking Up the Publisher and Subscribers......Page 560
Invoking a Delegate......Page 561
Check for null......Page 562
Delegate Operators......Page 563
Sequential Invocation......Page 565
Error Handling......Page 568
Method Returns and Pass-by-Reference......Page 571
Why Events?......Page 572
Declaring an Event......Page 574
Coding Conventions......Page 575
Generics and Delegates......Page 577
Customizing the Event Implementation......Page 581
Summary......Page 582
14 Collection Interfaces with Standard Query Operators......Page 584
Anonymous Types and Implicitly Typed Local Variables......Page 585
Anonymous Types......Page 586
Implicitly Typed Local Variables (var)......Page 587
More about Anonymous Types and Implicit Local Variables......Page 589
Collection Initializers......Page 592
foreach with Arrays......Page 595
foreach with I Enumerable
Standard Query Operators......Page 601
Filtering with Where()......Page 605
Projecting with Select()......Page 606
Counting Elements with Count()......Page 610
Deferred Execution......Page 611
Sorting with OrderBy() and ThenBy()......Page 615
Performing an Inner Join with Join()......Page 621
Grouping Results with GroupBy()......Page 624
Implementing a One-to-Many Relationship with GroupJoin()......Page 626
Calling SelectMany()......Page 629
More Standard Query Operators......Page 631
Summary......Page 635
15 LINQ with Query Expressions......Page 638
Introducing Query Expressions......Page 639
Projection......Page 641
Filtering......Page 647
Sorting......Page 648
The Let Clause......Page 649
Grouping......Page 651
Query Continuation with into......Page 654
Query Expressions as Method Invocations......Page 657
Summary......Page 658
16 Building Custom Collections......Page 660
More Collection Interfaces......Page 661
IComparable
ICollection
List Collections: List
Dictionary Collections: Dictionary
Sorted Collections: SortedDictionary
Stack Collections: Stack
Linked Lists: LinkedList
Providing an Index Operator......Page 679
Iterators......Page 683
Iterator Syntax......Page 685
Yielding Values from an Iterator......Page 686
Iterators and State......Page 688
More Iterator Examples......Page 690
Placing a yield return within a Loop......Page 692
Canceling Further Iteration: yield break......Page 694
Creating Multiple Iterators in a Single Class......Page 697
yield Statement Characteristics......Page 698
Summary......Page 699
17 Reflection, Attributes, and Dynamic Programming......Page 700
Reflection......Page 701
Accessing Metadata Using System.Type......Page 702
Member Invocation......Page 704
Reflection on Generic Types......Page 709
Attributes......Page 712
Custom Attributes......Page 715
Looking for Attributes......Page 716
Initializing an Attribute through a Constructor......Page 717
System.AttributeUsageAttribute......Page 722
Named Parameters......Page 723
Programming with Dynamic Objects......Page 737
Invoking Reflection Using dynamic......Page 738
dynamic Principles and Behaviors......Page 739
Why Dynamic Binding?......Page 743
Static Compilation versus Dynamic Programming......Page 744
Implementing a Custom Dynamic Object......Page 745
Summary......Page 748
18 Multithreading......Page 750
Running and Controlling a Separate Thread......Page 755
ContinueWith()......Page 760
Unhandled Exception Handling on Task......Page 764
Canceling a Task......Page 767
Long-Running Tasks......Page 771
Disposing a Task......Page 772
Executing Iterations in Parallel......Page 773
Parallel Exception Handling with System.AggregateException......Page 777
Canceling a Parallel Loop......Page 778
Running LINQ Queries in Parallel......Page 783
Canceling a PLINQ Query......Page 785
Asynchronous Operations with System.Threading.Thread......Page 787
Thread Management......Page 789
Thread Pooling......Page 791
Unhandled Exceptions on the AppDomain......Page 793
Summary......Page 795
19 Synchronization and More Multithreading Patterns......Page 798
Synchronization......Page 799
Synchronization Using Monitor......Page 803
Using the Lock Keyword......Page 806
Choosing a Lock Object......Page 807
Why to Avoid Locking on this, typeof(type), and string......Page 808
Declaring Fields as volatile......Page 809
Using the System.Threading.Interlocked Class......Page 810
Event Notification with Multiple Threads......Page 812
Synchronization Design Best Practices......Page 813
More Synchronization Types......Page 815
Thread Local Storage......Page 823
Timers......Page 827
Asynchronous Programming Model......Page 832
Calling the APM......Page 833
Calling the APM Using TPL......Page 840
Asynchronous Delegate Invocation......Page 846
Passing Data to and from an Alternate Thread......Page 848
Event-Based Asynchronous Pattern (EAP)......Page 850
Background Worker Pattern......Page 853
Establishing the Pattern......Page 856
Exception Handling......Page 857
Windows Forms......Page 858
Windows Presentation Foundation (WPF)......Page 860
Summary......Page 863
20 Platform Interoperability and Unsafe Code......Page 864
Platform Invoke......Page 865
Declaring External Functions......Page 866
Parameter Data Types......Page 867
Using ref Rather Than Pointers......Page 868
Using StructLayoutAttribute for Sequential Layout......Page 869
Error Handling......Page 870
Using SafeHandle......Page 872
Calling External Functions......Page 875
Simplifying API Calls with Wrappers......Page 877
Guidelines......Page 878
Unsafe Code......Page 879
Pointer Declaration......Page 881
Assigning a Pointer......Page 883
Dereferencing a Pointer......Page 886
Summary......Page 888
21 The Common Language Infrastructure......Page 892
Defining the Common Language Infrastructure (CLI)......Page 893
CLI Implementations......Page 894
C# Compilation to Machine Code......Page 896
Garbage Collection......Page 898
Garbage Collection on .NET......Page 899
Type Safety......Page 900
Platform Portability......Page 901
Performance......Page 902
Application Domains......Page 903
Assemblies, Manifests, and Modules......Page 904
Common Type System (CTS)......Page 907
Common Language Specification (CLS)......Page 908
Metadata......Page 909
Summary......Page 911
A: Downloading and Installing the C# Compiler and the CLI Platform......Page 914
B: Full Source Code Listings......Page 918
C: Concurrent Classes from System.Collections.Concurrent......Page 944
D: C# 2.0 Topics......Page 948
E: C# 3.0 Topics......Page 952
F: C# 4.0 Topics......Page 954
A......Page 956
C......Page 958
D......Page 962
E......Page 963
F......Page 964
H......Page 965
I......Page 966
J......Page 967
L......Page 968
M......Page 969
N......Page 970
O......Page 971
P......Page 972
R......Page 973
S......Page 974
T......Page 976
W......Page 978
Y......Page 979
📜 SIMILAR VOLUMES
Essential C# 4.0 is a well-organized, "no-fluff" guide to all versions of C# for programmers at all levels of C# experience. This fully updated edition shows how to make the most of C# 4.0's new features and programming patterns to write code that is simple, yet powerful. This edition con
“Based on my own experience, I can safely say that every .NET developer who reads this will have at least one ‘aha’ moment and will be a better developer for it.” –From the Foreword by Don Box The popular C# programming language combines the high productivity of rapid application development
Part of the Cavendish "Essential" which is designed to provide useful revision aids for the hard-pressed student. Not intended as substitutes for more detailed treatises, they offer succinct coverage of the topic at hand.
Design sleek, high-performance applications for the newest smart mobile devices with the industry’s most respected reference to Windows CE .NET. Now in its third edition, this essential guide has been updated for Windows CE .NET 4.2 and the .NET Compact Framework. Author Douglas Boling expertly demo
Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build web applications that work well on both the desktop and mobile devices. Web development expert Dino Esposito takes you through the web framework's Model-View-Controller (MVC) design model, and covers the tools you ne