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

Book Description

Teach yourself Visual C# 2010-one step at a time. Ideal for developers with fundamental programming skills, this practical tutorial features learn-by-doing exercises that demonstrate how, when, and why to use the features of the C# rapid application development environment. You'll learn how to use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0; develop a solid, fundamental understanding of C# language features; and then get to work creating actual components and working applications for the Windows operating system. You'll also delve into data management technologies and Web-based applications.

Table of Contents

  1. Microsoft® Visual C#® 2010 Step by Step
  2. A Note Regarding Supplemental Files
  3. Acknowledgments
  4. Introduction
    1. Who This Book Is For
    2. Finding Your Best Starting Point in This Book
    3. Conventions and Features in This Book
      1. Conventions
      2. Other Features
    4. Prerelease Software
    5. Hardware and Software Requirements
    6. Code Samples
      1. Installing the Code Samples
      2. Using the Code Samples
      3. Uninstalling the Code Samples
    7. Find Additional Content Online
    8. Support for This Book
      1. Questions and Comments
  5. I. Introducing Microsoft Visual C# and Microsoft Visual Studio 2010
    1. 1. Welcome to C#
      1. Beginning Programming with the Visual Studio 2010 Environment
        1. Create a console application in Visual Studio 2010
          1. Create a console application in Visual Studio 2010
            1. Create a console application in Visual Studio 2010
      2. Writing Your First Program
        1. Write the code by using Microsoft IntelliSense
          1. Write the code by using Microsoft IntelliSense
            1. Write the code by using Microsoft IntelliSense
            2. Build and run the console application
      3. Using Namespaces
        1. Try longhand names
          1. Try longhand names
            1. Try longhand names
      4. Creating a Graphical Application
        1. Create a graphical application in Visual Studio 2010
          1. Create a graphical application in Visual Studio 2010
            1. Create a graphical application in Visual Studio 2010
            2. Create the user interface
            3. Write the code for the OK button
      5. Chapter 1 Quick Reference
    2. 2. Working with Variables, Operators, and Expressions
      1. Understanding Statements
      2. Using Identifiers
        1. Identifying Keywords
      3. Using Variables
        1. Naming Variables
        2. Declaring Variables
      4. Working with Primitive Data Types
        1. Unassigned Local Variables
        2. Displaying Primitive Data Type Values
      5. Using Arithmetic Operators
        1. Operators and Types
        2. Examining Arithmetic Operators
        3. Controlling Precedence
        4. Using Associativity to Evaluate Expressions
        5. Associativity and the Assignment Operator
      6. Incrementing and Decrementing Variables
        1. Prefix and Postfix
      7. Declaring Implicitly Typed Local Variables
      8. Chapter 2 Quick Reference
    3. 3. Writing Methods and Applying Scope
      1. Creating Methods
        1. Declaring a Method
        2. Returning Data from a Method
        3. Calling Methods
        4. Specifying the Method Call Syntax
      2. Applying Scope
        1. Defining Local Scope
        2. Defining Class Scope
        3. Overloading Methods
      3. Writing Methods
      4. Using Optional Parameters and Named Arguments
        1. Defining Optional Parameters
        2. Passing Named Arguments
        3. Resolving Ambiguities with Optional Parameters and Named Arguments
      5. Chapter 3 Quick Reference
    4. 4. Using Decision Statements
      1. Declaring Boolean Variables
      2. Using Boolean Operators
        1. Understanding Equality and Relational Operators
        2. Understanding Conditional Logical Operators
        3. Short-Circuiting
        4. 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. Unhandled Exceptions
        2. Using Multiple catch Handlers
        3. 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
        1. Working with Constructors
        2. Overloading Constructors
      5. Understanding static Methods and Data
        1. Creating a Shared Field
        2. Creating a static Field by Using the const Keyword
        3. Static Classes
        4. Anonymous Classes
      6. Chapter 7 Quick Reference
    2. 8. Understanding Values and References
      1. Copying Value Type Variables and Classes
      2. Understanding Null Values and Nullable Types
        1. Using Nullable Types
        2. Understanding the Properties of Nullable Types
      3. Using ref and out Parameters
        1. Creating ref Parameters
        2. Creating out Parameters
      4. How Computer Memory Is Organized
        1. Using the Stack and the Heap
      5. The System.Object Class
      6. Boxing
      7. Unboxing
      8. Casting Data Safely
        1. The is Operator
        2. The as Operator
      9. Chapter 8 Quick Reference
    3. 9. Creating Value Types with Enumerations and Structures
      1. Working with Enumerations
        1. Declaring an Enumeration
        2. Using an Enumeration
        3. Choosing Enumeration Literal Values
        4. Choosing an Enumeration’s Underlying Type
      2. Working with Structures
        1. Declaring a Structure
        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 an Array Instance
        3. Initializing Array Variables
        4. Creating an Implicitly Typed Array
        5. Accessing an Individual Array Element
        6. Iterating Through an Array
        7. Copying Arrays
        8. Using Multidimensional Arrays
        9. Using Arrays to Play Cards
      2. What Are Collection Classes?
        1. The ArrayList Collection Class
        2. The Queue Collection Class
        3. The Stack Collection Class
        4. The Hashtable Collection Class
        5. The SortedList Collection Class
        6. Using Collection Initializers
        7. Comparing Arrays and Collections
        8. Using Collection Classes to Play Cards
      3. Chapter 10 Quick Reference
    5. 11. Understanding Parameter Arrays
      1. Using Array Arguments
        1. Declaring a params Array
        2. Using params object[ ]
        3. Using a params Array
      2. Comparing Parameters Arrays and Optional Parameters
      3. Chapter 11 Quick Reference
    6. 12. Working with Inheritance
      1. What Is Inheritance?
      2. Using Inheritance
        1. Calling Base Class Constructors
        2. Assigning Classes
        3. Declaring new Methods
        4. Declaring Virtual Methods
        5. Declaring override Methods
        6. Understanding protected Access
      3. Understanding Extension Methods
      4. Chapter 12 Quick Reference
    7. 13. Creating Interfaces and Defining Abstract Classes
      1. Understanding Interfaces
        1. Defining an Interface
        2. Implementing an Interface
        3. Referencing a Class Through Its Interface
        4. Working with Multiple Interfaces
        5. Explicitly Implementing an Interface
        6. Interface Restrictions
        7. Defining and Using Interfaces
      2. Abstract Classes
        1. Abstract Methods
      3. Sealed Classes
        1. Sealed Methods
        2. Implementing and Using an Abstract Class
      4. Chapter 13 Quick Reference
    8. 14. 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. Implementing Exception-Safe Disposal
      4. Chapter 14 Quick Reference
  7. III. Creating Components
    1. 15. Implementing Properties to Access Fields
      1. Implementing Encapsulation by Using 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. Generating Automatic Properties
      6. Initializing Objects by Using Properties
      7. Chapter 15 Quick Reference
    2. 16. 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 16 Quick Reference
    3. 17. Interrupting Program Flow and Handling Events
      1. Declaring and Using Delegates
        1. The Automated Factory Scenario
        2. Implementing the Factory Without Using Delegates
        3. Implementing the Factory by Using a Delegate
        4. Using Delegates
      2. Lambda Expressions and Delegates
        1. Creating a Method Adapter
        2. Using a Lambda Expression as an Adapter
        3. The Form of Lambda Expressions
      3. Enabling Notifications with Events
        1. Declaring an Event
        2. Subscribing to an Event
        3. Unsubscribing from an Event
        4. Raising an Event
      4. Understanding WPF User Interface Events
        1. Using Events
      5. Chapter 17 Quick Reference
    4. 18. 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 by Using Generics
      4. Creating a Generic Method
        1. Defining a Generic Method to Build a Binary Tree
      5. Variance and Generic Interfaces
        1. Covariant Interfaces
        2. Contravariant Interfaces
      6. Chapter 18 Quick Reference
    5. 19. Enumerating Collections
      1. Enumerating the Elements in a 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<TItem> Class by Using an Iterator
      3. Chapter 19 Quick Reference
    6. 20. Querying In-Memory Data by Using Query Expressions
      1. What Is Language Integrated Query?
      2. Using LINQ in a C# Application
        1. Selecting Data
        2. Filtering Data
        3. Ordering, Grouping, and Aggregating Data
        4. Joining Data
        5. Using Query Operators
        6. Querying Data in Tree<TItem> Objects
        7. LINQ and Deferred Evaluation
      3. Chapter 20 Quick Reference
    7. 21. Operator Overloading
      1. Understanding Operators
        1. Operator Constraints
        2. Overloaded Operators
        3. Creating Symmetric Operators
      2. Understanding Compound Assignment Evaluation
      3. Declaring Increment and Decrement Operators
      4. Comparing Operators in Structures and Classes
      5. Defining Operator Pairs
      6. Implementing Operators
      7. Understanding Conversion Operators
        1. Providing Built-in Conversions
        2. Implementing User-Defined Conversion Operators
        3. Creating Symmetric Operators, Revisited
        4. Writing Conversion Operators
      8. Chapter 21 Quick Reference
  8. IV. Building Windows Presentation Foundation Applications
    1. 22. Introducing Windows Presentation Foundation
      1. Creating a WPF Application
        1. Building the WPF Application
      2. Adding Controls to the Form
        1. Using WPF Controls
        2. Changing Properties Dynamically
      3. Handling Events in a WPF Form
        1. Processing Events in Windows Forms
      4. Chapter 22 Quick Reference
    2. 23. Gathering User Input
      1. Menu Guidelines and Style
      2. Menus and Menu Events
        1. Creating a Menu
        2. Handling Menu Events
      3. Shortcut Menus
        1. Creating Shortcut Menus
      4. Windows Common Dialog Boxes
        1. Using the SaveFileDialog Class
      5. Improving Responsiveness in a WPF Application
      6. Chapter 23 Quick Reference
    3. 24. Performing Validation
      1. Validating Data
        1. Strategies for Validating User Input
      2. An Example—Order Tickets for Events
        1. Performing Validation by Using Data Binding
        2. Changing the Point at Which Validation Occurs
      3. Chapter 24 Quick Reference
  9. V. Managing Data
    1. 25. Querying Information in a Database
      1. Querying a Database by Using ADO.NET
        1. The Northwind Database
        2. Creating the Database
        3. Using ADO.NET to Query Order Information
      2. Querying a Database by Using LINQ to SQL
        1. Defining an Entity Class
        2. Creating and Running a LINQ to SQL Query
        3. Deferred and Immediate Fetching
        4. Joining Tables and Creating Relationships
        5. Deferred and Immediate Fetching Revisited
        6. Defining a Custom DataContext Class
        7. Using LINQ to SQL to Query Order Information
      3. Chapter 25 Quick Reference
    2. 26. Displaying and Editing Data by Using the Entity Framework and Data Binding
      1. Using Data Binding with the Entity Framework
      2. Using Data Binding to Modify Data
        1. Updating Existing Data
        2. Handling Conflicting Updates
        3. Adding and Deleting Data
      3. Chapter 26 Quick Reference
  10. VI. Building Professional Solutions with Visual Studio
    1. 27. Introducing the Task Parallel Library
      1. Why Perform Multitasking by Using Parallel Processing?
        1. The Rise of the Multicore Processor
      2. Implementing Multitasking in a Desktop Application
        1. Tasks, Threads, and the ThreadPool
        2. Creating, Running, and Controlling Tasks
        3. Using the Task Class to Implement Parallelism
        4. Abstracting Tasks by Using the Parallel Class
        5. When Not to Use the Parallel Class
        6. Returning a Value from a Task
      3. Using Tasks and User Interface Threads Together
      4. Canceling Tasks and Handling Exceptions
        1. The Mechanics of Cooperative Cancellation
        2. Handling Task Exceptions by Using the AggregateException Class
        3. Using Continuations with Canceled and Faulted Tasks
      5. Chapter 27 Quick Reference
    2. 28. Performing Parallel Data Access
      1. Using PLINQ to Parallelize Declarative Data Access
        1. Using PLINQ to Improve Performance While Iterating Through a Collection
        2. Specifying Options for a PLINQ Query
        3. Canceling a PLINQ Query
      2. Synchronizing Concurrent Imperative Data Access
        1. Locking Data
        2. Synchronization Primitives in the Task Parallel Library
          1. The ManualResetEventSlim Class
          2. The SemaphoreSlim Class
          3. The CountdownEvent Class
          4. The ReaderWriterLockSlim Class
          5. The Barrier Class
        3. Cancellation and the Synchronization Primitives
        4. The Concurrent Collection Classes
        5. Using a Concurrent Collection and a Lock to Implement Thread-Safe Data Access
      3. Chapter 28 Quick Reference
    3. 29. Creating and Using a Web Service
      1. What Is a Web Service?
        1. The Role of Windows Communication Foundation
      2. Web Service Architectures
        1. SOAP Web Services
          1. The Role of SOAP
          2. What Is the Web Services Description Language?
          3. Nonfunctional Requirements of Web Services
        2. REST Web Services
      3. Building Web Services
        1. Creating the ProductInformation SOAP Web Service
        2. SOAP Web Services, Clients, and Proxies
          1. Talking SOAP: The Difficult Way
          2. Talking SOAP: The Easy Way
        3. Consuming the ProductInformation SOAP Web Service
        4. Creating the ProductDetails REST Web Service
        5. Consuming the ProductDetails REST Web Service
      4. Chapter 29 Quick Reference
  11. A. Interoperating with Dynamic Languages
    1. What Is the Dynamic Language Runtime?
    2. The dynamic Keyword
    3. Example: IronPython
    4. Example: IronRuby
    5. Summary
  12. Index
  13. About the Author
  14. Copyright