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
Essential C# 4.0, 3rd Edition
β Scribed by Mark Michaelis
- Publisher
- Addison-Wesley
- Year
- 2010
- Tongue
- English
- Leaves
- 978
- 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, multiΒthreading, 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 aboutBest 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.0Methods and parametersβincluding extension methods, partial methΒods, and C# 4.0βs optional and named parametersGenerics, concurrent collections, and custom collections with iteratorsDelegates, events, and lambda expressionsCollection interfaces and standard query operatorsQuery expressions and the tree expressions on which LINQ providers are basedReflection, attributes, and dynamic programmingParallel Query Processing with PLINQMultithreaded programming with the Task Parallel LibraryPlatform interoperability and unsafe codeThe 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
Contents......Page 7
Contents of C# 4.0 Topics......Page 21
Figures......Page 23
Tables......Page 25
Foreword......Page 27
Preface......Page 31
Acknowledgments......Page 43
About the Author......Page 47
1 Introducing C#......Page 49
Hello, World......Page 50
Compiling and Running the Application......Page 51
C# Syntax Fundamentals......Page 52
Type Definition......Page 55
Main......Page 56
Statements and Statement Delimiters......Page 58
Whitespace......Page 59
Working with Variables......Page 60
Data Types......Page 61
Assigning a Variable......Page 62
Getting Input from the Console......Page 64
Writing Output to the Console......Page 66
Comments......Page 68
Managed Execution and the Common Language Infrastructure......Page 71
C# and .NET Versioning......Page 74
Common Intermediate Language and ILDASM......Page 75
Summary......Page 78
2 Data Types......Page 79
Integer Types......Page 80
Floating-Point Types (float, double)......Page 81
Decimal Type......Page 82
Literal Values......Page 83
Boolean Type (bool)......Page 88
Character Type (char)......Page 89
Strings......Page 91
null......Page 99
The void Nontype......Page 100
Value Types......Page 103
Reference Types......Page 104
Nullable Modifier......Page 105
Explicit Cast......Page 106
Type Conversion without Casting......Page 110
Arrays......Page 112
Declaring an Array......Page 113
Instantiating and Assigning Arrays......Page 114
Using an Array......Page 118
Strings as Arrays......Page 124
Common Errors......Page 126
Summary......Page 129
3 Operators and Control Flow......Page 131
Plus and Minus Unary Operators (+, -)......Page 132
Arithmetic Binary Operators (+, -, , /, %)......Page 133
Parenthesis Operator......Page 140
Assignment Operators (+=, -=, =, /=, %=)......Page 141
Increment and Decrement Operators (++, --)......Page 142
Introducing Flow Control......Page 146
if Statement......Page 150
Nested if......Page 151
Code Blocks ({})......Page 153
Scope and Declaration Space......Page 155
Boolean Expressions......Page 157
Relational and Equality Operators......Page 158
Logical Boolean Operators......Page 159
Conditional Operator (?)......Page 161
Null Coalescing Operator (??)......Page 162
Bitwise Operators (<<, >>, |, &, ^, ~)......Page 163
Shift Operators (<<, >>, <<=, >>=)......Page 164
Bitwise Operators (&, |, ^)......Page 165
Bitwise Complement Operator (~)......Page 168
The while and do/while Loops......Page 169
The for Loop......Page 172
The foreach Loop......Page 175
The switch Statement......Page 178
The break Statement......Page 180
The continue Statement......Page 183
The goto Statement......Page 185
C# Preprocessor Directives......Page 186
Excluding and Including Code (#if, #elif, #else, #endif)......Page 188
Emitting Errors and Warnings (#error, #warning)......Page 189
Turning Off Warning Messages (#pragma)......Page 190
Specifying Line Numbers (#line)......Page 191
Hints for Visual Editors (#region, #endregion)......Page 192
Summary......Page 193
4 Methods and Parameters......Page 197
Calling a Method......Page 198
Namespace......Page 200
Type Name......Page 202
Method Return......Page 203
Statement versus Method Call......Page 204
Declaring a Method......Page 205
Method Return Declaration......Page 207
The using Directive......Page 209
Aliasing......Page 212
Returns and Parameters on Main()......Page 213
Value Parameters......Page 216
Reference Parameters (ref)......Page 218
Output Parameters (out)......Page 219
Parameter Arrays (params)......Page 221
Recursion......Page 224
Method Overloading......Page 227
Optional Parameters......Page 230
Basic Error Handling with Exceptions......Page 234
Trapping Errors......Page 235
Reporting Errors Using a throw Statement......Page 244
Summary......Page 247
5 Classes......Page 249
Declaring and Instantiating a Class......Page 253
Declaring an Instance Field......Page 257
Accessing an Instance Field......Page 258
Instance Methods......Page 259
Using the this Keyword......Page 261
Access Modifiers......Page 268
Properties......Page 270
Declaring a Property......Page 271
Automatically Implemented Properties......Page 273
Naming Conventions......Page 275
Using Properties with Validation......Page 276
Read-Only and Write-Only Properties......Page 278
Access Modifiers on Getters and Setters......Page 279
Properties as Virtual Fields......Page 280
Properties and Method Calls Not Allowed as ref or out Parameter Values......Page 282
Constructors......Page 284
Declaring a Constructor......Page 285
Object Initializers......Page 287
Overloading Constructors......Page 289
Constructor Chaining: Calling another Constructor Using this......Page 291
Static Members......Page 295
Static Fields......Page 296
Static Methods......Page 299
Static Constructors......Page 301
Static Properties......Page 302
Static Classes......Page 303
Extension Methods......Page 304
const......Page 306
readonly......Page 307
Nested Classes......Page 308
Partial Classes......Page 310
Defining a Partial Class......Page 311
Partial Methods......Page 312
Summary......Page 315
6 Inheritance......Page 317
Derivation......Page 318
Casting between Base and Derived Types......Page 320
private Access Modifier......Page 323
protected Access Modifier......Page 324
Single Inheritance......Page 326
Overriding the Base Class......Page 329
virtual Modifier......Page 330
new Modifier......Page 334
base Member......Page 339
Constructors......Page 340
Abstract Classes......Page 341
All Classes Derive from System.Object......Page 347
Verifying the Underlying Type with the is Operator......Page 349
Conversion Using the as Operator......Page 350
Summary......Page 351
7 Interfaces......Page 353
Introducing Interfaces......Page 354
Polymorphism through Interfaces......Page 355
Interface Implementation......Page 360
Explicit Member Implementation......Page 362
Implicit Member Implementation......Page 363
Explicit versus Implicit Interface Implementation......Page 364
Interface Inheritance......Page 366
Multiple Interface Inheritance......Page 369
Extension Methods on Interfaces......Page 370
Implementing Multiple Inheritance via Interfaces......Page 371
Versioning......Page 374
Interfaces Compared with Classes......Page 376
Summary......Page 377
8 Value Types......Page 379
Structs......Page 380
Initializing structs......Page 384
Inheritance and Interfaces with Value Types......Page 386
Boxing......Page 387
Enums......Page 394
Type Compatibility between Enums......Page 397
Converting between Enums and Strings......Page 398
Enums as Flags......Page 399
Summary......Page 404
Overriding object Members......Page 405
Overriding GetHashCode()......Page 406
Overriding Equals()......Page 409
Operator Overloading......Page 417
Comparison Operators (==, !=, <, >, <=, >=)......Page 418
Binary Operators (+, -, *, /, %, &, |, ^, <<, >>)......Page 419
Unary Operators (+, -, !, ~, ++, --, true, false)......Page 421
Conversion Operators......Page 423
Referencing Other Assemblies......Page 425
Changing the Assembly Target......Page 426
Encapsulation of Types......Page 427
Defining Namespaces......Page 430
Namespace Alias Qualifier......Page 432
XML Comments......Page 433
Associating XML Comments with Programming Constructs......Page 434
Generating an XML Documentation File......Page 436
Garbage Collection......Page 438
Weak References......Page 439
Finalizers......Page 441
Deterministic Finalization with the using Statement......Page 443
Garbage Collection and Finalization......Page 446
Lazy Initialization......Page 448
Summary......Page 451
Multiple Exception Types......Page 453
Catching Exceptions......Page 455
General Catch Block......Page 457
Guidelines for Exception Handling......Page 459
Defining Custom Exceptions......Page 462
Summary......Page 467
11 Generics......Page 469
C# without Generics......Page 470
Using a Generic Class......Page 475
Defining a Simple Generic Class......Page 477
Benefits of Generics......Page 478
Type Parameter Naming Guidelines......Page 479
Generic Interfaces and Structs......Page 480
Defining a Constructor and a Finalizer......Page 482
Specifying a Default Value......Page 483
Multiple Type Parameters......Page 484
Arity in Abundance......Page 485
Nested Generic Types......Page 486
Constraints......Page 487
Interface Constraints......Page 490
Base Class Constraints......Page 492
struct/class Constraints......Page 493
Constructor Constraints......Page 494
Constraint Inheritance......Page 495
Generic Methods......Page 501
Type Inferencing......Page 502
Specifying Constraints......Page 503
Covariance and Contravariance......Page 505
Enabling Covariance with the out Type Parameter Modifier in C# 4.0......Page 506
Enabling Contravariance with the in Type Parameter Modifier in C# 4.0......Page 508
Support for Parameter Covariance and Contravariance in Arrays......Page 510
Generic Internals......Page 511
Instantiating Generics Based on Value Types......Page 512
Instantiating Generics Based on Reference Types......Page 513
Summary......Page 515
12 Delegates and Lambda Expressions......Page 517
Defining the Scenario......Page 518
Delegate Data Types......Page 520
Delegate Internals......Page 521
Defining a Delegate Type......Page 522
Instantiating a Delegate......Page 523
Anonymous Methods......Page 528
System-Defined Delegates: Func<>......Page 531
Statement Lambdas......Page 534
Expression Lambdas......Page 537
Outer Variables......Page 543
Expression Trees......Page 546
Summary......Page 554
13 Events......Page 555
Defining Subscriber Methods......Page 556
Defining the Publisher......Page 558
Hooking Up the Publisher and Subscribers......Page 559
Invoking a Delegate......Page 560
Check for null......Page 561
Delegate Operators......Page 562
Sequential Invocation......Page 564
Error Handling......Page 567
Method Returns and Pass-by-Reference......Page 570
Why Events?......Page 571
Declaring an Event......Page 573
Coding Conventions......Page 574
Generics and Delegates......Page 576
Customizing the Event Implementation......Page 580
Summary......Page 581
14 Collection Interfaces with Standard Query Operators......Page 583
Anonymous Types and Implicitly Typed Local Variables......Page 584
Anonymous Types......Page 585
Implicitly Typed Local Variables (var)......Page 586
More about Anonymous Types and Implicit Local Variables......Page 588
Collection Initializers......Page 591
foreach with Arrays......Page 594
foreach with I Enumerable
Standard Query Operators......Page 600
Filtering with Where()......Page 604
Projecting with Select()......Page 605
Counting Elements with Count()......Page 609
Deferred Execution......Page 610
Sorting with OrderBy() and ThenBy()......Page 614
Performing an Inner Join with Join()......Page 620
Grouping Results with GroupBy()......Page 623
Implementing a One-to-Many Relationship with GroupJoin()......Page 625
Calling SelectMany()......Page 628
More Standard Query Operators......Page 630
Summary......Page 634
15 LINQ with Query Expressions......Page 637
Introducing Query Expressions......Page 638
Projection......Page 640
Filtering......Page 646
Sorting......Page 647
The Let Clause......Page 648
Grouping......Page 650
Query Continuation with into......Page 653
Query Expressions as Method Invocations......Page 656
Summary......Page 657
16 Building Custom Collections......Page 659
More Collection Interfaces......Page 660
IComparable
ICollection
List Collections: List
Dictionary Collections: Dictionary
Sorted Collections: SortedDictionary
Stack Collections: Stack
Linked Lists: LinkedList
Providing an Index Operator......Page 678
Iterators......Page 682
Iterator Syntax......Page 684
Yielding Values from an Iterator......Page 685
Iterators and State......Page 687
More Iterator Examples......Page 689
Placing a yield return within a Loop......Page 691
Canceling Further Iteration: yield break......Page 693
Creating Multiple Iterators in a Single Class......Page 696
yield Statement Characteristics......Page 697
Summary......Page 698
17 Reflection, Attributes, and Dynamic Programming......Page 699
Reflection......Page 700
Accessing Metadata Using System.Type......Page 701
Member Invocation......Page 703
Reflection on Generic Types......Page 708
Attributes......Page 711
Custom Attributes......Page 714
Looking for Attributes......Page 715
Initializing an Attribute through a Constructor......Page 716
System.AttributeUsageAttribute......Page 721
Named Parameters......Page 722
Programming with Dynamic Objects......Page 736
Invoking Reflection Using dynamic......Page 737
dynamic Principles and Behaviors......Page 738
Why Dynamic Binding?......Page 742
Static Compilation versus Dynamic Programming......Page 743
Implementing a Custom Dynamic Object......Page 744
Summary......Page 747
18 Multithreading......Page 749
Running and Controlling a Separate Thread......Page 754
ContinueWith()......Page 759
Unhandled Exception Handling on Task......Page 763
Canceling a Task......Page 766
Long-Running Tasks......Page 770
Disposing a Task......Page 771
Executing Iterations in Parallel......Page 772
Parallel Exception Handling with System.AggregateException......Page 776
Canceling a Parallel Loop......Page 777
Running LINQ Queries in Parallel......Page 782
Canceling a PLINQ Query......Page 784
Asynchronous Operations with System.Threading.Thread......Page 786
Thread Management......Page 788
Thread Pooling......Page 790
Unhandled Exceptions on the AppDomain......Page 792
Summary......Page 794
19 Synchronization and More Multithreading Patterns......Page 797
Synchronization......Page 798
Synchronization Using Monitor......Page 802
Using the Lock Keyword......Page 805
Choosing a Lock Object......Page 806
Why to Avoid Locking on this, typeof(type), and string......Page 807
Declaring Fields as volatile......Page 808
Using the System.Threading.Interlocked Class......Page 809
Event Notification with Multiple Threads......Page 811
Synchronization Design Best Practices......Page 812
More Synchronization Types......Page 814
Thread Local Storage......Page 822
Timers......Page 826
Asynchronous Programming Model......Page 831
Calling the APM......Page 832
Calling the APM Using TPL......Page 839
Asynchronous Delegate Invocation......Page 845
Passing Data to and from an Alternate Thread......Page 847
Event-Based Asynchronous Pattern (EAP)......Page 849
Background Worker Pattern......Page 852
Establishing the Pattern......Page 855
Exception Handling......Page 856
Windows Forms......Page 857
Windows Presentation Foundation (WPF)......Page 859
Summary......Page 862
20 Platform Interoperability and Unsafe Code......Page 863
Platform Invoke......Page 864
Declaring External Functions......Page 865
Parameter Data Types......Page 866
Using ref Rather Than Pointers......Page 867
Using StructLayoutAttribute for Sequential Layout......Page 868
Error Handling......Page 869
Using SafeHandle......Page 871
Calling External Functions......Page 874
Simplifying API Calls with Wrappers......Page 876
Guidelines......Page 877
Unsafe Code......Page 878
Pointer Declaration......Page 880
Assigning a Pointer......Page 882
Dereferencing a Pointer......Page 885
Summary......Page 887
21 The Common Language Infrastructure......Page 891
Defining the Common Language Infrastructure (CLI)......Page 892
CLI Implementations......Page 893
C# Compilation to Machine Code......Page 895
Garbage Collection......Page 897
Garbage Collection on .NET......Page 898
Type Safety......Page 899
Platform Portability......Page 900
Performance......Page 901
Application Domains......Page 902
Assemblies, Manifests, and Modules......Page 903
Common Type System (CTS)......Page 906
Common Language Specification (CLS)......Page 907
Metadata......Page 908
Summary......Page 910
A: Downloading and Installing the C# Compiler and the CLI Platform......Page 913
B: Full Source Code Listings......Page 917
C: Concurrent Classes from System.Collections.Concurrent......Page 943
D: C# 2.0 Topics......Page 947
E: C# 3.0 Topics......Page 951
F: C# 4.0 Topics......Page 953
A......Page 955
C......Page 957
D......Page 961
E......Page 962
F......Page 963
H......Page 964
I......Page 965
J......Page 966
L......Page 967
M......Page 968
N......Page 969
O......Page 970
P......Page 971
R......Page 972
S......Page 973
T......Page 975
W......Page 977
Y......Page 978
π 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 contains
When you're programming C# 4.0 and need a little help, this tightly focused and practical book tells you exactly what you need to know - without long introductions or bloated examples. It's ideal as a succinct quick reference or as a guide to get you rapidly up to speed if you already know Java, C++
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.
The Most Comprehensive C# Resource AvailableWith its support for Language-Integrated Query (LINQ), C# 3.0 has revolutionized C# programming, and bestselling author Herb Schildt has updated and expanded his classic programming reference to cover it. Using carefully crafted explanations, insider tips,
9 chapters plus 3 appendices cover the traditional core material of MIS. A comprehensive set of group projects and e-commerce projects support an applied component to the course. Consistent with Haagβs best-selling MIS for the Information Age, IS Essentials 3/e conveys the impact of IS on the indi