**Praise for __Essential C# 3.0__** > “If you want to be a C# developer, or if you want to enhance your C# programming skills, there is no more useful tool than a well-crafted book on the subject. You are holding such a book in your hands.” > > __–From the Foreword by Charlie Calvert, Community Program Manager, Visual C#, Microsoft__ > > > > “In a time when you can search online for any reference material, Essential C# 3.0 is the one book I still bother to carry in my bag. Any developer can hit F1 for help with the language syntax; this book, however, arms me with the knowledge to make better design and coding decisions (and the confidence to say so). You know when a book has more than twenty pages of index that it’s got depth covered, and although Essential C# 3.0 has introductory chapters, it progresses effortlessly into the lesser known corners of the language, which makes this book indispensible to any level of C# developer.” > > __–Troy Magennis, C# MVP and creator of HookedOnLINQ.com__ > > > > “If you are new to C#, as I was, Mark’s book is an excellent way to start. His approach takes you step by step through the structure and syntax of the language, making it easy to understand how things work. I found the code samples extremely helpful and supportive of the concepts being discussed.” > > __–Robert Stokesbary, senior consultant, Option Explicit__ > > > > “This book expands on the concepts introduced in Essential C# 2.0 to include C# 3.0 enhancements. One great aspect of this book is that it not only covers the new 3.0 enhancements in great detail, but it also shows where it makes sense to utilize these new features over the existing 2.0 features. While I have been programming C# for many years, I find myself referring to this book often and always seem to find something new buried in the pages. This is a must-read for both the C# newbie as well as the C# seasoned veteran.” > > __–Michael Stokesbary, senior software engineer, Itron Inc.__ > > **Praise for the First Edition** > “Essential C# 2.0 pulls off a very difficult task. The early chapters are comprehensible by beginning developers, while the later chapters pull no punches and provide the experienced developer with the detailed information they need to make the most of C# 2.0. Starting with the first chapter, Mark has successfully interwoven tidbits of information useful to even the most advanced developer while keeping the book approachable.” > > __–Chris Kinsman, chief architect, Vertafore, Microsoft Regional Director__ > > > > “How refreshing! This book deals with C# thoroughly, rather than skimming over the whole .NET framework. It is valuable to newcomers and professionals alike.” > > __–Jon Skeet, C# MVP__ > > > > “Essential C# 2.0 is a one-stop shop for an experienced programmer looking to ramp up on one of the hottest languages around today. Mark delivers an intelligent and detailed tour of C#, providing newcomers to the language with a solid foundation of skill on which to build their next generation of applications.” > > __–Stephen Toub, technical editor, MSDN Magazine__ > > > > “This book provides complete, up-to-date coverage of all the programming constructs in C#. Masterfully organized, it allows beginning programmers to get on board and leads more experienced programmers into the world of structured programming. Because of its unwavering focus on the essential programming constructs of C#–such as generics, delegates, and much more–this book is indispensable. For programmers who want to solve their day-to-day programming issues using the latest features this modern programming language has to offer, this book is indispensable.” > > __–Narendra Poflee, IT integration specialist, Itron Inc.__ > > > > “Essential C# 2.0 is an ideal book for all programmers interested in C#. If you are a beginner, you will quickly learn the basics of C# programming and become familiar with the concepts. The flow of the text is easy to follow and does a great job of not repeating concepts that have already been covered. For the experienced programmer, this book has priceless nuggets embedded within its pages, making it a great read for programmers who are already familiar with C#. This will be a book that I will keep next to my computer for years to come.” > > __–Michael Stokesbary, software engineer, Itron Inc.__ > > **__**Essential C# 3.0**__** is an extremely well-written and well-organized “no-fluff” guide to C# 3.0, which will appeal to programmers at all levels of experience with C#. This fully updated edition dives deep into the new features that are revolutionizing programming, with brand new chapters covering query expressions, lambda expressions, extension methods, collection interface extensions, standard query operators, and LINQ as a whole. Author Mark Michaelis covers the C# language in depth, and each importantconstruct is illustrated with succinct, relevant code examples. (Complete code examples are available online.) Graphical “mind maps” at the beginning of each chapter show what material is covered and how each topic relates to the whole. Topics intended for beginners and advanced readers are clearly marked. Following an introduction to C#, readers learn about * C# primitive data types, value types, reference types, type conversions, and arrays * Operators and control flow, loops, conditional logic, and sequential programming * Methods, parameters, exception handling, and structured programming * Classes, inheritance, structures, interfaces, and object-oriented programming * Well-formed types, operator overloading, namespaces, and garbage collection * Generics, collections, custom collections, and iterators * Delegates and lambda expressions * Standard query operators and query expressions * LINQ: language integrated query * Reflection, attributes, and declarative programming * Threading, synchronization, and multithreaded patterns * Interoperability and unsafe code * The Common Language Infrastructure that underlies C# Whether you are just starting out as a programmer, are an experienced developer looking to learn C#, or are a seasoned C# programmer interested in learning the new features of C# 3.0, **__**Essential C# 3.0**__** gives you just what you need to quickly get up and running writing C# applications. Contents......Page 5 Figures......Page 16 Tables......Page 18 Acknowledgments......Page 35 About the Author......Page 37 1 Introducing C#......Page 38 Hello, World......Page 39 Compiling and Running the Application......Page 40 C# Syntax Fundamentals......Page 41 C# Keywords......Page 42 Type Definition......Page 43 Main......Page 44 Whitespace......Page 47 Data Types......Page 49 Declaring a Variable......Page 50 Assigning a Variable......Page 51 Console Input and Output......Page 52 Getting Input from the Console......Page 53 Writing Output to the Console......Page 54 Comments......Page 56 Managed Execution and the Common Language Infrastructure......Page 59 C# and .NET Versioning......Page 62 Common Intermediate Language and ILDASM......Page 63 2 Data Types......Page 67 Integer Types......Page 68 Floating-Point Types (float, double)......Page 69 Decimal Type......Page 70 Literal Values......Page 71 Boolean Type (bool)......Page 76 Character Type (char)......Page 77 Strings......Page 79 null......Page 87 The void Nontype......Page 88 Value Types......Page 91 Reference Types......Page 92 Nullable Modifier......Page 93 Conversions between Data Types......Page 94 Explicit Cast......Page 95 Type Conversion without Casting......Page 98 Declaring an Array......Page 101 Instantiating and Assigning Arrays......Page 102 Using an Array......Page 106 Strings as Arrays......Page 112 Common Errors......Page 114 3 Operators and Control Flow......Page 118 Plus and Minus Unary Operators (+, –)......Page 119 Arithmetic Binary Operators (+, –, *, /, %)......Page 120 Parenthesis Operator......Page 127 Assignment Operators (+=, –=, *=, /=, %=)......Page 128 Increment and Decrement Operators (++, ––)......Page 129 Introducing Flow Control......Page 133 if Statement......Page 137 Nested if......Page 138 Code Blocks ({})......Page 140 Scope......Page 142 Boolean Expressions......Page 143 Relational and Equality Operators......Page 144 Logical Boolean Operators......Page 146 Logical Negation Operator (!)......Page 147 Conditional Operator (?)......Page 148 Bitwise Operators (< >, |, &, ^, ~)......Page 149 Shift Operators (< >, < >=)......Page 150 Bitwise Operators (&, |, ^)......Page 151 Bitwise Assignment Operators (&=, |=, ^=)......Page 153 The while and do/while Loops......Page 154 The for loop......Page 157 The foreach Loop......Page 160 The switch Statement......Page 163 Jump Statements......Page 165 The break Statement......Page 166 The continue Statement......Page 168 The goto Statement......Page 170 C# Preprocessor Directives......Page 171 Excluding and Including Code (#if, #elif, #else, #endif)......Page 173 Emitting Errors and Warnings (#error, #warning)......Page 174 Turning Off Warning Messages (#pragma)......Page 175 Specifying Line Numbers (#line)......Page 176 Hints for Visual Editors (#region, #endregion)......Page 177 4 Methods and Parameters......Page 181 Calling a Method......Page 182 Namespace......Page 184 Type Name......Page 186 Method Return......Page 187 Statement versus Method Call......Page 188 Declaring a Method......Page 189 Method Return Declaration......Page 191 The using Directive......Page 194 Aliasing......Page 197 Returns and Parameters on Main()......Page 198 Value Parameters......Page 201 Reference Parameters (ref)......Page 202 Output Parameters (out)......Page 204 Parameter Arrays (params)......Page 206 Recursion......Page 208 Method Overloading......Page 211 Basic Error Handling with Exceptions......Page 214 Trapping Errors......Page 215 Reporting Errors Using a throw Statement......Page 223 5 Classes......Page 228 Defining and Instantiating a Class......Page 232 Declaring an Instance Field......Page 236 Accessing an Instance Field......Page 237 Instance Methods......Page 238 Using the this Keyword......Page 239 Access Modifiers......Page 246 Properties......Page 248 Declaring a Property......Page 250 Automatically Implemented Properties......Page 252 Naming Conventions......Page 253 Using Properties with Validation......Page 254 Read-Only and Write-Only Properties......Page 256 Access Modifiers on Getters and Setters......Page 257 Properties as Virtual Fields......Page 258 Properties and Method Calls Not Allowed as ref or out Parameter Values......Page 260 Declaring a Constructor......Page 262 Default Constructors......Page 264 Object Initializers......Page 265 Overloading Constructors......Page 267 Calling Another Constructor Using this......Page 268 Static......Page 272 Static Fields......Page 273 Static Methods......Page 276 Static Constructors......Page 278 Static Properties......Page 279 Static Classes......Page 280 Extension Methods......Page 282 Encapsulating the Data......Page 283 readonly......Page 284 Nested Classes......Page 285 Partial Classes......Page 287 Defining a Partial Class......Page 288 Partial Methods......Page 289 6 Inheritance......Page 293 Derivation......Page 294 Casting between Base and Derived Types......Page 296 private Access Modifier......Page 299 protected Access Modifier......Page 300 Single Inheritance......Page 301 Overriding the Base Class......Page 304 virtual Modifier......Page 305 new Modifier......Page 310 base Member......Page 314 Constructors......Page 315 Abstract Classes......Page 316 Everything Ultimately Derives from System.Object......Page 322 Verifying the Underlying Type with the is Operator......Page 324 Conversion Using the as Operator......Page 325 7 Interfaces......Page 327 Introducing Interfaces......Page 328 Polymorphism through Interfaces......Page 329 Interface Implementation......Page 334 Explicit Member Implementation......Page 336 Implicit Member Implementation......Page 337 Explicit versus Implicit Interface Implementation......Page 338 Casting between the Implementing Class and Its Interfaces......Page 339 Interface Inheritance......Page 340 Multiple Interface Inheritance......Page 342 Extension Methods on Interfaces......Page 343 Implementing Multiple Inheritance via Interfaces......Page 345 Versioning......Page 347 Interfaces Compared with Classes......Page 349 8 Value Types......Page 352 Structs......Page 353 Initializing structs......Page 357 Inheritance and Interfaces with Value Types......Page 359 Boxing......Page 360 Enums......Page 366 Converting between Enums and Strings......Page 370 Enums as Flags......Page 371 Overriding object Members......Page 377 Overriding GetHashCode()......Page 378 Overriding Equals()......Page 380 Guidelines for Implementing Equality......Page 387 Comparison Operators (==, !=, , =)......Page 388 Binary Operators (+, –, *, /, %, &, |, ^, < >)......Page 389 Conditional Logical Operators (&&, ||)......Page 391 Unary Operators (+, –, !, ~, ++, ––, true, false)......Page 392 Conversion Operators......Page 393 Referencing Other Assemblies......Page 395 Changing the Assembly Target......Page 396 Referencing an Assembly......Page 397 Encapsulation of Types......Page 398 Defining Namespaces......Page 400 Namespace Alias Qualifier......Page 402 XML Comments......Page 403 Associating XML Comments with Programming Constructs......Page 404 Generating an XML Documentation File......Page 406 Garbage Collection......Page 407 Weak References......Page 409 Finalizers......Page 411 Deterministic Finalization with the using Statement......Page 412 Garbage Collection and Finalization......Page 415 Resource Utilization and Finalization Guidelines......Page 417 Multiple Exception Types......Page 419 Catching Exceptions......Page 421 General Catch Block......Page 422 Guidelines for Exception Handling......Page 425 Defining Custom Exceptions......Page 427 11 Generics......Page 434 C# without Generics......Page 435 Using a Generic Class......Page 440 Defining a Simple Generic Class......Page 442 Benefits of Generics......Page 443 Type Parameter Naming Guidelines......Page 444 Generic Interfaces and Structs......Page 445 Defining a Constructor and a Finalizer......Page 447 Specifying a Default Value......Page 448 Multiple Type Parameters......Page 449 Nested Generic Types......Page 450 Type Compatibility between Generic Classes with Type-Compatible Type Parameters......Page 451 Constraints......Page 452 Interface Constraints......Page 454 struct/class Constraints......Page 457 Multiple Constraints......Page 458 Constructor Constraints......Page 459 Constraint Inheritance......Page 460 Generic Methods......Page 465 Type Inferencing......Page 466 Specifying Constraints......Page 467 Generic Internals......Page 469 Instantiating Generics Based on Value Types......Page 470 Instantiating Generics Based on Reference Types......Page 471 12 Delegates and Lambda Expressions......Page 474 Defining the Scenario......Page 475 Delegate Data Types......Page 477 Delegate Internals......Page 478 Defining a Delegate Type......Page 479 Instantiating a Delegate......Page 480 Anonymous Methods......Page 485 System-Defined Delegates: Func......Page 488 Statement Lambdas......Page 489 Expression Lambdas......Page 492 Outer Variables......Page 498 Expression Trees......Page 500 13 Events......Page 508 Defining Subscriber Methods......Page 509 Defining the Publisher......Page 511 Hooking Up the Publisher and Subscribers......Page 512 Invoking a Delegate......Page 513 Check for null......Page 514 Delegate Operators......Page 515 Sequential Invocation......Page 517 Error Handling......Page 520 Method Returns and Pass-By-Reference......Page 522 Why Events?......Page 524 Declaring an Event......Page 526 Coding Conventions......Page 527 Generics and Delegates......Page 529 Customizing the Event Implementation......Page 533 14 Collection Interfaces with Standard Query Operators......Page 535 Anonymous Types......Page 536 Implicitly Typed Local Variables......Page 538 More about Anonymous Types and Implicit Local Variables......Page 540 Collection Initializers......Page 542 foreach with Arrays......Page 545 foreach with IEnumerable ......Page 546 Do Not Modify Collections during foreach Iteration......Page 550 Standard Query Operators......Page 551 Filtering with Where()......Page 554 Projecting with Select()......Page 556 Deferred Execution......Page 558 Sorting with OrderBy() and ThenBy()......Page 562 Performing an Inner Join with Join()......Page 568 Implementing a One-to-Many Relationship with GroupJoin()......Page 571 Calling SelectMany()......Page 575 More Standard Query Operators......Page 576 15 Query Expressions......Page 582 Introducing Query Expressions......Page 583 Projection......Page 585 Filtering......Page 591 Sorting......Page 592 Let......Page 593 Grouping......Page 595 Query Expressions as Method Invocations......Page 600 16 Building Custom Collections......Page 603 More Collection Interfaces......Page 604 IList versus IDictionary ......Page 605 IComparable ......Page 606 ICollection ......Page 608 List Collections: List ......Page 609 Dictionary Collections: Dictionary ......Page 614 Sorted Collections: SortedDictionary and SortedList ......Page 619 Stack Collections: Stack ......Page 620 Queue Collections: Queue ......Page 621 Providing an Index Operator......Page 623 Iterators......Page 627 Defining an Iterator......Page 628 Iterator Syntax......Page 629 Yielding Values from an Iterator......Page 630 Iterators and State......Page 632 More Iterator Examples......Page 633 Placing a yield return within a Loop......Page 635 Canceling Further Iteration: yield break......Page 638 Creating Multiple Iterators in a Single Class......Page 640 yield Statement Characteristics......Page 641 17 Reflection and Attributes......Page 643 Reflection......Page 644 Accessing Metadata Using System.Type......Page 645 Member Invocation......Page 647 Reflection on Generic Types......Page 652 Attributes......Page 655 Looking for Attributes......Page 659 Initializing an Attribute through a Constructor......Page 660 System.AttributeUsageAttribute......Page 665 Named Parameters......Page 667 18 Multithreading......Page 682 Running and Controlling a Separate Thread......Page 685 Starting a Thread......Page 687 Thread Management......Page 688 Passing Parameters to Threads......Page 690 Thread Pooling......Page 694 Unhandled Exceptions......Page 695 Synchronization......Page 697 Synchronization Using Monitor......Page 700 Using the lock Keyword......Page 702 Choosing a lock Object......Page 704 Declaring Fields as volatile......Page 705 Using the System.Threading.Interlocked Class......Page 706 Event Notification with Multiple Threads......Page 707 Synchronization Design Best Practices......Page 709 More Synchronization Types......Page 710 Timers......Page 716 19 Multithreading Patterns......Page 724 Asynchronous Results Pattern......Page 725 Introducing the Asynchronous Results Pattern......Page 726 Passing Data to and from an Alternate Thread......Page 728 Receiving Notification of Thread Completion......Page 733 Passing Arbitrary State......Page 736 Asynchronous Results Conclusions......Page 738 Background Worker Pattern......Page 739 Establishing the Pattern......Page 742 Exception Handling......Page 743 Windows Forms......Page 744 20 Platform Interoperability and Unsafe Code......Page 748 Platform Invoke......Page 749 Declaring External Functions......Page 750 Parameter Data Types......Page 751 Using ref Rather Than Pointers......Page 752 Using StructLayoutAttribute for Sequential Layout......Page 753 Error Handling......Page 754 Using SafeHandle......Page 756 Calling External Functions......Page 759 Simplifying API Calls with Wrappers......Page 761 Guidelines......Page 762 Unsafe Code......Page 763 Pointer Declaration......Page 765 Assigning a Pointer......Page 767 Dereferencing a Pointer......Page 769 Accessing the Member of a Referent Type......Page 771 21 The Common Language Infrastructure......Page 774 Defining the Common Language Infrastructure (CLI)......Page 775 CLI Implementations......Page 776 C# Compilation to Machine Code......Page 777 Garbage Collection......Page 780 Garbage Collection on .NET......Page 781 Type Safety......Page 782 Platform Portability......Page 783 Performance......Page 784 Application Domains......Page 785 Assemblies, Manifests, and Modules......Page 786 Common Type System (CTS)......Page 789 Common Language Specification (CLS)......Page 790 Metadata......Page 791 A: Downloading and Installing the C# Compiler and the CLI Platform......Page 795 B: Full Source Code Listings......Page 798 C: C# 3.0 Topics......Page 823 A......Page 828 B......Page 829 C......Page 830 D......Page 833 E......Page 834 G......Page 835 I......Page 836 L......Page 838 M......Page 839 N......Page 840 P......Page 841 S......Page 843 T......Page 845 V......Page 846 Z......Page 847
Essential C# 3.0 is an extremely well-written and well-organized “no-fluff” guide to C# 3.0, which will appeal to programmers at all levels of experience with C#. This fully updated edition dives deep into the new features that are revolutionizing programming, with brand new chapters covering query expressions, lambda expressions, extension methods, collection interface extensions, standard query operators, and LINQ as a whole.
Author Mark Michaelis covers the C# language in depth, and each importantconstruct is illustrated with succinct, relevant code examples. (Complete code examples are available online.) Graphical “mind maps” at the beginning of each chapter show what material is covered and how each topic relates to the whole. Topics intended for beginners and advanced readers are clearly marked.
Following an introduction to C#, readers learn about
- C# primitive data types, value types, reference types, type conversions, and arrays
- Operators and control flow, loops, conditional logic, and sequential programming
- Methods, parameters, exception handling, and structured programming
- Classes, inheritance, structures, interfaces, and object-oriented programming
- Well-formed types, operator overloading, namespaces, and garbage collection
- Generics, collections, custom collections, and iterators
- Delegates and lambda expressions
- Standard query operators and query expressions
- LINQ: language integrated query
- Reflection, attributes, and declarative programming
- Threading, synchronization, and multithreaded patterns
- Interoperability and unsafe code
- The Common Language Infrastructure that underlies C#
Whether you are just starting out as a programmer, are an experienced developer looking to learn C#, or are a seasoned C# programmer interested in learning the new features of C# 3.0, Essential C# 3.0 gives you just what you need to quickly get up and running writing C# applications.
A updated guide to the C# programming language covers the fundamentals of C# 3.0, including such topics as syntax fundamentals, working with variables, console input and output, common intermediate language data strings, exception handling, and multithreading patterns, with new chapters on LINQ, Query Expressions, Lambda Expressions, Standard Query Operators, Delegates, and Collections Interfaces. Original. (Intermediate)