You are previewing C# 2008 Programmer's Reference.
O'Reilly logo
C# 2008 Programmer's Reference

Book Description

Have access to all aspects of the C# programming language at hand with the C# 2008 Programmer's Reference, the only comprehensive reference guide for C# programmers. Regardless of your experience with C#, you will appreciate the detailed code samples, which offer a quick and easy way to help you refresh your understanding of important concepts. In this ideal C# resource, you will find an introduction to the .NET Framework, a guide to using Visual Studio 2008, and explanations of classes, objects, types, delegates and events, strings and regular expressions, and threading.

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Editor
  4. Credits
  5. Acknowledgments
  6. Introduction
    1. 0.1. Who This Book Is For
    2. 0.2. What This Book Covers
      1. 0.2.1. Part I: C# Fundamentals
      2. 0.2.2. Part II: Application Development Using C#
      3. 0.2.3. Part III: Appendixes
    3. 0.3. What You Need to Use This Book
    4. 0.4. Conventions
    5. 0.5. Source Code
    6. 0.6. Errata
    7. 0.7. p2p.wrox.com
  7. I. C# Fundamentals
    1. 1. The .NET Framework
      1. 1.1. What's the .NET Framework?
        1. 1.1.1. Common Language Runtime
        2. 1.1.2. .NET Framework Class Library
      2. 1.2. Assemblies and the Microsoft Intermediate Language (MSIL)
      3. 1.3. Versions of the .NET Framework and Visual Studio
      4. 1.4. Summary
    2. 2. Getting Started with Visual Studio 2008
      1. 2.1. Visual Studio 2008 Overview
        1. 2.1.1. Choosing the Development Settings
        2. 2.1.2. Resetting the Development Settings
        3. 2.1.3. Creating a New Project
        4. 2.1.4. Components of the IDE
          1. 2.1.4.1. Menu Bar
          2. 2.1.4.2. Toolbar
          3. 2.1.4.3. Toolbox
          4. 2.1.4.4. Solution Explorer
          5. 2.1.4.5. Properties
          6. 2.1.4.6. Error List
          7. 2.1.4.7. Output Window
          8. 2.1.4.8. Designer Window
          9. 2.1.4.9. Code View
      2. 2.2. Code and Text Editor
        1. 2.2.1. Code Snippets
        2. 2.2.2. IntelliSense
        3. 2.2.3. Refactoring Support
          1. 2.2.3.1. Rename
          2. 2.2.3.2. Extract Method
          3. 2.2.3.3. Reorder and Remove Parameters
          4. 2.2.3.4. Encapsulate Field
          5. 2.2.3.5. Extract Interface
          6. 2.2.3.6. Promote Local Variable to Parameter
      3. 2.3. Debugging
        1. 2.3.1. Setting Breakpoints
        2. 2.3.2. Stepping through the Code
        3. 2.3.3. Watching
        4. 2.3.4. Autos and Immediate Windows
      4. 2.4. Unit Testing
        1. 2.4.1. Creating the Test
        2. 2.4.2. Running the Test
        3. 2.4.3. Testing with Floating Point Numbers
        4. 2.4.4. Adding Additional Test Methods
      5. 2.5. Summary
    3. 3. C# Language Foundations
      1. 3.1. Using Visual Studio 2008
      2. 3.2. Using the C# Compiler (csc.exe)
      3. 3.3. Dissecting the Program
        1. 3.3.1. Passing Arguments to Main()
      4. 3.4. Language Syntax
        1. 3.4.1. Keywords
        2. 3.4.2. Variables
        3. 3.4.3. Scope of Variables
        4. 3.4.4. Constants
        5. 3.4.5. Comments
        6. 3.4.6. XML Documentation
      5. 3.5. Data Types
        1. 3.5.1. Value Types
          1. 3.5.1.1. Predefined Value Types
          2. 3.5.1.2. Nullable Type
        2. 3.5.2. Reference Types
        3. 3.5.3. Enumerations
        4. 3.5.4. Arrays
        5. 3.5.5. Implicit Typing
        6. 3.5.6. Type Conversion
      6. 3.6. Flow Control
        1. 3.6.1. if-else Statement
        2. 3.6.2. switch Statement
      7. 3.7. Looping
        1. 3.7.1. for Loop
        2. 3.7.2. Nested for Loop
        3. 3.7.3. foreach
        4. 3.7.4. while and do-while Loops
        5. 3.7.5. Exiting from a Loop
          1. 3.7.5.1. break
          2. 3.7.5.2. return
          3. 3.7.5.3. throw
          4. 3.7.5.4. goto
        6. 3.7.6. Skipping an Iteration
      8. 3.8. Operators
        1. 3.8.1. Assignment Operators
          1. 3.8.1.1. Self-Assignment Operators
          2. 3.8.1.2. Prefix and Postfix Operators
        2. 3.8.2. Relational Operators
        3. 3.8.3. Logical Operators
          1. 3.8.3.1. Short-Circuit Evaluation
        4. 3.8.4. Mathematical Operators
        5. 3.8.5. Operator Precedence
      9. 3.9. Preprocessor Directives
        1. 3.9.1. #define and #undef
        2. 3.9.2. #if, #else, #elif, and #endif
        3. 3.9.3. #warning and #error
        4. 3.9.4. #line
        5. 3.9.5. #region and #endregion
        6. 3.9.6. #pragma warning
      10. 3.10. Summary
    4. 4. Classes and Objects
      1. 4.1. Classes
        1. 4.1.1. Defining a Class
        2. 4.1.2. Using Partial Classes
        3. 4.1.3. Creating an Instance of a Class (Object Instantiation)
        4. 4.1.4. Anonymous Types (C# 3.0)
        5. 4.1.5. Class Members
          1. 4.1.5.1. Instance Members
          2. 4.1.5.2. Static Members
          3. 4.1.5.3. Access Modifiers
        6. 4.1.6. Function Members
          1. 4.1.6.1. Methods
            1. 4.1.6.1.1. Passing Arguments into Methods
            2. 4.1.6.1.2. The this Keyword
          2. 4.1.6.2. Properties
            1. 4.1.6.2.1. Read-Only and Write-Only Properties
            2. 4.1.6.2.2. Partial Methods (C# 3.0)
            3. 4.1.6.2.3. Automatic Properties (C# 3.0)
          3. 4.1.6.3. Constructors
            1. 4.1.6.3.1. Constructor Chaining
            2. 4.1.6.3.2. Static Constructors
            3. 4.1.6.3.3. Copy Constructor
          4. 4.1.6.4. Object Initializers (C# 3.0)
          5. 4.1.6.5. Destructors
        7. 4.1.7. Static Classes
      2. 4.2. System.Object Class
        1. 4.2.1. Testing for Equality
        2. 4.2.2. Implementing Equals
        3. 4.2.3. ToString() Method
        4. 4.2.4. Attributes
          1. 4.2.4.1. CLR Attributes
          2. 4.2.4.2. Custom Attributes
      3. 4.3. Structures
      4. 4.4. Summary
    5. 5. Interfaces
      1. 5.1. Defining an Interface
      2. 5.2. Implementing an Interface
      3. 5.3. Implementing Multiple Interfaces
      4. 5.4. Extending Interfaces
      5. 5.5. Interface Casting
      6. 5.6. The is and as Operators
      7. 5.7. Overriding Interface Implementations
      8. 5.8. Summary
    6. 6. Inheritance
      1. 6.1. Understanding Inheritance in C#
      2. 6.2. Implementation Inheritance
        1. 6.2.1. Abstract Class
        2. 6.2.2. Abstract Methods
        3. 6.2.3. Virtual Methods
        4. 6.2.4. Sealed Classes and Methods
        5. 6.2.5. Overloading Methods
        6. 6.2.6. Overloading Operators
        7. 6.2.7. Extension Methods (C# 3.0)
        8. 6.2.8. Access Modifiers
        9. 6.2.9. Inheritance and Constructors
        10. 6.2.10. Calling Base Class Constructors
      3. 6.3. Interface Inheritance
      4. 6.4. Explicit Interface Members Implementation
      5. 6.5. Summary
    7. 7. Delegates and Events
      1. 7.1. Delegates
        1. 7.1.1. Creating a Delegate
        2. 7.1.2. Delegates Chaining (Multicast Delegates)
        3. 7.1.3. Implementing Callbacks Using Delegates
        4. 7.1.4. Asynchronous Callbacks
        5. 7.1.5. Anonymous Methods and Lambda Expressions
      2. 7.2. Events
        1. 7.2.1. Handling Events
        2. 7.2.2. Implementing Events
        3. 7.2.3. Difference between Events and Delegates
        4. 7.2.4. Passing State Information to an Event Handler
      3. 7.3. Summary
    8. 8. Strings and Regular Expressions
      1. 8.1. The System.String Class
        1. 8.1.1. Escape Characters
        2. 8.1.2. String Manipulations
          1. 8.1.2.1. Testing for Equality
          2. 8.1.2.2. Comparing Strings
          3. 8.1.2.3. Creating and Concatenating Strings
          4. 8.1.2.4. Trimming Strings
          5. 8.1.2.5. Splitting Strings
          6. 8.1.2.6. Searching and Replacing Strings
          7. 8.1.2.7. Changing Case
        3. 8.1.3. String Formatting
        4. 8.1.4. The StringBuilder Class
      2. 8.2. Regular Expressions
        1. 8.2.1. Searching for a Match
        2. 8.2.2. More Complex Pattern Matching
      3. 8.3. Summary
    9. 9. Generics
      1. 9.1. Understanding Generics
        1. 9.1.1. Generic Classes
        2. 9.1.2. Using the default Keyword in Generics
        3. 9.1.3. Advantages of Generics
        4. 9.1.4. Using Constraints in a Generic Type
          1. 9.1.4.1. Specifying Multiple Constraints
          2. 9.1.4.2. Multiple Type Parameter
        5. 9.1.5. Generic Interfaces
        6. 9.1.6. Generic Structs
        7. 9.1.7. Generic Methods
        8. 9.1.8. Generic Operators
        9. 9.1.9. Generic Delegates
      2. 9.2. Generics and the .NET Framework Class Library
      3. 9.3. Using the LinkedList<T> Generic Class
      4. 9.4. System.Collections.ObjectModel
      5. 9.5. Summary
    10. 10. Threading
      1. 10.1. The Need for Multithreading
        1. 10.1.1. Starting a Thread
        2. 10.1.2. Aborting a Thread
        3. 10.1.3. Passing Parameters to Threads
      2. 10.2. Thread Synchronization
        1. 10.2.1. Using Interlocked Class
        2. 10.2.2. Using C# Lock
        3. 10.2.3. Monitor Class
      3. 10.3. Thread Safety in Windows Forms
        1. 10.3.1. Using the BackgroundWorker Control
        2. 10.3.2. Testing the Application
      4. 10.4. Summary
    11. 11. Files and Streams
      1. 11.1. Working with Files and Directories
        1. 11.1.1. Working with Directories
          1. 11.1.1.1. DirectoryInfo Class
          2. 11.1.1.2. Directory Class
        2. 11.1.2. Working with Files Using the File and FileInfo Classes
          1. 11.1.2.1. Reading and Writing to Files
          2. 11.1.2.2. StreamReader and StreamWriter Classes
          3. 11.1.2.3. BinaryReader and BinaryWriter Classes
        3. 11.1.3. Creating a FileExplorer
      2. 11.2. The Stream Class
        1. 11.2.1. BufferedStream
        2. 11.2.2. The FileStream Class
        3. 11.2.3. MemoryStream
        4. 11.2.4. NetworkStream Class
          1. 11.2.4.1. Building a Client-Server Application
          2. 11.2.4.2. Building a Multi-User Server Application
      3. 11.3. Cryptography
        1. 11.3.1. Hashing
        2. 11.3.2. Salted Hash
        3. 11.3.3. Encryption and Decryption
          1. 11.3.3.1. Symmetric Encryption
          2. 11.3.3.2. Asymmetric Encryption
      4. 11.4. Compressions for Stream Objects
        1. 11.4.1. Compression
        2. 11.4.2. Decompression
      5. 11.5. Serialization
        1. 11.5.1. Binary Serialization
        2. 11.5.2. XML Serialization
          1. 11.5.2.1. Defining a Sample Class
          2. 11.5.2.2. Serializing the Class
          3. 11.5.2.3. Deserializing the Class
          4. 11.5.2.4. Customizing the Serialization Process
          5. 11.5.2.5. XML Serialization Needs a Default Constructor
          6. 11.5.2.6. Uses of XML Serialization
      6. 11.6. Summary
    12. 12. Exception Handling
      1. 12.1. Handling Exceptions
        1. 12.1.1. Handling Exceptions Using the try-catch Statement
        2. 12.1.2. Handling Multiple Exceptions
        3. 12.1.3. Throwing Exceptions Using the throw Statement
        4. 12.1.4. Rethrowing Exceptions
        5. 12.1.5. Exception Chaining
        6. 12.1.6. Using Exception Objects
        7. 12.1.7. The finally Statement
      2. 12.2. Creating Custom Exceptions
      3. 12.3. Summary
    13. 13. Arrays and Collections
      1. 13.1. Arrays
        1. 13.1.1. Accessing Array Elements
        2. 13.1.2. Multidimensional Arrays
        3. 13.1.3. Arrays of Arrays: Jagged Arrays
        4. 13.1.4. Parameter Arrays
        5. 13.1.5. Copying Arrays
      2. 13.2. Collections Interfaces
        1. 13.2.1. Dynamic Arrays Using the ArrayList Class
        2. 13.2.2. Indexers and Iterators
        3. 13.2.3. Implementing IEnumerable<T> and IEnumerator<T>
        4. 13.2.4. Implementing Comparison Using IComparer<T> and IComparable<T>
        5. 13.2.5. Dictionary
        6. 13.2.6. Stacks
        7. 13.2.7. Queues
      3. 13.3. Summary
    14. 14. Language Integrated Query (LINQ)
      1. 14.1. LINQ Architecture
      2. 14.2. LINQ to Objects
        1. 14.2.1. Query Syntax versus Method Syntax and Lambda Expressions
        2. 14.2.2. LINQ and Extension Methods
          1. 14.2.2.1. Deferred Query Execution
          2. 14.2.2.2. Forced Immediate Query Execution
        3. 14.2.3. LINQ and Anonymous Types
      3. 14.3. LINQ to DataSet
        1. 14.3.1. Reshaping Data
        2. 14.3.2. Aggregate Functions
        3. 14.3.3. Joining Tables
        4. 14.3.4. Typed DataSet
        5. 14.3.5. Detecting Null Fields
        6. 14.3.6. Saving the Result of a Query to a DataTable
      4. 14.4. LINQ to XML
        1. 14.4.1. Creating XML Trees
        2. 14.4.2. Querying Elements
        3. 14.4.3. An Example Using RSS
          1. 14.4.3.1. Query Elements with a Namespace
          2. 14.4.3.2. Retrieving Postings in the Last 10 Days
      5. 14.5. LINQ to SQL
        1. 14.5.1. Using the Object Relational Designer
        2. 14.5.2. Querying
        3. 14.5.3. Inserting New Rows
        4. 14.5.4. Updating Rows
        5. 14.5.5. Deleting Rows
      6. 14.6. Summary
    15. 15. Assemblies and Versioning
      1. 15.1. Assemblies
        1. 15.1.1. Structure of an Assembly
        2. 15.1.2. Examining the Content of an Assembly
        3. 15.1.3. Single and Multi-File Assemblies
          1. 15.1.3.1. Updating the DLL
          2. 15.1.3.2. Modules and Assemblies
        4. 15.1.4. Understanding Namespaces and Assemblies
      2. 15.2. Private versus Shared Assemblies
        1. 15.2.1. Creating a Shared Assembly
          1. 15.2.1.1. Creating a Strong Name
          2. 15.2.1.2. Versioning
          3. 15.2.1.3. Building the Assembly
        2. 15.2.2. The Global Assembly Cache
        3. 15.2.3. Putting the Shared Assembly into GAC
        4. 15.2.4. Making the Shared Assembly Visible in Visual Studio
        5. 15.2.5. Using the Shared Assembly
      3. 15.3. Summary
  8. II. Application Development Using C#
    1. 16. Developing Windows Applications
      1. 16.1. The Project
        1. 16.1.1. Configuring the FTP Server
        2. 16.1.2. Creating the Application
        3. 16.1.3. Using Application Settings
        4. 16.1.4. Coding the Application
        5. 16.1.5. Building the Directory Tree and Displaying Images
        6. 16.1.6. Creating a New Directory
        7. 16.1.7. Removing a Directory
        8. 16.1.8. Uploading Photos
        9. 16.1.9. Deleting a Photo
        10. 16.1.10. Testing the Application
      2. 16.2. Adding Print Capability
        1. 16.2.1. Basics of Printing in .NET
        2. 16.2.2. Adding Print Support to the Project
      3. 16.3. Deploying the Application
        1. 16.3.1. Publishing the Application Using ClickOnce
        2. 16.3.2. Updating the Application
        3. 16.3.3. Programmatically Updating the Application
        4. 16.3.4. Rolling Back
        5. 16.3.5. Under the Hood: Application and Deployment Manifests
          1. 16.3.5.1. Application Manifest
          2. 16.3.5.2. Deployment Manifest
          3. 16.3.5.3. Where Are the Files Installed Locally?
      4. 16.4. Summary
    2. 17. Developing ASP.NET Web Applications
      1. 17.1. About ASP.NET
        1. 17.1.1. How ASP.NET Works
        2. 17.1.2. What Do You Need to Run ASP.NET?
      2. 17.2. Data Binding
        1. 17.2.1. Modeling Databases Using LINQ to SQL
        2. 17.2.2. Data Binding Using the GridView Control
        3. 17.2.3. Displaying Publisher's Name
        4. 17.2.4. Displaying Titles from a Selected Publisher
        5. 17.2.5. Making the Publisher Field Editable
      3. 17.3. Building Responsive Applications Using AJAX
        1. 17.3.1. AJAX Control Toolkit
        2. 17.3.2. AJAX-Enabling a Page Using the ScriptManager Control
        3. 17.3.3. Using the UpdatePanel Control
        4. 17.3.4. Using Triggers to Cause an Update
        5. 17.3.5. Displaying Progress Using the UpdateProgress Control
        6. 17.3.6. Displaying a Modal Dialog Using the ModalPopupExtender Control
      4. 17.4. Summary
    3. 18. Developing Windows Mobile Applications
      1. 18.1. The Windows Mobile Platform
      2. 18.2. Developing Windows Mobile Applications Using the .NET Compact Framework
      3. 18.3. Obtaining the Appropriate SDKs and Tools
      4. 18.4. Building the RSS Reader Application
        1. 18.4.1. Building the User Interface
        2. 18.4.2. Creating the Helper Methods
        3. 18.4.3. Wiring All the Event Handlers
        4. 18.4.4. Testing Using Emulators
        5. 18.4.5. Testing Using Real Devices
      5. 18.5. Deploying the Application
        1. 18.5.1. Creating a CAB File
        2. 18.5.2. Creating a Setup Application
          1. 18.5.2.1. Creating the Custom Installer
          2. 18.5.2.2. Creating a MSI File
          3. 18.5.2.3. Testing the Setup
          4. 18.5.2.4. Installing the Prerequisites — .NET Compact Framework 3.5
      6. 18.6. Summary
    4. 19. Developing Silverlight Applications
      1. 19.1. The State of Silverlight
        1. 19.1.1. Obtaining the Tools
        2. 19.1.2. Architecture of Silverlight
      2. 19.2. Building a Silverlight UI Using XAML
        1. 19.2.1. Creating a Bare-Bones Silverlight Application
        2. 19.2.2. Understanding XAML
          1. 19.2.2.1. The Canvas Control
          2. 19.2.2.2. Drawing Shapes
            1. 19.2.2.2.1. Rectangle
            2. 19.2.2.2.2. Line
            3. 19.2.2.2.3. Ellipse
            4. 19.2.2.2.4. Polygon
            5. 19.2.2.2.5. Polyline
          3. 19.2.2.3. Painting Shapes
            1. 19.2.2.3.1. Using SolidColorBrush
            2. 19.2.2.3.2. Using LinearGradientBrush
            3. 19.2.2.3.3. Using RadialGradientBrush
            4. 19.2.2.3.4. Using ImageBrush
        3. 19.2.3. Crafting XAML Using Expression Blend 2
          1. 19.2.3.1. Using Expression Blend 2
          2. 19.2.3.2. Scripting the UI Using JavaScript
      3. 19.3. Silverlight 1.0
        1. 19.3.1. Animation — Part 1
        2. 19.3.2. Animations — Part 2
          1. 19.3.2.1. Slowing the Rate of Fall
          2. 19.3.2.2. Varying the Rate of Fall
        3. 19.3.3. Playing Media
          1. 19.3.3.1. Creating the Silverlight Project
          2. 19.3.3.2. Disabling Auto-Play
          3. 19.3.3.3. Creating the Mirror Effect
        4. 19.3.4. Creating Your Own Media Player
          1. 19.3.4.1. Creating the Project
          2. 19.3.4.2. Designing the User Interface
          3. 19.3.4.3. Wiring All the Controls
          4. 19.3.4.4. Creating the Helper Functions
          5. 19.3.4.5. Defining the Event Handlers
      4. 19.4. Silverlight 2.0
        1. 19.4.1. Creating the Project Using Visual Studio 2008
          1. 19.4.1.1. Capturing the Signature
          2. 19.4.1.2. Coding the Application
          3. 19.4.1.3. Saving the Signature to Isolated Storage
          4. 19.4.1.4. Saving the Signature to Web Services
      5. 19.5. Summary
    5. 20. Windows Communication Foundation
      1. 20.1. What Is WCF?
        1. 20.1.1. Comparing WCF with ASMX Web Services
        2. 20.1.2. Your First WCF Service
        3. 20.1.3. Consuming the WCF Service
      2. 20.2. Understanding How WCF Works
        1. 20.2.1. WCF Communication Protocols
        2. 20.2.2. The ABCs of WCF
          1. 20.2.2.1. Addresses and Endpoints
          2. 20.2.2.2. Bindings
          3. 20.2.2.3. Contracts
        3. 20.2.3. Messaging Patterns
        4. 20.2.4. Hosting Web Services
      3. 20.3. Building WCF Services
        1. 20.3.1. Exposing Multiple Endpoints
          1. 20.3.1.1. Creating the WCF Service
          2. 20.3.1.2. Creating the Client
        2. 20.3.2. Creating Self-Hosted WCF Service
          1. 20.3.2.1. Creating the WCF Service
          2. 20.3.2.2. Creating the Client
        3. 20.3.3. Implementing WCF Callbacks
          1. 20.3.3.1. Building the Service
          2. 20.3.3.2. Building the Client
          3. 20.3.3.3. Testing the Application
        4. 20.3.4. Calling WCF Services from an AJAX Page
      4. 20.4. Summary
  9. III. Appendixes
    1. A. C# Keywords
      1. A.1. C# Reserved Keywords
      2. A.2. Contextual Keywords
    2. B. Examining the .Net Class Libraries Using the Object Browser
      1. B.1. Versions of the .NET Framework
      2. B.2. .NET Framework 2.0
      3. B.3. .NET Framework 3.0
      4. B.4. .NET Framework 3.5
      5. B.5. Using the Object Browser
    3. C. Generating Documentation for Your C# Applications
      1. C.1. Inline Documentation using XML
      2. C.2. Generating the Documentation
        1. C.2.1. Downloading and Installing Sandcastle
        2. C.2.2. Launching Sandcastle
        3. C.2.3. Building and Viewing the Documentation
        4. C.2.4. Distributing the Documentation