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

Book Description

Teach yourself how to build applications with Microsoft Visual C# 2012 and Visual Studio 2012—one step at a time. Ideal for those with fundamental programming skills, this tutorial provides practical, learn-by-doing exercises for mastering core C# language features and creating working applications and components for Windows.

Discover how to:

  • Work with variables, non-reserved identifiers, statements, operators, and methods

  • Use the new application models provided by Windows 8 and the Windows Runtime

  • Create interfaces and define abstract classes

  • Manage errors and exception handling

  • Use collection classes

  • Work with databases by using DataBinding with the Entity Framework

  • Respond to user input and gestures; gather input from devices and other sources

  • Handle events arising from multiple sources

  • Develop your first Windows 8 apps

  • Table of Contents

    1. Microsoft® Visual C#® 2012 Step by Step
    2. Dedication
    3. A Note Regarding Supplemental Files
    4. Introduction
      1. Who Should Read This Book
      2. Who Should Not Read This Book
      3. Organization of This Book
        1. Finding Your Best Starting Point in This Book
      4. Conventions and Features in This Book
      5. System Requirements
      6. Code Samples
        1. Installing the Code Samples
        2. Using the Code Samples
      7. Acknowledgments
      8. Errata and Book Support
      9. We Want to Hear from You
      10. Stay in Touch
    5. I. Introducing Microsoft Visual C# and Microsoft Visual Studio 2012
      1. 1. Welcome to C#
        1. Beginning Programming with the Visual Studio 2012 Environment
        2. Writing Your First Program
        3. Using Namespaces
        4. Creating a Graphical Application
          1. Examining the Windows Store App
          2. Examining the WPF Application
          3. Adding Code to the Graphical Application
        5. Summary
        6. 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. Summary
        9. 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
            1. 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. Summary
        6. 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. Summary
        6. 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. Summary
        6. 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
          4. Propagating 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. Summary
        7. Chapter 6 Quick Reference
    6. II. Understanding the C# Object Model
      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. Understanding static Classes
          4. Anonymous Classes
        6. Summary
        7. 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. Summary
        10. 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. Summary
        4. Chapter 9 Quick Reference
      4. 10. Using Arrays
        1. Declaring and Creating an Array
          1. Declaring Array Variables
          2. Creating an Array Instance
        2. Populating and Using an Array
          1. Creating an Implicitly Typed Array
          2. Accessing an Individual Array Element
          3. Iterating Through an Array
          4. Passing Arrays as Parameters and Return Values for a Method
        3. Copying Arrays
        4. Using Multidimensional Arrays
          1. Creating Jagged Arrays
        5. Summary
        6. Chapter 10 Quick Reference
      5. 11. Understanding Parameter Arrays
        1. Overloading: A Recap
        2. Using Array Arguments
          1. Declaring a params Array
          2. Using params object[ ]
          3. Using a params Array
        3. Comparing Parameter Arrays and Optional Parameters
        4. Summary
        5. Chapter 11 Quick Reference
      6. 12. Working with Inheritance
        1. What Is Inheritance?
        2. Using Inheritance
          1. The System.Object Class Revisited
          2. Calling Base Class Constructors
          3. Assigning Classes
          4. Declaring new Methods
          5. Declaring virtual Methods
          6. Declaring override Methods
          7. Understanding protected Access
        3. Understanding Extension Methods
        4. Summary
        5. 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. Summary
        5. 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 and the IDisposable Interface
          4. Calling the Dispose Method from a Destructor
        3. Implementing Exception-Safe Disposal
        4. Summary
        5. Chapter 14 Quick Reference
    7. III. Defining Extensible Types with C#
      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. Replacing Methods with Properties
        5. Generating Automatic Properties
        6. Initializing Objects by Using Properties
        7. Summary
        8. 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. Summary
        5. Chapter 16 Quick Reference
      3. 17. Introducing Generics
        1. The Problem with the object Type
        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. Summary
        7. Chapter 17 Quick Reference
      4. 18. Using Collections
        1. What Are Collection Classes?
          1. The List<T> Collection Class
          2. The LinkedList<T> Collection Class
          3. The Queue<T> Collection Class
          4. The Stack<T> Collection Class
          5. The Dictionary<TKey, TValue> Collection Class
          6. The SortedList<TKey, TValue> Collection Class
          7. The HashSet<T> Collection Class
        2. Using Collection Initializers
        3. The Find Methods, Predicates, and Lambda Expressions
        4. Comparing Arrays and Collections
          1. Using Collection Classes to Play Cards
        5. Summary
        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. Summary
        4. Chapter 19 Quick Reference
      6. 20. Decoupling Application Logic and Handling Events
        1. Understanding Delegates
          1. Examples of Delegates in the .NET Framework Class Library
          2. The Automated Factory Scenario
          3. Implementing the Factory Control System Without Using Delegates
          4. Implementing the Factory by Using a Delegate
          5. Declaring and Using Delegates
        2. Lambda Expressions and Delegates
          1. Creating a Method Adapter
          2. The Forms 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 User Interface Events
          1. Using Events
        5. Summary
        6. Chapter 20 Quick Reference
      7. 21. 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. Summary
        4. Chapter 21 Quick Reference
      8. 22. Operator Overloading
        1. Understanding Operators
          1. Operator Constraints
          2. Overloaded Operators
          3. Creating Symmetric Operators
            1. Operators and Language Interoperability
        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. Summary
        9. Chapter 22 Quick Reference
    8. IV. Building Professional Windows 8 Applications with C#
      1. 23. Improving Throughput by Using Tasks
        1. Why Perform Multitasking by Using Parallel Processing?
          1. The Rise of the Multicore Processor
        2. Implementing Multitasking with the .NET Framework
          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
        3. Canceling Tasks and Handling Exceptions
          1. The Mechanics of Cooperative Cancellation
          2. Using Continuations with Canceled and Faulted Tasks
        4. Summary
        5. Chapter 23 Quick Reference
      2. 24. Improving Response Time by Performing Asynchronous Operations
        1. Implementing Asynchronous Methods
          1. Defining Asynchronous Methods: The Problem
          2. Defining Asynchronous Methods: The Solution
          3. Defining Asynchronous Methods That Return Values
          4. Asynchronous Methods and the Windows Runtime APIs
        2. Using PLINQ to Parallelize Declarative Data Access
          1. Using PLINQ to Improve Performance While Iterating Through a Collection
          2. Canceling a PLINQ Query
        3. Synchronizing Concurrent Access to Data
          1. Locking Data
          2. Synchronization Primitives for Coordinating Tasks
          3. Cancelling Synchronization
          4. The Concurrent Collection Classes
          5. Using a Concurrent Collection and a Lock to Implement Thread-Safe Data Access
        4. Summary
        5. Quick Reference
      3. 25. Implementing the User Interface for a Windows Store App
        1. What Is a Windows Store App?
        2. Using the Blank App Template to Build a Windows Store App
          1. Implementing a Scalable User Interface
            1. Implementing a Tabular Layout with a Grid Control
            2. Adapting the Layout by Using the Visual State Manager
          2. Applying Styles to a User Interface
        3. Summary
        4. Chapter 25 Quick Reference
      4. 26. Displaying and Searching for Data in a Windows Store App
        1. Implementing the Model-View-ViewModel Pattern
          1. Displaying Data by Using Data Binding
          2. Modifying Data by Using Data Binding
          3. Using Data Binding with a ComboBox Control
          4. Creating a ViewModel
          5. Adding Commands to a ViewModel
        2. Windows 8 Contracts
          1. Implementing the Search Contract
          2. Navigating to a Selected Item
          3. Starting an Application from the Search Charm
        3. Summary
        4. Chapter 26 Quick Reference
      5. 27. Accessing a Remote Database in a Windows Store App
        1. Retrieving Data from a Database
          1. Creating an Entity Model
          2. Creating and Using a Data Service
        2. Inserting, Updating, and Deleting Data in a Database
          1. Performing Insert, Update, and Delete Operations Through a WCF Data Service
          2. Reporting Errors and Updating the User Interface
        3. Summary
        4. Chapter 27 Quick Reference
    9. A. About the Author
    10. Index
    11. About the Author
    12. Copyright