You are previewing Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition.
O'Reilly logo
Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition

Book Description

The first edition of this book was released at the 2001 Tech Ed conference in Atlanta, Georgia. Since that time, this text has been revised, tweaked, and enhanced to account for the changes found within each release of the .NET platform (1.1, 2.0, 3.0 and now 3.5).

The last version, .NET 3.0, was more of an augmentative release, essentially providing three new APIs: Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF). As you would expect, coverage of the "W's" has been expanded a great deal in this version of the book from the previous Special Edition text.

Unlike .NET 3.0, .NET 3.5 provides dozens of new C# language features and .NET APIs. This edition of the book will walk you through all of this new material using the same readable approach as was found in previous editions. Rest assured, you'll find detailed coverage of Language Integrated Query (LINQ), the C# 2008 language changes (automatic properties, extension methods, anonymous types, etc.) and the numerous bells and whistles of Visual Studio 2008

Table of Contents

  1. About the Author
  2. About the Technical Reviewer
  3. Acknowledgments
  4. Introduction
    1. We're a Team, You and I
    2. An Overview of This Book
    3. Diving Even Deeper with Five Free Chapters
    4. Obtaining This Book's Source Code
    5. Obtaining Updates for This Book
    6. Contacting Me
  5. I. Introducing C# and the .NET Platform
    1. 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 Lutz Roeder's Reflector
      12. Deploying the .NET Runtime
      13. The Platform-Independent Nature of .NET
      14. Summary
    2. 2. Building C# Applications
      1. The Role of the .NET Framework 3.5 SDK
      2. Building C# Applications Using csc.exe
      3. Building .NET Applications Using TextPad
      4. Building .NET Applications Using Notepad++
      5. Building .NET Applications Using SharpDevelop
      6. Building .NET Applications Using Visual C# 2008 Express
      7. Building .NET Applications Using Visual Studio 2008
      8. A Partial Catalog of Additional .NET Development Tools
      9. Summary
  6. II. Core C# Programming Constructs
    1. 3. 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. Understanding the System.String Type
      6. Narrowing and Widening Data Type Conversions
      7. C# Iteration Constructs
      8. Decision Constructs and the Relational/Equality Operators
      9. Summary
    2. 4. Core C# Programming Constructs, Part II
      1. Methods and Parameter Modifiers
      2. Understanding Member Overloading
      3. Array Manipulation in C#
      4. Understanding the Enum Type
      5. Understanding the Structure Type
      6. Understanding Value Types and Reference Types
      7. Value and Reference Types: Final Details
      8. Understanding C# Nullable Types
      9. Summary
    3. 5. Defining Encapsulated Class Types
      1. Introducing the C# Class Type
      2. Understanding Class 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 Constant Data
      9. Understanding Read-Only Fields
      10. Understanding Partial Types
      11. Documenting C# Source Code via XML
      12. Visualizing the Fruits of Our Labor
      13. Summary
    4. 6. Understanding Inheritance and Polymorphism
      1. The Basic Mechanics of Inheritance
      2. Revising Visual Studio Class Diagrams
      3. The Second Pillar: The Details of Inheritance
      4. Programming for Containment/Delegation
      5. The Third Pillar: C#'s Polymorphic Support
      6. Understanding Base Class/Derived Class Casting Rules
      7. The Master Parent Class: System.Object
      8. Summary
    5. 7. 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. The Finally Block
      9. Who Is Throwing What?
      10. The Result of Unhandled Exceptions
      11. Debugging Unhandled Exceptions Using Visual Studio
      12. Summary
    6. 8. 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. The System.GC Type
      6. Building Finalizable Objects
      7. Building Disposable Objects
      8. Building Finalizable and Disposable Types
      9. Summary
  7. III. Advanced C# Programming Constructs
    1. 9. 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 2008
      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. Understanding Callback Interfaces
      15. Summary
    2. 10. Collections and Generics
      1. The Interfaces of the System.Collections Namespace
      2. The Class Types of System.Collections
      3. System.Collections.Specialized Namespace
      4. The Boxing, Unboxing, and System.Object Relationship
      5. The Issue of Type Safety and Strongly Typed Collections
      6. The System.Collections.Generic Namespace
      7. Creating Custom Generic Methods
      8. Creating Generic Structures and Classes
      9. Creating a Custom Generic Collection
      10. Creating Generic Base Classes
      11. Creating Generic Interfaces
      12. Summary
    3. 11. Delegates, Events, and Lambdas
      1. Understanding the .NET Delegate Type
      2. Defining a Delegate in C#
      3. The System.MulticastDelegate and System.Delegate Base Classes
      4. The Simplest Possible Delegate Example
      5. Retrofitting the Car Type with Delegates
      6. A More Elaborate Delegate Example
      7. Understanding Delegate Covariance
      8. Creating Generic Delegates
      9. Understanding C# Events
      10. The Generic EventHandler<T> Delegate
      11. Understanding C# Anonymous Methods
      12. Understanding Method Group Conversions
      13. The C# 2008 Lambda Operator
      14. Summary
    4. 12. Indexers, Operators, and Pointers
      1. Understanding Indexer Methods
      2. Understanding Operator Overloading
      3. Understanding Custom Type Conversions
      4. Working with Pointer Types
      5. C# Preprocessor Directives
      6. Summary
    5. 13. C# 2008 Language Features
      1. Understanding Implicitly Typed Local Variables
      2. Understanding Automatic Properties
      3. Understanding Extension Methods
      4. Understanding Partial Methods
      5. Understanding Object Initializer Syntax
      6. Understanding Anonymous Types
      7. Summary
    6. 14. An Introduction to LINQ
      1. Understanding the Role of LINQ
      2. A First Look at LINQ Query Expressions
      3. LINQ and Generic Collections
      4. LINQ and Nongeneric Collections
      5. The Internal Representation of LINQ Query Operators
      6. Investigating the C# LINQ Query Operators
      7. LINQ Queries: An Island unto Themselves?
      8. Summary
  8. IV. Programming with .NET Assemblies
    1. 15. Introducing .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. Investigating the Internal Composition of the GAC
      11. Understanding Publisher Policy Assemblies
      12. Understanding the <codeBase> Element
      13. The System.Configuration Namespace
      14. The Machine Configuration File
      15. Summary
    2. 16. 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 Attributed Programming
      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. 17. Processes, AppDomains, and Object Contexts
      1. Reviewing Traditional Win32 Processes
      2. Interacting with Processes Under the .NET Platform
      3. Understanding .NET Application Domains
      4. Understanding Object Context Boundaries
      5. Summarizing Processes, AppDomains, and Context
      6. Summary
    4. 18. Building Multithreaded Applications
      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. The Role of the BackgroundWorker Component
      12. Summary
    5. 19. Understanding CIL and the Role of Dynamic Assemblies
      1. Reflecting on the Nature of CIL Programming
      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
  9. V. Introducing the .NET Base Class Libraries
    1. 20. File I/O and Isolated Storage
      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. Programmatically "Watching" Files
      13. Performing Asynchronous File I/O
      14. Understanding the Role of Isolated Storage
      15. A Primer on Code Access Security
      16. An Overview of Isolated Storage
      17. Obtaining a Store Using IsolatedStorageFile
      18. Isolated Storage in Action: ClickOnce Deployment
      19. Summary
    2. 21. Introducing Object Serialization
      1. Understanding Object Serialization
      2. Configuring Objects for Serialization
      3. Choosing a Serialization Formatter
      4. Serializing Objects Using the BinaryFormatter
      5. Serializing Objects Using the SoapFormatter
      6. Serializing Objects Using the XmlSerializer
      7. Serializing Collections of Objects
      8. Customizing the Serialization Process
      9. Summary
    3. 22. 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 UIBased Front End
      12. Asynchronous Data Access Using SqlCommand
      13. Understanding Database Transactions
      14. Summary
    4. 23. 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 User Interfaces
      7. Filling DataSet/DataTable Objects Using Data Adapters
      8. Revisiting AutoLotDAL.dll
      9. Navigating Multitabled DataSet Objects
      10. The Data Access Tools of Visual Studio 2008
      11. Decoupling Autogenerated Code from the UI Layer
      12. Summary
    5. 24. Programming with the LINQ APIs
      1. The Role of LINQ to ADO.NET
      2. Programming with LINQ to DataSet
      3. Programming with LINQ to SQL
      4. Generating Entity Classes Using SqlMetal.exe
      5. Building Entity Classes Using Visual Studio 2008
      6. Manipulating XML Documents Using LINQ to XML
      7. Navigating an In-Memory Document
      8. Summary
    6. 25. 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. Hosting the WCF Service
      9. Building the WCF Client Application
      10. Using the WCF Service Library Project Template
      11. Hosting the WCF Service As a Windows Service
      12. Invoking a Service Asynchronously
      13. Designing WCF Data Contracts
      14. Summary
    7. 26. Introducing Windows Workflow Foundation
      1. Defining a Business Process
      2. The Building Blocks of WF
      3. WF Assemblies, Namespaces, and Projects
      4. Building a Simple Workflow-Enabled Application
      5. Examining the WF Engine Hosting Code
      6. Invoking Web Services Within Workflows
      7. Building a Reusable WF Code Library
      8. A Brief Word Regarding Custom Activities
      9. Summary
  10. VI. Desktop User Interfaces
    1. 27. Programming with Windows Forms
      1. The Windows Forms Namespaces
      2. Building a Simple Windows Forms Application (IDE-Free)
      3. The Visual Studio Windows Forms Project Template
      4. The Anatomy of a Form
      5. Responding to Mouse Activity
      6. Responding to Keyboard Activity
      7. Designing Dialog Boxes
      8. Rendering Graphical Data Using GDI+
      9. Building a Complete Windows Forms Application
      10. Summary
    2. 28. Introducing Windows Presentation Foundation and XAML
      1. The Motivation Behind WPF
      2. The Various Flavors of WPF Applications
      3. Investigating the WPF Assemblies
      4. Building a (XAML-Free) WPF Application
      5. Additional Details of the Application Type
      6. Additional Details of the Window Type
      7. Building a (XAML-Centric) WPF Application
      8. Transforming Markup into a .NET Assembly
      9. Separation of Concerns Using Code-Behind Files
      10. The Syntax of XAML
      11. Building WPF Applications Using Visual Studio 2008
      12. Processing XAML at Runtime: SimpleXamlPad.exe
      13. The Role of Microsoft Expression Blend
      14. Summary
    3. 29. Programming with WPF Controls
      1. A Survey of the WPF Control Library
      2. Declaring Controls in XAML
      3. Understanding the Role of Dependency Properties
      4. Understanding Routed Events
      5. Working with Button Types
      6. Working with CheckBoxes and RadioButtons
      7. Working with the ListBox and ComboBox Types
      8. Working with Text Areas
      9. Controlling Content Layout Using Panels
      10. Building a Window's Frame Using Nested Panels
      11. Understanding WPF Control Commands
      12. Understanding the WPF Data-Binding Model
      13. Data Conversion Using IValueConverter
      14. Binding to Custom Objects
      15. Binding UI Elements to XML Documents
      16. Summary
    4. 30. WPF 2D Graphical Rendering, Resources, and Themes
      1. The Philosophy of WPF Graphical Rendering Services
      2. Exploring the Shape-Derived Types
      3. Working with WPF Brushes
      4. Working with WPF Pens
      5. Exploring the Drawing-Derived Types
      6. The Role of UI Transformations
      7. Understanding WPF's Animation Services
      8. Understanding the WPF Resource System
      9. Defining and Applying Styles for WPF Controls
      10. Altering a Control's UI Using Templates
      11. Summary
  11. VII. Building Web Applications with ASP.NET
    1. 31. 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. Submitting the Form Data (GET and POST)
      6. Building a Classic ASP Page
      7. Problems with Classic ASP
      8. The ASP.NET Namespaces
      9. The ASP.NET Web Page Code Model
      10. Details of an ASP.NET Website Directory Structure
      11. The ASP.NET Page Compilation Cycle
      12. The Inheritance Chain of the Page Type
      13. Interacting with the Incoming HTTP Request
      14. Interacting with the Outgoing HTTP Response
      15. The Life Cycle of an ASP.NET Web Page
      16. The Role of the Web.config File
      17. Summary
    2. 32. ASP.NET Web Controls, Themes, and Master Pages
      1. Understanding the Nature of Web Controls
      2. The System.Web.UI.Control Type
      3. The System.Web.UI.WebControls.WebControl Type
      4. Major Categories of ASP.NET Web Controls
      5. Building a Feature-Rich ASP.NET Website
      6. The Role of the Validation Controls
      7. Working with Themes
      8. Positioning Controls Using HTML Tables
      9. Summary
    3. 33. 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
  12. VIII. Appendixes
    1. A. COM and .NET Interoperability
      1. The Scope of .NET Interoperability
      2. A Simple Example of .NET to COM Interop
      3. Investigating a .NET Interop Assembly
      4. Understanding the Runtime Callable Wrapper
      5. The Role of COM IDL
      6. Using a Type Library to Build an Interop Assembly
      7. Building a More Elaborate COM Server
      8. Examining the Interop Assembly
      9. Understanding COM to .NET Interoperability
      10. The Role of the CCW
      11. The Role of the .NET Class Interface
      12. Building Your .NET Types
      13. Generating the Type Library and Registering the .NET Types
      14. Examining the Exported Type Information
      15. Building a Visual Basic 6.0 Test Client
      16. Summary
    2. B. Platform-Independent .NET Development with Mono
      1. The Platform-Independent Nature of .NET
      2. Obtaining and Installing Mono
      3. The Mono Development Tools
      4. Building .NET Applications with Mono
      5. Suggestions for Further Study
      6. Summary