You are previewing Pro C# 2010 and the .NET 4 Platform, Fifth Edition.
O'Reilly logo
Pro C# 2010 and the .NET 4 Platform, Fifth Edition

Book Description

The first edition of this book was released at the 2001 Tech-Ed conference in Atlanta, Georgia. At that time, the .NET platform was still a beta product, and in many ways, so was this book. This is not to say that the early editions of this text did not have merit—after all, the book was a 2002 Jolt Award finalist and it won the 2003 Referenceware Excellence Award. However, over the years that author Andrew Troelsen spent working with the common language runtime (CLR), he gained a much deeper understanding of the .NET platform and the subtleties of the C# programming language, and he feels that this fifth edition of the book is as close to a "final release" as he's come yet.

This new edition has been comprehensively revised and rewritten to make it accurately reflect the C# 4 language specification for the .NET 4 platform. You'll find new chapters covering the important concepts of dynamic lookups, named and optional arguments, Parallel LINQ (PLINQ), improved COM interop, and variance for generics.

If you're checking out this book for the first time, do understand that it's targeted at experienced software professionals and/or graduate students of computer science (so don't expect three chapters on iteration or decision constructs!). The mission of this text is to provide you with a rock-solid foundation in the C# programming language and the core aspects of the .NET platform (assemblies, remoting, Windows Forms, Web Forms, ADO.NET, XML web services, etc.). Once you digest the information presented in these 25 chapters, you'll be in a perfect position to apply this knowledge to your specific programming assignments, and you'll be well equipped to explore the .NET universe on your own terms.

Table of Contents

  1. Copyright
  2. Dedication
  3. About the Author
  4. About the Technical Reviewer
  5. Acknowledgments
  6. Introduction
  7. Introducing C# and the .NET Platform
    1. The Philosophy of .NET
      1. Understanding the Previous State of Affairs
      2. The .NET Solution
      3. Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS)
      4. Additional .NET-Aware Programming Languages
      5. An Overview of .NET Assemblies
      6. Understanding the Common Type System
      7. Understanding the Common Language Specification
      8. Understanding the Common Language Runtime
      9. The Assembly/Namespace/Type Distinction
      10. Exploring an Assembly Using ildasm.exe
      11. Exploring an Assembly Using Reflector
      12. Deploying the .NET Runtime
      13. The Platform-Independent Nature of .NET
      14. Summary
    2. Building C# Applications
      1. The Role of the .NET Framework 4.0 SDK
      2. Building C# Applications Using csc.exe
      3. Building .NET Applications Using Notepad++
      4. Building .NET Applications Using SharpDevelop
      5. Building .NET Applications Using Visual C# 2010 Express
      6. Building .NET Applications Using Visual Studio 2010
      7. Summary
  8. Core C# Programming Constructs
    1. Core C# Programming Constructs, Part I
      1. The Anatomy of a Simple C# Program
      2. An Interesting Aside: Some Additional Members of the System.Environment Class
      3. The System.Console Class
      4. System Data Types and C# Shorthand Notation
      5. Working with String Data
      6. Narrowing and Widening Data Type Conversions
      7. Understanding Implicitly Typed Local Variables
      8. C# Iteration Constructs
      9. Decision Constructs and the Relational/Equality Operators
      10. Summary
    2. Core C# Programming Constructs, Part II
      1. Methods and Parameter Modifiers
      2. Understanding C# Arrays
      3. Understanding the Enum Type
      4. Understanding the Structure Type
      5. Understanding Value Types and Reference Types
      6. Understanding C# Nullable Types
      7. Summary
    3. Defining Encapsulated Class Types
      1. Introducing the C# Class Type
      2. Understanding Constructors
      3. The Role of the this Keyword
      4. Understanding the static Keyword
      5. Defining the Pillars of OOP
      6. C# Access Modifiers
      7. The First Pillar: C#'s Encapsulation Services
      8. Understanding Automatic Properties
      9. Understanding Object Initializer Syntax
      10. Working with Constant Field Data
      11. Understanding Partial Types
      12. Summary
    4. Understanding Inheritance and Polymorphism
      1. The Basic Mechanics of Inheritance
      2. Revising Visual Studio Class Diagrams
      3. The Second Pillar of OOP: The Details of Inheritance
      4. Programming for Containment/Delegation
      5. The Third Pillar of OOP: C#'s Polymorphic Support
      6. Understanding Base Class/Derived Class Casting Rules
      7. The Master Parent Class: System.Object
      8. Summary
    5. Understanding Structured Exception Handling
      1. Ode to Errors, Bugs, and Exceptions
      2. The Role of .NET Exception Handling
      3. The Simplest Possible Example
      4. Configuring the State of an Exception
      5. System-Level Exceptions (System.SystemException)
      6. Application-Level Exceptions (System.ApplicationException)
      7. Processing Multiple Exceptions
      8. Who Is Throwing What?
      9. The Result of Unhandled Exceptions
      10. Debugging Unhandled Exceptions Using Visual Studio
      11. A Brief Word Regarding Corrupted State Exceptions (CSE)
      12. Summary
    6. Understanding Object Lifetime
      1. Classes, Objects, and References
      2. The Basics of Object Lifetime
      3. The Role of Application Roots
      4. Understanding Object Generations
      5. Concurrent Garbage Collection under .NET 1.0 – 3.5
      6. The System.GC Type
      7. Building Finalizable Objects
      8. Building Disposable Objects
      9. Building Finalizable and Disposable Types
      10. Understanding Lazy Object Instantiation
      11. Summary
  9. Advanced C# Programming Constructs
    1. Working with Interfaces
      1. Understanding Interface Types
      2. Defining Custom Interfaces
      3. Implementing an Interface
      4. Invoking Interface Members at the Object Level
      5. Interfaces As Parameters
      6. Interfaces As Return Values
      7. Arrays of Interface Types
      8. Implementing Interfaces Using Visual Studio 2010
      9. Resolving Name Clashes via Explicit Interface Implementation
      10. Designing Interface Hierarchies
      11. Building Enumerable Types (IEnumerable and IEnumerator)
      12. Building Cloneable Objects (ICloneable)
      13. Building Comparable Objects (IComparable)
      14. Summary
    2. Understanding Generics
      1. The Issues with Non-Generic Collections
      2. The Role of Generic Type Parameters
      3. The System.Collections.Generic Namespace
      4. Creating Custom Generic Methods
      5. Creating Custom Generic Structures and Classes
      6. Constraining Type Parameters
      7. Summary
    3. Delegates, Events, and Lambdas
      1. Understanding the .NET Delegate Type
      2. Defining a Delegate Type in C#
      3. The System.MulticastDelegate and System.Delegate Base Classes
      4. The Simplest Possible Delegate Example
      5. Sending Object State Notifications using Delegates
      6. Method Group Conversion Syntax
      7. Understanding Delegate Covariance
      8. Understanding Generic Delegates
      9. Understanding C# Events
      10. Understanding C# Anonymous Methods
      11. Understanding Lambda Expressions
      12. Summary
    4. Advanced C# Language Features
      1. Understanding Indexer Methods
      2. Understanding Operator Overloading
      3. Understanding Custom Type Conversions
      4. Working with Pointer Types
      5. Summary
    5. LINQ to Objects
      1. LINQ Specific Programming Constructs
      2. Understanding the Role of LINQ
      3. Applying LINQ Queries to Primitive Arrays
      4. Returning the Result of a LINQ Query
      5. Applying LINQ Queries to Collection Objects
      6. Investigating the C# LINQ Query Operators
      7. The Internal Representation of LINQ Query Statements
      8. Summary
  10. Programming with .NET Assemblies
    1. Configuring .NET Assemblies
      1. Defining Custom Namespaces
      2. The Role of .NET Assemblies
      3. Understanding the Format of a .NET Assembly
      4. Building and Consuming a Single-File Assembly
      5. Building and Consuming a Multifile Assembly
      6. Understanding Private Assemblies
      7. Understanding Shared Assemblies
      8. Consuming a Shared Assembly
      9. Configuring Shared Assemblies
      10. Understanding Publisher Policy Assemblies
      11. Understanding the <codeBase> Element
      12. The System.Configuration Namespace
      13. Summary
    2. Type Reflection, Late Binding, and Attribute-Based Programming
      1. The Necessity of Type Metadata
      2. Understanding Reflection
      3. Building a Custom Metadata Viewer
      4. Dynamically Loading Assemblies
      5. Reflecting on Shared Assemblies
      6. Understanding Late Binding
      7. Understanding the Role of .NET Attributes
      8. Building Custom Attributes
      9. Assembly-Level (and Module-Level) Attributes
      10. Reflecting on Attributes Using Early Binding
      11. Reflecting on Attributes Using Late Binding
      12. Putting Reflection, Late Binding, and Custom Attributes in Perspective
      13. Building an Extendable Application
      14. Summary
    3. Processes, AppDomains, and Object Contexts
      1. The Role of a Windows Process
      2. Interacting with Processes Under the .NET Platform
      3. Understanding .NET Application Domains
      4. Interacting with the Default Application Domain
      5. Creating New Application Domains
      6. Understanding Object Context Boundaries
      7. Summarizing Processes, AppDomains, and Context
      8. Summary
    4. Understanding CIL and the Role of Dynamic Assemblies
      1. Reasons for Learning the Grammar of CIL
      2. Examining CIL Directives, Attributes, and Opcodes
      3. Pushing and Popping: The Stack-Based Nature of CIL
      4. Understanding Round-Trip Engineering
      5. Understanding CIL Directives and Attributes
      6. .NET Base Class Library, C#, and CIL Data Type Mappings
      7. Defining Type Members in CIL
      8. Examining CIL Opcodes
      9. Building a .NET Assembly with CIL
      10. Understanding Dynamic Assemblies
      11. Summary
    5. Dynamic Types and the Dynamic Language Runtime
      1. The Role of the C# dynamic Keyword
      2. The Role of the Dynamic Language Runtime (DLR)
      3. Simplifying Late Bound Calls Using Dynamic Types
      4. Simplifying COM Interoperability using Dynamic Data
      5. COM Interop using C# 4.0 Language Features
      6. Summary
  11. Introducing the .NET Base Class Libraries
    1. Multithreaded and Parallel Programming
      1. The Process/AppDomain/Context/Thread Relationship
      2. A Brief Review of the .NET Delegate
      3. The Asynchronous Nature of Delegates
      4. Invoking a Method Asynchronously
      5. The System.Threading Namespace
      6. The System.Threading.Thread Class
      7. Programmatically Creating Secondary Threads
      8. The Issue of Concurrency
      9. Programming with Timer Callbacks
      10. Understanding the CLR ThreadPool
      11. Parallel Programming under the .NET Platform
      12. Parallel LINQ Queries (PLINQ)
      13. Summary
    2. File I/O and Object Serialization
      1. Exploring the System.IO Namespace
      2. The Directory(Info) and File(Info) Types
      3. Working with the DirectoryInfo Type
      4. Working with the Directory Type
      5. Working with the DriveInfo Class Type
      6. Working with the FileInfo Class
      7. Working with the File Type
      8. The Abstract Stream Class
      9. Working with StreamWriters and StreamReaders
      10. Working with StringWriters and StringReaders
      11. Working with BinaryWriters and BinaryReaders
      12. Watching Files Programmatically
      13. Understanding Object Serialization
      14. Configuring Objects for Serialization
      15. Choosing a Serialization Formatter
      16. Serializing Objects Using the BinaryFormatter
      17. Serializing Objects Using the SoapFormatter
      18. Serializing Objects Using the XmlSerializer
      19. Serializing Collections of Objects
      20. Customizing the Soap/Binary Serialization Process
      21. Summary
    3. ADO.NET Part I: The Connected Layer
      1. A High-Level Definition of ADO.NET
      2. Understanding ADO.NET Data Providers
      3. Additional ADO.NET Namespaces
      4. The Types of the System.Data Namespace
      5. Abstracting Data Providers Using Interfaces
      6. Creating the AutoLot Database
      7. The ADO.NET Data Provider Factory Model
      8. Understanding the Connected Layer of ADO.NET
      9. Working with Data Readers
      10. Building a Reusable Data Access Library
      11. Creating a Console UI–Based Front End
      12. Understanding Database Transactions
      13. Summary
    4. ADO.NET Part II: The Disconnected Layer
      1. Understanding the Disconnected Layer of ADO.NET
      2. Understanding the Role of the DataSet
      3. Working with DataColumns
      4. Working with DataRows
      5. Working with DataTables
      6. Binding DataTable Objects to Windows Forms GUIs
      7. Working with Data Adapters
      8. Adding Disconnection Functionality to AutoLotDAL.dll
      9. Multitabled DataSet Objects and Data Relationships
      10. The Windows Forms Database Designer Tools
      11. Isolating Strongly Typed Database Code into a Class Library
      12. Programming with LINQ to DataSet
      13. Summary
    5. ADO.NET Part III: The Entity Framework
      1. Understanding the Role of Entity Framework
      2. Building and Analyzing your First EDM
      3. Programming Against the Conceptual Model
      4. AutoLotDAL Version 4.0, Now with Entities
      5. Data Binding Entities to Windows Forms GUIs
      6. Summary
    6. Introducing LINQ to XML
      1. A Tale of Two XML APIs
      2. Members of the System.Xml.Linq Namespace
      3. Working with XElement and XDocument
      4. Manipulating an in Memory XML Document
      5. Summary
    7. Introducing Windows Communication Foundation
      1. A Potpourri of Distributed Computing APIs
      2. The Role of WCF
      3. Investigating the Core WCF Assemblies
      4. The Visual Studio WCF Project Templates
      5. The Basic Composition of a WCF Application
      6. The ABCs of WCF
      7. Building a WCF Service
      8. The [ServiceContract] Attribute
      9. Hosting the WCF Service
      10. Building the WCF Client Application
      11. Simplifying Configuration Settings with WCF 4.0
      12. Using the WCF Service Library Project Template
      13. Hosting the WCF Service within a Windows Service
      14. Invoking a Service Asynchronously from the Client
      15. Designing WCF Data Contracts
      16. Summary
    8. Introducing Windows Workflow Foundation 4.0
      1. Defining a Business Process
      2. Building a (Painfully) Simple Workflow
      3. The WF 4.0 Runtime
      4. Examining the Workflow 4.0 Activities
      5. Building a Flowchart Workflow
      6. Isolating Workflows into Dedicated Libraries
      7. Consuming the Workflow Library
      8. Summary
  12. Building Desktop User Interfaces with WPF
    1. Introducing Windows Presentation Foundation and XAML
      1. The Motivation Behind WPF
      2. The Various Flavors of WPF
      3. Investigating the WPF Assemblies
      4. Building a WPF Application without XAML
      5. Building a WPF Application using Only XAML
      6. Transforming Markup into a .NET Assembly
      7. Understanding The Syntax of WPF XAML
      8. Building a WPF Application using Code-Behind Files
      9. Building WPF Applications Using Visual Studio 2010
      10. Summary
    2. Programming with WPF Controls
      1. A Survey of the Core WPF Controls
      2. Controlling Content Layout Using Panels
      3. Building a Window's Frame Using Nested Panels
      4. Understanding WPF Control Commands
      5. Building a WPF User Interface with Expression Blend
      6. Building the Ink API Tab
      7. Introducing the Documents API
      8. Building the Documents Tab
      9. Introducing the WPF Data-Binding Model
      10. Summary
    3. WPF Graphics Rendering Services
      1. Understanding WPF's Graphical Rendering Services
      2. Rendering Graphical Data Using Shapes
      3. WPF Brushes and Pens
      4. Applying Graphical Transformations
      5. Working with Shapes using Expression Blend
      6. Rendering Graphical Data Using Drawings and Geometries
      7. Generating Complex Vector Graphics using Expression Design
      8. Rendering Graphical Data Using the Visual Layer
      9. Summary
    4. WPF Resources, Animations, and Styles
      1. Understanding the WPF Resource System
      2. Working with Object (Logical) Resources
      3. Understanding WPF's Animation Services
      4. Authoring Animations in XAML
      5. Understanding the Role of WPF Styles
      6. Generating Styles with Expression Blend
      7. Summary
    5. WPF Control Templates and UserControls
      1. Understanding the Role of Dependency Properties
      2. Building a Custom Dependency Property
      3. Understanding Routed Events
      4. Logical Trees, Visual Trees and Default Templates
      5. Building a Custom Control Template with Visual Studio 2010
      6. Building Custom UserControls with Blend
      7. Creating the Jackpot Deluxe WPF Application
      8. Summary
  13. Building Web Applications with ASP.NET
    1. Building ASP.NET Web Pages
      1. The Role of HTTP
      2. Understanding Web Applications and Web Servers
      3. The Role of HTML
      4. The Role of Client-Side Scripting
      5. Posting Back to the Web Server
      6. The Feature Set of the ASP.NET API
      7. Building a Single File ASP.NET Web Page
      8. Building an ASP.NET Web Page using Code Files
      9. ASP.NET Web Sites and ASP.NET Web Applications
      10. The ASP.NET Web Site Directory Structure
      11. The Inheritance Chain of the Page Type
      12. Interacting with the Incoming HTTP Request
      13. Interacting with the Outgoing HTTP Response
      14. The Life Cycle of an ASP.NET Web Page
      15. The Role of the Web.config File
      16. Summary
    2. ASP.NET Web Controls, Master Pages and Themes
      1. Understanding the Nature of Web Controls
      2. The Control and WebControl Base Classes
      3. Major Categories of ASP.NET Web Controls
      4. Building the ASP.NET Cars Web Site
      5. The Role of the Validation Controls
      6. Working with Themes
      7. Summary
    3. ASP.NET State Management Techniques
      1. The Issue of State
      2. ASP.NET State Management Techniques
      3. Understanding the Role of ASP.NET View State
      4. The Role of the Global.asax File
      5. Understanding the Application/Session Distinction
      6. Working with the Application Cache
      7. Maintaining Session Data
      8. Understanding Cookies
      9. The Role of the <sessionState> Element
      10. Understanding the ASP.NET Profile API
      11. Summary
  14. Programming with Windows Forms
    1. The Windows Forms Namespaces
    2. Building a Simple Windows Forms Application
    3. The Visual Studio Windows Forms Project Template
    4. The Anatomy of a Form
    5. Responding to Mouse and Keyboard Activity
    6. Designing Dialog Boxes
    7. Rendering Graphical Data Using GDI+
    8. Building a Complete Windows Forms Application
    9. Summary
  15. Platform-Independent .NET Development with Mono
    1. The Platform-Independent Nature of .NET
    2. Obtaining and Installing Mono
    3. The Mono Development Languages
    4. Microsoft-Compatible Mono Development Tools
    5. Building .NET Applications with Mono
    6. Who is Using Mono?
    7. Suggestions for Further Study
    8. Summary
  16. Index