Cover image for Programming C#, 4th Edition

Book description

The programming language C# was built with the future of application development in mind. Pursuing that vision, C#'s designers succeeded in creating a safe, simple, component-based, high-performance language that works effectively with Microsoft's .NET Framework. Now the favored language among those programming for the Microsoft platform, C# continues to grow in popularity as more developers discover its strength and flexibility. And, from the start, C# developers have relied on Programming C# both as an introduction to the language and a means of further building their skills. The fourth edition of Programming C#--the top-selling C# book on the market--has been updated to the C# ISO standard as well as changes to Microsoft's implementation of the language. It also provides notes and warnings on C# 1.1 and C# 2.0. Aimed at experienced programmers and web developers, Programming C#, 4th Edition, doesn't waste too much time on the basics. Rather, it focuses on the features and programming patterns unique to the C# language. New C# 2005 features covered in-depth include:

  • Visual Studio 2005

  • Generics

  • Collection interfaces and iterators

  • Anonymous methods

  • New ADO.NET data controls

  • Fundamentals of Object-Oriented Programming

Author Jesse Liberty, an acclaimed web programming expert and entrepreneur, teaches C# in a way that experienced programmers will appreciate by grounding its applications firmly in the context of Microsoft's .NET platform and the development of desktop and Internet applications.

Liberty also incorporates reader suggestions from previous editions to help create the most consumer-friendly guide possible.

Table of Contents

  1. Programming C#, 4th Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. About This Book
    2. What You Need To Use This Book
    3. How the Book Is Organized
      1. Part I, The C# Language
      2. Part II, Programming with C#
      3. Part III, The CLR and the .NET Framework
    4. Who This Book Is For
    5. C# 2.0 Versus C# 1.1
    6. C# Versus Visual Basic .NET
    7. C# Versus Java
    8. C# Versus C and C++
    9. Conventions Used in This Book
    10. Support
    11. We'd Like to Hear from You
    12. Safari Enabled
    13. Acknowledgments
  4. I. The C# Language
    1. 1. C# and the .NET Framework
      1. 1.1. The .NET Platform
      2. 1.2. The .NET Framework
      3. 1.3. Compilation and the MSIL
      4. 1.4. The C# Language
    2. 2. Getting Started: "Hello World"
      1. 2.1. Classes, Objects, and Types
        1. 2.1.1. Methods
        2. 2.1.2. Comments
        3. 2.1.3. Console Applications
        4. 2.1.4. Namespaces
        5. 2.1.5. The Dot Operator (.)
        6. 2.1.6. The using Keyword
        7. 2.1.7. Case Sensitivity
        8. 2.1.8. The static Keyword
      2. 2.2. Developing "Hello World"
        1. 2.2.1. Editing "Hello World"
        2. 2.2.2. Compiling and Running "Hello World"
      3. 2.3. Using the Visual Studio .NET Debugger
    3. 3. C# Language Fundamentals
      1. 3.1. Types
        1. 3.1.1. Working with Built-in Types
          1. 3.1.1.1. Choosing a built-in type
          2. 3.1.1.2. Converting built-in types
      2. 3.2. Variables and Constants
        1. 3.2.1. Definite Assignment
        2. 3.2.2. Constants
        3. 3.2.3. Enumerations
        4. 3.2.4. Strings
        5. 3.2.5. Identifiers
      3. 3.3. Expressions
      4. 3.4. Whitespace
      5. 3.5. Statements
        1. 3.5.1. Unconditional Branching Statements
        2. 3.5.2. Conditional Branching Statements
          1. 3.5.2.1. if...else statements
          2. 3.5.2.2. Nested if statements
          3. 3.5.2.3. switch statements: an alternative to nested ifs
          4. 3.5.2.4. Switch on string statements
        3. 3.5.3. Iteration Statements
          1. 3.5.3.1. The goto statement
          2. 3.5.3.2. The while loop
          3. 3.5.3.3. The do...while loop
          4. 3.5.3.4. The for loop
          5. 3.5.3.5. The foreach statement
          6. 3.5.3.6. The continue and break statements
      6. 3.6. Operators
        1. 3.6.1. The Assignment Operator (=)
        2. 3.6.2. Mathematical Operators
          1. 3.6.2.1. Simple arithmetical operators (+, -, *, /)
          2. 3.6.2.2. The modulus operator (%) to return remainders
        3. 3.6.3. Increment and Decrement Operators
          1. 3.6.3.1. Calculate and reassign operators
          2. 3.6.3.2. The prefix and postfix operators
        4. 3.6.4. Relational Operators
        5. 3.6.5. Use of Logical Operators with Conditionals
        6. 3.6.6. Operator Precedence
        7. 3.6.7. The Ternary Operator
      7. 3.7. Preprocessor Directives
        1. 3.7.1. Defining Identifiers
        2. 3.7.2. Undefining Identifiers
        3. 3.7.3. #if, #elif, #else, and #endif
        4. 3.7.4. #region
    4. 4. Classes and Objects
      1. 4.1. Defining Classes
        1. 4.1.1. Access Modifiers
        2. 4.1.2. Method Arguments
      2. 4.2. Creating Objects
        1. 4.2.1. Constructors
        2. 4.2.2. Initializers
        3. 4.2.3. The ICloneable Interface
        4. 4.2.4. The this Keyword
      3. 4.3. Using Static Members
        1. 4.3.1. Invoking Static Methods
        2. 4.3.2. Using Static Constructors
        3. 4.3.3. Static Classes
        4. 4.3.4. Using Static Fields
      4. 4.4. Destroying Objects
        1. 4.4.1. The C# Destructor
        2. 4.4.2. Destructors Versus Dispose
        3. 4.4.3. Implementing the Close( ) Method
        4. 4.4.4. The using Statement
      5. 4.5. Passing Parameters
        1. 4.5.1. Passing by Reference
        2. 4.5.2. Overcoming Definite Assignment with out Parameters
      6. 4.6. Overloading Methods and Constructors
      7. 4.7. Encapsulating Data with Properties
        1. 4.7.1. The get Accessor
        2. 4.7.2. The set Accessor
        3. 4.7.3. Property Access Modifiers
      8. 4.8. readonly Fields
    5. 5. Inheritance and Polymorphism
      1. 5.1. Specialization and Generalization
      2. 5.2. Inheritance
        1. 5.2.1. Implementing Inheritance
      3. 5.3. Polymorphism
        1. 5.3.1. Creating Polymorphic Types
        2. 5.3.2. Creating Polymorphic Methods
        3. 5.3.3. Calling Base Class Constructors
        4. 5.3.4. Controlling Access
        5. 5.3.5. Versioning with the new and override Keywords
      4. 5.4. Abstract Classes
        1. 5.4.1. Limitations of Abstract
        2. 5.4.2. Sealed Class
      5. 5.5. The Root of All Classes: Object
      6. 5.6. Boxing and Unboxing Types
        1. 5.6.1. Boxing Is Implicit
        2. 5.6.2. Unboxing Must Be Explicit
      7. 5.7. Nesting Classes
    6. 6. Operator Overloading
      1. 6.1. Using the operator Keyword
      2. 6.2. Supporting Other .NET Languages
      3. 6.3. Creating Useful Operators
      4. 6.4. Logical Pairs
      5. 6.5. The Equality Operator
      6. 6.6. Conversion Operators
    7. 7. Structs
      1. 7.1. Defining Structs
      2. 7.2. Creating Structs
        1. 7.2.1. Structs as Value Types
        2. 7.2.2. Creating Structs Without new
    8. 8. Interfaces
      1. 8.1. Defining and Implementing an Interface
        1. 8.1.1. Implementing More Than One Interface
        2. 8.1.2. Extending Interfaces
        3. 8.1.3. Combining Interfaces
          1. 8.1.3.1. Casting to extended interfaces
      2. 8.2. Accessing Interface Methods
        1. 8.2.1. Casting to an Interface
        2. 8.2.2. The is Operator
        3. 8.2.3. The as Operator
        4. 8.2.4. The is Operator Versus the as Operator
        5. 8.2.5. Interface Versus Abstract Class
      3. 8.3. Overriding Interface Implementations
      4. 8.4. Explicit Interface Implementation
        1. 8.4.1. Selectively Exposing Interface Methods
        2. 8.4.2. Member Hiding
        3. 8.4.3. Accessing Sealed Classes and Value Types
    9. 9. Arrays, Indexers, and Collections
      1. 9.1. Arrays
        1. 9.1.1. Declaring Arrays
        2. 9.1.2. Understanding Default Values
        3. 9.1.3. Accessing Array Elements
      2. 9.2. The foreach Statement
        1. 9.2.1. Initializing Array Elements
        2. 9.2.2. The params Keyword
        3. 9.2.3. Multidimensional Arrays
          1. 9.2.3.1. Rectangular arrays
          2. 9.2.3.2. Jagged arrays
        4. 9.2.4. Array Bounds
        5. 9.2.5. Array Conversions
        6. 9.2.6. Sorting Arrays
      3. 9.3. Indexers
        1. 9.3.1. Indexers and Assignment
        2. 9.3.2. Indexing on Other Values
      4. 9.4. Collection Interfaces
        1. 9.4.1. The IEnumerable<T> Interface
      5. 9.5. Constraints
      6. 9.6. List<T>
        1. 9.6.1. Implementing IComparable
        2. 9.6.2. Implementing IComparer
      7. 9.7. Queues
      8. 9.8. Stacks
      9. 9.9. Dictionaries
        1. 9.9.1. IDictionary<K,V>
    10. 10. Strings and Regular Expressions
      1. 10.1. Strings
        1. 10.1.1. Creating Strings
        2. 10.1.2. The ToString() Method
        3. 10.1.3. Manipulating Strings
        4. 10.1.4. Finding Substrings
        5. 10.1.5. Splitting Strings
        6. 10.1.6. Manipulating Dynamic Strings
      2. 10.2. Regular Expressions
        1. 10.2.1. Using Regular Expressions: Regex
        2. 10.2.2. Using Regex Match Collections
        3. 10.2.3. Using Regex Groups
        4. 10.2.4. Using CaptureCollection
    11. 11. Handling Exceptions
      1. 11.1. Throwing and Catching Exceptions
        1. 11.1.1. The throw Statement
        2. 11.1.2. The catch Statement
          1. 11.1.2.1. Taking corrective action
          2. 11.1.2.2. Unwinding the call stack
          3. 11.1.2.3. Creating dedicated catch statements
        3. 11.1.3. The finally Statement
      2. 11.2. Exception Objects
      3. 11.3. Custom Exceptions
      4. 11.4. Rethrowing Exceptions
    12. 12. Delegates and Events
      1. 12.1. Delegates
        1. 12.1.1. Using Delegates to Specify Methods at Runtime
        2. 12.1.2. Delegates and Instance Methods
        3. 12.1.3. Static Delegates
        4. 12.1.4. Delegates as Properties
      2. 12.2. Multicasting
      3. 12.3. Events
        1. 12.3.1. Publishing and Subscribing
        2. 12.3.2. Events and Delegates
        3. 12.3.3. Solving Delegate Problems with Events
        4. 12.3.4. The event Keyword
      4. 12.4. Using Anonymous Methods
      5. 12.5. Retrieving Values from Multicast Delegates
        1. 12.5.1. Invoking Events Asynchronously
        2. 12.5.2. Callback Methods
  5. II. Programming with C#
    1. 13. Building Windows Applications
      1. 13.1. Creating a Simple Windows Form
        1. 13.1.1. Using the Visual Studio Designer
      2. 13.2. Creating a Windows Forms Application
        1. 13.2.1. Creating the Basic UI Form
        2. 13.2.2. Populating the TreeView Controls
          1. 13.2.2.1. TreeNode objects
          2. 13.2.2.2. Recursing through the subdirectories
          3. 13.2.2.3. Getting the files in the directory
        3. 13.2.3. Handling TreeView Events
          1. 13.2.3.1. Clicking the source TreeView
          2. 13.2.3.2. Expanding a directory
          3. 13.2.3.3. Clicking the target TreeView
          4. 13.2.3.4. Handling the Clear button event
        4. 13.2.4. Implementing the Copy Button Event
          1. 13.2.4.1. Getting the selected files
          2. 13.2.4.2. Sorting the list of selected files
        5. 13.2.5. Handling the Delete Button Event
      3. 13.3. XML Documentation Comments
    2. 14. Accessing Data with ADO.NET
      1. 14.1. Relational Databases and SQL
        1. 14.1.1. Tables, Records, and Columns
        2. 14.1.2. Normalization
        3. 14.1.3. Declarative Referential Integrity
        4. 14.1.4. SQL
      2. 14.2. The ADO.NET Object Model
        1. 14.2.1. DataTables and DataColumns
        2. 14.2.2. DataRelations
        3. 14.2.3. Rows
        4. 14.2.4. Data Adapter
        5. 14.2.5. DBCommand and DBConnection
        6. 14.2.6. DataAdapter
        7. 14.2.7. DataReader
      3. 14.3. Getting Started with ADO.NET
      4. 14.4. Using OLE DB Managed Providers
      5. 14.5. Working with Data-Bound Controls
        1. 14.5.1. Populating a DataGrid Programmatically
        2. 14.5.2. Customizing the DataSet
    3. 15. Programming ASP.NET Applications and Web Services
      1. 15.1. Understanding Web Forms
        1. 15.1.1. Web Form Events
          1. 15.1.1.1. Postback versus nonpostback events
          2. 15.1.1.2. State
        2. 15.1.2. Web Form Life Cycle
      2. 15.2. Creating a Web Form
        1. 15.2.1. Code-Behind Files
      3. 15.3. Adding Controls
        1. 15.3.1. Server Controls
      4. 15.4. Data Binding
        1. 15.4.1. Examining the Code
        2. 15.4.2. Adding Controls and Events
      5. 15.5. Web Services
      6. 15.6. SOAP, WSDL, and Discovery
        1. 15.6.1. Server-Side Support
        2. 15.6.2. Client-Side Support
      7. 15.7. Building a Web Service
        1. 15.7.1. Testing Your Web Service
        2. 15.7.2. Viewing the WSDL Contract
      8. 15.8. Creating the Proxy
        1. 15.8.1. Testing the Web Service
    4. 16. Putting It All Together
      1. 16.1. The Overall Design
      2. 16.2. Creating the Web Services Client
        1. 16.2.1. Creating the Amazon proxy
          1. 16.2.1.1. Creating the desktop application
      3. 16.3. Displaying the Output
        1. 16.3.1. Implementing the Grid
        2. 16.3.2. Handling the RowDataBound Event
      4. 16.4. Searching by Category
  6. III. The CLR and the .NET Framework
    1. 17. Assemblies and Versioning
      1. 17.1. PE Files
      2. 17.2. Metadata
      3. 17.3. Security Boundary
      4. 17.4. Manifests
      5. 17.5. Multimodule Assemblies
        1. 17.5.1. Building a Multimodule Assembly
          1. 17.5.1.1. Testing the assembly
          2. 17.5.1.2. Loading the assembly
      6. 17.6. Private Assemblies
      7. 17.7. Shared Assemblies
        1. 17.7.1. The End of DLL Hell
        2. 17.7.2. Versions
        3. 17.7.3. Strong Names
        4. 17.7.4. The Global Assembly Cache
        5. 17.7.5. Building a Shared Assembly
          1. 17.7.5.1. Step 1: Create a strong name
          2. 17.7.5.2. Step 2: Put the shared assembly in the GAC
        6. 17.7.6. Other Required Assemblies
    2. 18. Attributes and Reflection
      1. 18.1. Attributes
        1. 18.1.1. Attributes
          1. 18.1.1.1. Attribute targets
          2. 18.1.1.2. Applying attributes
        2. 18.1.2. Custom Attributes
          1. 18.1.2.1. Declaring an attribute
          2. 18.1.2.2. Naming an attribute
          3. 18.1.2.3. Constructing an attribute
          4. 18.1.2.4. Using an attribute
      2. 18.2. Reflection
        1. 18.2.1. Viewing Metadata
        2. 18.2.2. Type Discovery
        3. 18.2.3. Reflecting on a Type
          1. 18.2.3.1. Finding all type members
          2. 18.2.3.2. Finding type methods
          3. 18.2.3.3. Finding particular type members
        4. 18.2.4. Late Binding
    3. 19. Marshaling and Remoting
      1. 19.1. Application Domains
        1. 19.1.1. Creating and Using App Domains
        2. 19.1.2. Marshaling Across App Domain Boundaries
          1. 19.1.2.1. Understanding marshaling with proxies
          2. 19.1.2.2. Specifying the marshaling method
      2. 19.2. Context
        1. 19.2.1. Context-Bound and Context-Agile Objects
        2. 19.2.2. Marshaling Across Context Boundaries
      3. 19.3. Remoting
        1. 19.3.1. Understanding Server Object Types
        2. 19.3.2. Specifying a Server with an Interface
        3. 19.3.3. Building a Server
        4. 19.3.4. Building the Client
        5. 19.3.5. Using SingleCall
        6. 19.3.6. Understanding RegisterWellKnownServiceType
        7. 19.3.7. Understanding Endpoints
    4. 20. Threads and Synchronization
      1. 20.1. Threads
        1. 20.1.1. Starting Threads
        2. 20.1.2. Joining Threads
        3. 20.1.3. Blocking Threads with Sleep
        4. 20.1.4. Killing Threads
      2. 20.2. Synchronization
        1. 20.2.1. Using Interlocked
        2. 20.2.2. Using Locks
        3. 20.2.3. Using Monitors
      3. 20.3. Race Conditions and Deadlocks
        1. 20.3.1. Race Conditions
        2. 20.3.2. Deadlock
    5. 21. Streams
      1. 21.1. Files and Directories
        1. 21.1.1. Working with Directories
        2. 21.1.2. Creating a DirectoryInfo Object
        3. 21.1.3. Working with Files
        4. 21.1.4. Modifying Files
      2. 21.2. Reading and Writing Data
        1. 21.2.1. Binary Files
        2. 21.2.2. Buffered Streams
        3. 21.2.3. Working with Text Files
      3. 21.3. Asynchronous I/O
      4. 21.4. Network I/O
        1. 21.4.1. Creating a Network Streaming Server
        2. 21.4.2. Creating a Streaming Network Client
        3. 21.4.3. Handling Multiple Connections
        4. 21.4.4. Asynchronous Network File Streaming
      5. 21.5. Web Streams
      6. 21.6. Serialization
        1. 21.6.1. Using a Formatter
        2. 21.6.2. Working with Serialization
          1. 21.6.2.1. Serializing the object
          2. 21.6.2.2. Deserializing the object
        3. 21.6.3. Handling Transient Data
      7. 21.7. Isolated Storage
    6. 22. Programming .NET and COM
      1. 22.1. Importing ActiveX Controls
        1. 22.1.1. Creating an ActiveX Control
        2. 22.1.2. Importing a Control in .NET
          1. 22.1.2.1. Importing a control
          2. 22.1.2.2. Manually importing the control
          3. 22.1.2.3. Adding the control to the form
      2. 22.2. Importing COM Components
        1. 22.2.1. Coding the COMTestForm Program
        2. 22.2.2. Importing the COM .DLL to .NET
        3. 22.2.3. Importing the Type Library
        4. 22.2.4. Importing Manually
        5. 22.2.5. Creating a Test Program
        6. 22.2.6. Using Late Binding and Reflection
      3. 22.3. Exporting .NET Components
        1. 22.3.1. Creating a Type Library
      4. 22.4. P/Invoke
      5. 22.5. Pointers
  7. A. C# Keywords
  8. Index
  9. Colophon
  10. Copyright