You are previewing Programming C# 3.0, 5th Edition.
O'Reilly logo
Programming C# 3.0, 5th Edition

Book Description

Written by popular author and .NET expert Jesse Liberty, this thoroughly updated tutorial for beginning to intermediate programmers covers the latest release of Microsoft's popular C# language (C# 3.0) and the newest .NET platform for developing Windows and web applications. Our bestselling Programming C# 3.0, now in its fifth edition, is a world-class tutorial that goes well beyond the documentation otherwise available. Liberty doesn't just teach C#; he tells the complete story of the C# language and how it integrates with all of .NET programming, so that you can get started creating professional quality web and Windows applications. This book:

  • Provides a comprehensive tutorial in C# and .NET programming that also serves as a useful reference you'll want by your side while you're working

  • Covers all of the new features of the language, thoroughly integrated into every chapter, rather than tacked on at the end

  • Provides insight into best practices and insight into real world programming by a professional programmer who worked with C# as an independent contractor for nearly a decade before joining Microsoft as a Senior Program Manager

  • Every chapter in this book has been totally revised, and the entire book has been reorganized to respond to the significant changes in the language

  • Full coverage, from the ground up of LINQ (Language Integrated Query) and other C# 3.0 language innovations to speed up development tasks

  • Explains how to use C# in creating Web Applications as well as Windows Applications, using both the new Windows Presentation Foundation (WPF) and the older WinForms technology

This new edition of Programming C# 3.0 is for working programmers who want to develop proficiency in Microsoft's most important language. No prior .NET experience is required for you to get started. There's no time like the present to work with C# -- and no book like this one to teach you everything you need to know.

Special note to VB6 and Java programmers: if you've decided to transition to .NET, this book will take you there.

Table of Contents

  1. Programming C# 3.0, 5th Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. C# and .NET
    2. About This Book
    3. What You Need to Use This Book
    4. How This Book Is Organized
      1. The C# Language
      2. Part II C# and Data
      3. Part III Programming with C#
      4. Part IV The CLR and the .NET Framework
    5. Who This Book Is For
    6. Conventions Used in This Book
    7. Support
    8. We'd Like to Hear from You
    9. Using Code Examples
    10. Safari® Books Online
    11. Acknowledgments
      1. From Jesse Liberty
      2. From Donald Xie
    12. Dedications
      1. From Jesse Liberty
      2. From Donald Xie
  4. I. The C# Language
    1. 1. C# 3.0 and .NET 3.5
      1. The Evolution of C#
      2. The C# Language
        1. A Tiny Bit of History
        2. C# Features
      3. The .NET Platform
    2. 2. Getting Started: "Hello World"
      1. Classes, Objects, and Types
        1. Methods
        2. Comments
        3. Console Applications
        4. Namespaces
        5. The Dot Operator (.)
        6. The using Directive
        7. Case Sensitivity
        8. The static Keyword
      2. Developing "Hello World"
        1. Editing "Hello World"
        2. Compiling and Running "Hello World"
      3. Using the Visual Studio 2008 Debugger
    3. 3. C# Language Fundamentals
      1. Types
        1. The Built-In Types
          1. Choosing a built-in type
          2. Converting built-in types
      2. Variables and Constants
        1. Definite Assignment
        2. Constants
        3. Enumerations
        4. Strings
        5. Identifiers
      3. Whitespace
      4. Statements
        1. Unconditional Branching Statements
        2. Conditional Branching Statements
          1. if...else statements
          2. Nested if statements
          3. switch statements: an alternative to nested ifs
          4. Switch on string statements
        3. Iteration Statements
          1. The goto statement
          2. The while loop
          3. The do...while loop
          4. The for loop
          5. The foreach statement
          6. The continue and break statements
      5. Operators
        1. The Assignment Operator (=)
        2. Mathematical Operators
          1. Simple arithmetical operators (+, −, *, /)
          2. The modulus operator (%) to return remainders
        3. Increment and Decrement Operators
          1. Calculate and reassign operators
          2. The prefix and postfix operators
        4. Relational Operators
        5. Use of Logical Operators with Conditionals
        6. Operator Precedence
        7. The Ternary Operator
      6. Preprocessor Directives
        1. Defining Identifiers
        2. Undefining Identifiers
        3. #if, #elif, #else, and #endif
    4. 4. Classes and Objects
      1. Defining Classes
        1. Access Modifiers
        2. Method Arguments
      2. Creating Objects
        1. Constructors
        2. Initializers
        3. Object Initializer
        4. The this Keyword
      3. Using Static Members
        1. Invoking Static Methods
        2. Using Static Fields
      4. Destroying Objects
        1. Finalizers Versus Dispose
        2. Implementing the Close( ) Method
        3. The using Statement
      5. Passing Parameters
        1. Passing by Reference
        2. Overcoming Definite Assignment with "out" Parameters
      6. Overloading Methods and Constructors
      7. Encapsulating Data with Properties
        1. The get Accessor
        2. The set Accessor
        3. Property Access Modifiers
      8. Automatic Properties
      9. readonly Fields
    5. 5. Inheritance and Polymorphism
      1. Specialization and Generalization
      2. Inheritance
        1. Implementing Inheritance
      3. Polymorphism
        1. Creating Polymorphic Types
        2. Creating Polymorphic Methods
        3. Calling Base Class Constructors
        4. Controlling Access
        5. Versioning with the new and override Keywords
      4. Abstract Classes
        1. Limitations of Abstract
        2. Sealed Class
      5. The Root of All Types: Object
      6. Nesting Classes
    6. 6. Operator Overloading
      1. Using the operator Keyword
      2. Supporting Other .NET Languages
      3. Creating Useful Operators
      4. Logical Pairs
      5. The Equality Operator
      6. Conversion Operators
      7. Putting Operators to Work
    7. 7. Structs
      1. Defining Structs
      2. Creating Structs
        1. Structs As Value Types
    8. 8. Interfaces
      1. Defining and Implementing an Interface
        1. Implementing More Than One Interface
        2. Extending Interfaces
        3. Combining Interfaces
        4. Polymorphism with Interfaces
        5. Interface Versus Abstract Class
      2. Overriding Interface Implementations
      3. Explicit Interface Implementation
        1. Selectively Exposing Interface Methods
        2. Member Hiding
    9. 9. Arrays, Indexers, and Collections
      1. Arrays
        1. Declaring Arrays
        2. Understanding Default Values
        3. Accessing Array Elements
      2. The foreach Statement
        1. Initializing Array Elements
        2. The params Keyword
        3. Multidimensional Arrays
          1. Rectangular arrays
          2. Jagged arrays
        4. Array Bounds
        5. Array Conversions
        6. Sorting Arrays
      3. Indexers
        1. Indexers and Assignment
        2. Indexing on Other Values
      4. Collection Interfaces
        1. The IEnumerable<T> Interface
      5. Constraints
      6. List<T>
        1. Implementing IComparable
        2. Implementing IComparer
      7. Queues
      8. Stacks
      9. Dictionaries
        1. IDictionary<K,V>
    10. 10. Strings and Regular Expressions
      1. Strings
        1. Creating Strings
        2. The ToString( ) Method
        3. Manipulating Strings
        4. Finding Substrings
        5. Splitting Strings
        6. Manipulating Dynamic Strings
      2. Regular Expressions
        1. Using Regular Expressions: Regex
        2. Using Regex Match Collections
        3. Using Regex Groups
        4. Using CaptureCollection
    11. 11. Exceptions
      1. Throwing and Catching Exceptions
        1. The throw Statement
        2. The catch Statement
          1. Taking corrective action
          2. Unwinding the call stack
        3. Try/Catch Best Practices
        4. The finally Statement
      2. Exception Objects
    12. 12. Delegates and Events
      1. Events
      2. Events and Delegates
        1. Indirect Invocation
        2. Publish and Subscribe/Observer
        3. The Publishing Class: Clock
        4. Registering to Be Notified
        5. Sequence of Events
        6. The Danger with Delegates
        7. The event Keyword
      3. Anonymous Methods
        1. Lambda Expressions
        2. Callback Methods
  5. II. C# and Data
    1. 13. Introducing LINQ
      1. Defining and Executing a Query
        1. Creating the Query
          1. The from clause
          2. Filtering
          3. Projection (or select)
        2. Deferred Query Evaluation
      2. LINQ and C#
        1. Joining
        2. Ordering and the var Keyword
        3. Grouping and the group Keyword
      3. Anonymous Types
      4. Implicitly Typed Local Variables
      5. Extension Methods
        1. Defining and Using Extension Methods
        2. Extension Method Restrictions
      6. Lambda Expressions in LINQ
    2. 14. Working with XML
      1. XML Basics (A Quick Review)
        1. Elements
        2. XHTML
      2. X Stands for eXtensible
      3. Creating XML Documents
        1. XML Elements
        2. XML Attributes
      4. Searching in XML with XPath
        1. Searching for a Single Node
        2. Searching Using Axes
        3. Predicates
        4. XPath Functions
      5. Searching Using XPathNavigator
        1. Using XPathNodeIterator
        2. Using XPathExpression
      6. XML Serialization
        1. Customizing XML Serialization Using Attributes
        2. Runtime XML Serialization Customization
    3. 15. Putting LINQ to Work
      1. Getting Set Up
      2. LINQ to SQL Fundamentals
      3. Using Visual Studio LINQ to SQL Designer
      4. Retrieving Data
        1. Creating Properties for Each Table
        2. A LINQ Query
      5. Updating Data Using LINQ to SQL
        1. Adding a Customer Record
        2. Modifying a Customer Record
      6. Deleting Relational Data
      7. LINQ to XML
    4. 16. ADO.NET and Relational Databases
      1. Relational Databases and SQL
        1. Tables, Records, and Columns
        2. Normalization
        3. Declarative Referential Integrity
        4. SQL
      2. The ADO.NET Object Model
        1. DataTables and DataColumns
        2. DataRelations
        3. Rows
        4. Data Adapter
        5. DBCommand and DBConnection
        6. DataReader
      3. Getting Started with ADO.NET
  6. III. Programming with C#
    1. 17. Programming ASP.NET Applications
      1. Web Forms Fundamentals
        1. Web Forms Events
          1. Postback versus nonpostback events
          2. State
        2. Web Forms Life Cycle
      2. Creating a Web Form
        1. Code-Behind Files
        2. Adding Controls
        3. Server Controls
      3. Data Binding
        1. Examining the Code
        2. Adding Controls and Events
    2. 18. WPF Applications
      1. WPF in a Very Small Nutshell
      2. Creating a WPF Example
        1. The Example Program
        2. Building the Application
        3. Grids and Stack Panels
        4. Style and Animation
          1. Defining listbox styles
          2. Triggers and animations
        5. Adding Data
          1. Instantiating objects declaratively
        6. Using the Data in the XAML
        7. Defining the Listbox
        8. The Complete XAML File
      3. Event Handling (Finally!)
      4. What Have You Learned, Dorothy?
    3. 19. Programming Windows Forms Applications
      1. Creating the Application
        1. Creating Event Handlers
        2. Populating the TreeView Controls
          1. TreeNode objects
          2. Recursing through the subdirectories
          3. Getting the files in the directory
        3. Handling TreeView Events
          1. Clicking the source TreeView
          2. Expanding a directory
          3. Clicking the target TreeView
          4. Handling the Clear button event
        4. Implementing the Copy Button Event
          1. Getting the selected files
          2. Sorting the list of selected files
        5. Handling the Delete Button Event
  7. IV. The CLR and the .NET Framework
    1. 20. Attributes and Reflection
      1. Attributes
        1. Types of Attributes
          1. Attribute targets
          2. Applying attributes
        2. Custom Attributes
          1. Declaring an attribute
          2. Naming an attribute
          3. Constructing an attribute
          4. Using an attribute
      2. Reflection
        1. Viewing Metadata
        2. Type Discovery
        3. Reflecting on a Type
          1. Finding all type members
          2. Finding type methods
          3. Finding particular type members
        4. Late Binding
    2. 21. Threads and Synchronization
      1. Threads
        1. Starting Threads
        2. Joining Threads
        3. Blocking Threads with Sleep
        4. Killing Threads
      2. Synchronization
        1. Using Interlocked
        2. Using Locks
        3. Using Monitors
      3. Race Conditions and Deadlocks
        1. Race Conditions
        2. Deadlocks
    3. 22. Streams
      1. Files and Directories
        1. Working with Directories
        2. Creating a DirectoryInfo Object
        3. Working with Files
        4. Modifying Files
      2. Reading and Writing Data
        1. Binary Files
        2. Buffered Streams
        3. Working with Text Files
      3. Asynchronous I/O
      4. Network I/O
        1. Creating a Network Streaming Server
        2. Creating a Streaming Network Client
        3. Handling Multiple Connections
        4. Asynchronous Network File Streaming
      5. Web Streams
      6. Serialization
        1. Using a Formatter
        2. Working with Serialization
          1. Serializing the object
          2. Deserializing the object
        3. Handling Transient Data
      7. Isolated Storage
    4. 23. Programming .NET and COM
      1. Importing ActiveX Controls
        1. Creating an ActiveX Control
        2. Importing a Control in .NET
          1. Importing a control
          2. Manually importing the control
          3. Adding the control to the form
        3. P/Invoke
      2. Pointers
  8. C# Keywords
  9. About the Authors
  10. Colophon
  11. Copyright