You are previewing Microsoft® Visual C#® 2005 Step by Step.
O'Reilly logo
Microsoft® Visual C#® 2005 Step by Step

Book Description

Visual C#, a feature of Microsoft Visual Studio® 2005, is a modern programming language designed to deliver a productive environment for creating business frameworks and reusable object-oriented components. Now you can teach yourself essential techniques with Visual C#—and start building components and Microsoft Windows®-based applications—one step at a time. With STEP BY STEP, you work at your own pace through hands-on, learn-by-doing exercises. Whether you’re a beginning programmer or new to this particular language, you’ll learn how, when, and why to use specific features of Visual C# 2005. Each chapter puts you to work, building your knowledge of core capabilities and guiding you as create your first C#-based applications for Windows, data management, and the Web.

Table of Contents

  1. Microsoft® Visual C#® 2005 Step by Step
  2. A Note Regarding Supplemental Files
  3. Acknowledgments
  4. Introduction
    1. Finding Your Best Starting Point in This Book
    2. Conventions and Features in This Book
      1. Conventions
      2. Other Features
    3. Online Companion Content
      1. Technology Updates
    4. System Requirements
      1. Prerelease Software
    5. Installing and Using the Practice Files
      1. Installing the Practice Files
      2. Configuring SQL Server Express Edition
      3. Using the Practice Files
      4. Uninstalling the Practice Files
    6. Support for this Book
      1. Questions and Comments
  5. I. Introducing Microsoft Visual C# and Microsoft Visual Studio 2005
    1. 1. Welcome to C#
      1. Beginning Programming with the Visual Studio 2005 Environment
      2. Writing Your First Program
      3. Using Namespaces
      4. Creating a Windows Forms Application
      5. Chapter 1 Quick Reference
    2. 2. Working with Variables, Operators, and Expressions
      1. Understanding Statements
      2. Using Identifiers
      3. Identifying Keywords
      4. Using Variables
        1. Naming Variables
        2. Declaring Variables
      5. Working with Primitive Data Types
        1. Displaying Primitive Data Type Values
      6. Using Arithmetic Operators
        1. Determining an Operator’s Values
        2. Examining Arithmetic Operators
        3. Controlling Precedence
        4. Using Associativity to Evaluate Expressions
      7. Incrementing and Decrementing Variables
        1. Prefix and Postfix
      8. Chapter 2 Quick Reference
    3. 3. Writing Methods and Applying Scope
      1. Declaring Methods
        1. Specifying the Method Declaration Syntax
        2. Writing return Statements
      2. Calling Methods
        1. Specifying the Method Call Syntax
      3. Applying Scope
        1. Defining Local Scope
        2. Defining Class Scope
        3. Overloading Methods
      4. Writing Methods
      5. Chapter 3 Quick Reference
    4. 4. Using Decision Statements
      1. Declaring bool Variables
      2. Using Boolean Operators
        1. Understanding Equality and Relational Operators
        2. Understanding Conditional Logical Operators
        3. Summarizing Operator Precedence and Associativity
      3. Using if Statements to Make Decisions
        1. Understanding if Statement Syntax
        2. Using Blocks to Group Statements
        3. Cascading if Statements
      4. Using switch Statements
        1. Understanding switch Statement Syntax
        2. Following the switch Statement Rules
      5. Chapter 4 Quick Reference
    5. 5. Using Compound Assignment and Iteration Statements
      1. Using Compound Assignment Operators
      2. Writing while Statements
      3. Writing for Statements
        1. Understanding for Statement Scope
      4. Writing do Statements
      5. Chapter 5 Quick Reference
    6. 6. Managing Errors and Exceptions
      1. Coping with Errors
      2. Trying Code and Catching Exceptions
        1. Handling an Exception
        2. Unhandled Exceptions
        3. Using Multiple catch Handlers
        4. Catching Multiple Exceptions
      3. Using Checked and Unchecked Integer Arithmetic
        1. Writing checked Statements
        2. Writing Checked Expressions
      4. Throwing Exceptions
      5. Using a finally Block
      6. Chapter 6 Quick Reference
  6. II. Understanding the C# Language
    1. 7. Creating and Managing Classes and Objects
      1. Understanding Classification
      2. The Purpose of Encapsulation
      3. Defining and Using a Class
      4. Controlling Accessibility
      5. Working with Constructors
        1. Overloading Constructors
      6. Understanding static Methods and Data
        1. Creating a Shared Field
        2. Creating a static Field with the const Keyword
      7. Chapter 7 Quick Reference
    2. 8. Understanding Values and References
      1. Copying int Variables and Classes
      2. Using ref and out Parameters
        1. Creating ref Parameters
        2. Creating out Parameters
      3. How Computer Memory is Organized
        1. Using the Stack and the Heap
      4. The System.Object Class
      5. Boxing
      6. Unboxing
      7. Chapter 8 Quick Reference
    3. 9. Creating Value Types with Enumerations and Structs
      1. Working with Enumerations
        1. Declaring an Enumeration Type
        2. Using an Enumeration
        3. Choosing Enumeration Literal Values
        4. Choosing an Enumeration’s Underlying Type
      2. Working with Structure Types
        1. Declaring Structure Types
        2. Understanding Structure and Class Differences
        3. Declaring Structure Variables
        4. Understanding Structure Initialization
        5. Copying Structure Variables
      3. Chapter 9 Quick Reference
    4. 10. Using Arrays and Collections
      1. What Is an Array?
        1. Declaring Array Variables
        2. Creating Array Instances
        3. Initializing Array Variables
        4. Accessing Individual Array Elements
        5. Iterating Through an Array
        6. Copying Arrays
      2. What Are Collection Classes?
        1. The ArrayList Class
        2. The Queue Class
        3. The Stack Class
        4. The Hashtable Class
        5. The SortedList Class
        6. Comparing Arrays and Collections
        7. Using Collection Classes to Play Cards
      3. Chapter 10 Quick Reference
    5. 11. Understanding Parameter Arrays
      1. Using Array Arguments
        1. Declaring params Arrays
        2. Using params object[ ]
        3. Using params Arrays
      2. Chapter 11 Quick Reference
    6. 12. Working with Inheritance
      1. What Is Inheritance?
      2. Using Inheritance
        1. Base Classes and Derived Classes
        2. Calling Base Class Constructors
        3. Assigning Classes
        4. new Methods
        5. Virtual Methods
        6. override Methods
        7. protected Access
      3. Creating Interfaces
        1. Interface Syntax
        2. Interface Restrictions
        3. Implementing an Interface
        4. Referencing a Class Through Its Interface
        5. Working with Multiple Interfaces
      4. Abstract Classes
      5. Sealed Classes
        1. Sealed Methods
      6. Extending an Inheritance Hierarchy
      7. Summarizing Keyword Combinations
      8. Chapter 12 Quick Reference
    7. 13. Using Garbage Collection and Resource Management
      1. The Life and Times of an Object
        1. Writing Destructors
        2. Why Use the Garbage Collector?
        3. How Does the Garbage Collector Work?
        4. Recommendations
      2. Resource Management
        1. Disposal Methods
        2. Exception-Safe Disposal
        3. The using Statement
        4. Calling the Dispose Method from a Destructor
      3. Making Code Exception-Safe
      4. Chapter 13 Quick Reference
  7. III. Creating Components
    1. 14. Implementing Properties to Access Attributes
      1. Comparing Fields and Methods
      2. What Are Properties?
        1. Using Properties
        2. Read-Only Properties
        3. Write-Only Properties
        4. Property Accessibility
      3. Understanding the Property Restrictions
      4. Declaring Interface Properties
        1. Using Properties in a Windows Application
      5. Chapter 14 Quick Reference
    2. 15. Using Indexers
      1. What Is an Indexer?
        1. An Example That Doesn’t Use Indexers
        2. The Same Example Using Indexers
        3. Understanding Indexer Accessors
        4. Comparing Indexers and Arrays
      2. Indexers in Interfaces
      3. Using Indexers in a Windows Application
      4. Chapter 15 Quick Reference
    3. 16. Delegates and Events
      1. Declaring and Using Delegates
      2. The Automated Factory Scenario
        1. Implementing the Factory Without Using Delegates
        2. Implementing the Factory by Using a Delegate
      3. Using Delegates
      4. Anonymous Methods and Delegates
        1. Creating a Method Adapter
        2. Using an Anonymous Method as an Adapter
        3. Features of Anonymous Methods
      5. Enabling Notifications with Events
      6. Declaring an Event
      7. Subscribing to an Event
      8. Unsubscribing from an Event
      9. Raising an Event
      10. Understanding GUI Events
      11. Using Events
      12. Chapter 16 Quick Reference
    4. 17. Introducing Generics
      1. The Problem with Objects
      2. The Generics Solution
        1. Generics vs. Generalized Classes
        2. Generics and Constraints
      3. Creating a Generic Class
        1. The Theory of Binary Trees
        2. Building a Binary Tree Class Using Generics
      4. Creating a Generic Method
        1. Defining a Generic Method to Build a Binary Tree
      5. Chapter 17 Quick Reference
    5. 18. Enumerating Collections
      1. Enumerating the Elements in Collection
        1. Manually Implementing an Enumerator
        2. Implementing the IEnumerable Interface
      2. Implementing an Enumerator by Using an Iterator
        1. A Simple Iterator
        2. Defining an Enumerator for the Tree<T> Class by Using an Iterator
      3. Chapter 18 Quick Reference
    6. 19. Operator Overloading
      1. Understanding Operators
        1. Operator Constraints
        2. Overloaded Operators
        3. Creating Symmetric Operators
      2. Understanding Compound Assignment
      3. Declaring Increment and Decrement Operators
      4. Defining Operator Pairs
      5. Implementing an Operator
      6. Understanding Conversion Operators
        1. Providing Built-In Conversions
        2. Implementing User-Defined Conversion Operators
        3. Creating Symmetric Operators Revisited
        4. Adding an Implicit Conversion Operator
      7. Chapter 19 Quick Reference
  8. IV. Working with Windows Applications
    1. 20. Introducing Windows Forms
      1. Creating Your Application
        1. Creating a Windows Forms Application
        2. What Are the Common Windows Forms Properties?
        3. Changing Properties Programmatically
      2. Adding Controls to the Form
        1. Using Windows Forms Controls
        2. Setting Control Properties
        3. Changing Properties Dynamically
          1. Programming the User Interface
            1. Populating the ComboBox
            2. Set the current date
            3. Initialize the CheckBox
            4. Initialize the radio button group
            5. Fill the ListBox
            6. Call the Reset method
      3. Publishing Events in Windows Forms
        1. Processing Events in Windows Forms
      4. Run the Application
      5. Chapter 20 Quick Reference
    2. 21. Working with Menus and Dialog Boxes
      1. Menu Guidelines and Style
      2. Adding Menus and Processing Menu Events
        1. Creating a Menu
        2. Setting Menu Item Properties
        3. Other Menu Item Properties
        4. Menu Events
      3. Pop-Up Menus
        1. Creating Pop-Up Menus
      4. Using Common Dialog Controls
        1. Using the SaveFileDialog Control
        2. Using a Printer
      5. Chapter 21 Quick Reference
    3. 22. Performing Validation
      1. Validating Data
        1. The CausesValidation Property
        2. Validation Events
      2. An Example—Customer Maintenance
        1. A First Attempt at Performing Validation
        2. Being Unobtrusive
        3. Using an ErrorProvider Control
        4. Adding a Status Bar
      3. Chapter 22 Quick Reference
  9. V. Managing Data
    1. 23. Using a Database
      1. Using ADO.NET Databases
        1. The Northwind Traders Database
        2. Creating the Database
        3. Accessing the Database
          1. Understanding DataSets, DataTables, and TableAdapters
          2. Displaying Data in an Application
      2. Using ADO.NET Programmatically
      3. Chapter 23 Quick Reference
    2. 24. Working with Data Binding and DataSets
      1. Windows Forms Controls and Data Binding
        1. Defining a DataSet and Using Simple Data Binding
        2. Using Complex Data Binding
      2. Updating a Database Using a DataSet
        1. Managing Connections
        2. Handling Multi-User Updates
        3. Using a DataSet with a DataGridView Control
        4. Validating User Input in the DataGridView Control
        5. Performing Updates by Using a DataSet
      3. Chapter 24 Quick Reference
  10. VI. Building Web Applications
    1. 25. Introducing ASP.NET
      1. Understanding the Internet as an Infrastructure
        1. Understanding Web Server Requests and Responses
        2. Managing State
        3. Understanding ASP.NET
      2. Creating Web Applications with ASP.NET
        1. Building an ASP.NET Application
        2. Understanding Server Controls
        3. Creating and Using a Theme
          1. Defining a Theme
          2. Applying a Theme
      3. Chapter 25 Quick Reference
    2. 26. Understanding Web Forms Validation Controls
      1. Comparing Server and Client Validations
        1. Server Validation
        2. Client Validation
        3. Implementing Client Validation
      2. Chapter 26 Quick Reference
    3. 27. Securing a Web Site and Accessing Data with Web Forms
      1. Using the Web Forms GridView Control
      2. Managing Security
        1. Understanding Forms-Based Security
        2. Implementing Forms-Based Security
      3. Querying Data
        1. Displaying Customer Information
        2. Displaying Data in Pages
        3. Optimizing Data Access
        4. Caching Data in a Data Source
      4. Editing Data
        1. Deleting Rows from a GridView Control
        2. Updating Rows in a GridView Control
      5. Chapter 27 Quick Reference
    4. 28. Creating and Using a Web Service
      1. What Is a Web Service?
        1. The Role of SOAP
        2. What Is the Web Services Description Language?
      2. Building the ProductService Web Service
        1. Creating the ProductService Web Service
        2. Handling Complex Data
      3. Web Services, Clients, and Proxies
        1. Talking SOAP: The Difficult Way
        2. Talking SOAP: The Easy Way
        3. Consuming the ProductService Web Service
      4. Chapter 28 Quick Reference
  11. About the Author
  12. Index
  13. About the Author
  14. Copyright