You are previewing C# 2012 for Programmers, Fifth Edition.
O'Reilly logo
C# 2012 for Programmers, Fifth Edition

Book Description

The professional programmer’s Deitel® guide to  C# 2012 and object-oriented development for  Windows® 7 and Windows® 8

Written for programmers with a background in high-level language programming, this book applies the Deitel signature live-code approach to teaching programming and explores Microsoft’s C# 2012 and .NET 4.5 in depth. The book presents the concepts in the context of fully tested apps, complete with syntax shading,  code highlighting, code walkthroughs and program outputs. You’ll work through 200+ complete C# apps with 15,000+ lines of proven C# code and hundreds of savvy software-development tips.

Start with an introduction to C# using an early classes and objects approach, then rapidly move on to more advanced topics, including LINQ, asynchronous programming with async and await,  Windows® 8 UI and WPF graphics and multimedia, web services, Windows® Phone 8, Windows Azure™ and more. You’ll enjoy the treatment of object-oriented programming and an OOD/UML® ATM case study, including a complete C# implementation. When you’re finished, you’ll have everything you need to build industrial-strength, object-oriented C# apps.

Paul Deitel and Harvey Deitel are the founders of Deitel & Associates, Inc.,  the internationally recognized programming languages authoring and corporate-training organization. Millions of people worldwide have used Deitel books, LiveLessons video training and online resource centers to master C#, .NET, Visual Basic®, C++, Visual C++®, C, Java™, Android™ app development, iOS® app development, Internet and web programming, JavaScript®, XML, Perl®, Python and more.

Practical, example-rich coverage of:

•            .NET, Types, Arrays, Exception Handling

•            LINQ, Object/Collection Initializers

•            Objects, Classes, Inheritance,  Polymorphism

•            Industrial-Strength, C#-Based Object-Oriented Design/UML® ATM Case Study

•            WinForms, WPF, Windows® 8 UI, XAML, Event Handling, Visual C# Debugging

•            WPF and Windows® 8 Graphics  and Multimedia

•            Windows® Phone 8, Cloud Computing with Windows Azure™, Asynchronous Programming with async and await

•            Generic Collections, Methods and Classes

•            XML, LINQ to XML and LINQ to Entities

•            ASP.NET and ASP.NET Ajax

•            Web Forms, Web Controls

•            WCF REST-Based Web Services and more.

Visit www.deitel.com

  • For information on Deitel’s Dive Into® Series  programming training courses delivered at organizations worldwide visit www.deitel.com/training or write to deitel@deitel.com

  • Download code examples

  • Join the Deitel social networking communities on Facebook®  at facebook.com/DeitelFan, Twitter® @deitel, Google+™ at  gplus.to/deitel and LinkedIn®  at bit.ly/DeitelLinkedIn.

  • To receive updates for this book, subscribe to the Deitel® Buzz Online e-mail newsletter at www.deitel.com/newsletter/ subscribe.html

  • Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents
    6. Preface
      1. Visual C#® 2012, the Visual Studio® 2012 IDE, .NET 4.5, Windows® 7 and Windows® 8
      2. Object-Oriented Programming
      3. Other Features
      4. Training Approach
      5. Obtaining the Software Used in This Book
      6. C# 2012 Fundamentals: Parts I, II, III and IV, Second Edition LiveLessons Video Training
      7. Acknowledgments
      8. About the Authors
      9. Deitel® Dive-Into® Series Corporate Training
    7. Before You Begin
      1. Font and Naming Conventions
      2. Software
      3. Hardware and Software Requirements for the Visual Studio 2012 Express Editions
      4. Viewing File Extensions
      5. Obtaining the Code Examples
      6. Visual Studio Theme
      7. Displaying Line Numbers and Configuring Tabs
      8. Miscellaneous Notes
    8. 1. Introduction
      1. 1.1. Introduction
      2. 1.2. Object Technology
      3. 1.3. C#
      4. 1.4. Microsoft’s .NET
      5. 1.5. Microsoft’s Windows® Operating System
      6. 1.6. Windows Phone 8 for Smartphones
      7. 1.7. Windows Azure™ and Cloud Computing
      8. 1.8. Visual Studio Express 2012 Integrated Development Environment
      9. 1.9. Painter Test-Drive in Visual Studio Express 2012 for Windows Desktop
      10. 1.10. Painter Test-Drive in Visual Studio Express 2012 for Windows 8
    9. 2. Dive Into® Visual Studio Express 2012 for Windows Desktop
      1. 2.1. Introduction
      2. 2.2. Overview of the Visual Studio Express 2012 IDE
      3. 2.3. Menu Bar and Toolbar
      4. 2.4. Navigating the Visual Studio IDE
      5. 2.5. Using Help
      6. 2.6. Using Visual App Development to Create a Simple App that Displays Text and an Image
      7. 2.7. Wrap-Up
      8. 2.8. Web Resources
    10. 3. Introduction to C# Apps
      1. 3.1. Introduction
      2. 3.2. A Simple C# App: Displaying a Line of Text
      3. 3.3. Creating a Simple App in Visual Studio
      4. 3.4. Modifying Your Simple C# App
      5. 3.5. Formatting Text with Console.Write and Console.WriteLine
      6. 3.6. Another C# App: Adding Integers
      7. 3.7. Arithmetic
      8. 3.8. Decision Making: Equality and Relational Operators
      9. 3.9. Wrap-Up
    11. 4. Introduction to Classes, Objects, Methods and strings
      1. 4.1. Introduction
      2. 4.2. Classes, Objects, Methods, Properties and Instance Variables
      3. 4.3. Declaring a Class with a Method and Instantiating an Object of a Class
      4. 4.4. Declaring a Method with a Parameter
      5. 4.5. Instance Variables and Properties
      6. 4.6. UML Class Diagram with a Property
      7. 4.7. Software Engineering with Properties and set and get Accessors
      8. 4.8. Auto-Implemented Properties
      9. 4.9. Value Types vs. Reference Types
      10. 4.10. Initializing Objects with Constructors
      11. 4.11. Floating-Point Numbers and Type decimal
      12. 4.12. Wrap-Up
    12. 5. Control Statements: Part 1
      1. 5.1. Introduction
      2. 5.2. Control Structures
      3. 5.3. if Single-Selection Statement
      4. 5.4. if...else Double-Selection Statement
      5. 5.5. while Repetition Statement
      6. 5.6. Counter-Controlled Repetition
      7. 5.7. Sentinel-Controlled Repetition
      8. 5.8. Nested Control Statements
      9. 5.9. Compound Assignment Operators
      10. 5.10. Increment and Decrement Operators
      11. 5.11. Simple Types
      12. 5.12. Wrap-Up
    13. 6. Control Statements: Part 2
      1. 6.1. Introduction
      2. 6.2. Essentials of Counter-Controlled Repetition
      3. 6.3. for Repetition Statement
      4. 6.4. Examples Using the for Statement
      5. 6.5. do...while Repetition Statement
      6. 6.6. switch Multiple-Selection Statement
      7. 6.7. break and continue Statements
      8. 6.8. Logical Operators
      9. 6.9. Wrap-Up
    14. 7. Methods: A Deeper Look
      1. 7.1. Introduction
      2. 7.2. Packaging Code in C#
      3. 7.3. static Methods, static Variables and Class Math
      4. 7.4. Declaring Methods with Multiple Parameters
      5. 7.5. Notes on Declaring and Using Methods
      6. 7.6. Method-Call Stack and Activation Records
      7. 7.7. Argument Promotion and Casting
      8. 7.8. The .NET Framework Class Library
      9. 7.9. Case Study: Random-Number Generation
      10. 7.10. Case Study: A Game of Chance; Introducing Enumerations
      11. 7.11. Scope of Declarations
      12. 7.12. Method Overloading
      13. 7.13. Optional Parameters
      14. 7.14. Named Parameters
      15. 7.15. Recursion
      16. 7.16. Passing Arguments: Pass-by-Value vs. Pass-by-Reference
      17. 7.17. Wrap-Up
    15. 8. Arrays; Introduction to Exception Handling
      1. 8.1. Introduction
      2. 8.2. Arrays
      3. 8.3. Declaring and Creating Arrays
      4. 8.4. Examples Using Arrays
      5. 8.5. Case Study: Card Shuffling and Dealing Simulation
      6. 8.6. foreach Statement
      7. 8.7. Passing Arrays and Array Elements to Methods
      8. 8.8. Passing Arrays by Value and by Reference
      9. 8.9. Case Study: GradeBook Using an Array to Store Grades
      10. 8.10. Multidimensional Arrays
      11. 8.11. Case Study: GradeBook Using a Rectangular Array
      12. 8.12. Variable-Length Argument Lists
      13. 8.13. Using Command-Line Arguments
      14. 8.14. Wrap-Up
    16. 9. Introduction to LINQ and the List Collection
      1. 9.1. Introduction
      2. 9.2. Querying an Array of int Values Using LINQ
      3. 9.3. Querying an Array of Employee Objects Using LINQ
      4. 9.4. Introduction to Collections
      5. 9.5. Querying a Generic Collection Using LINQ
      6. 9.6. Wrap-Up
      7. 9.7. Deitel LINQ Resource Center
    17. 10. Classes and Objects: A Deeper Look
      1. 10.1. Introduction
      2. 10.2. Time Class Case Study
      3. 10.3. Controlling Access to Members
      4. 10.4. Referring to the Current Object’s Members with the this Reference
      5. 10.5. Time Class Case Study: Overloaded Constructors
      6. 10.6. Default and Parameterless Constructors
      7. 10.7. Composition
      8. 10.8. Garbage Collection and Destructors
      9. 10.9. static Class Members
      10. 10.10. readonly Instance Variables
      11. 10.11. Data Abstraction and Encapsulation
      12. 10.12. Class View and Object Browser
      13. 10.13. Object Initializers
      14. 10.14. Wrap-Up
    18. 11. Object-Oriented Programming: Inheritance
      1. 11.1. Introduction
      2. 11.2. Base Classes and Derived Classes
      3. 11.3. protected Members
      4. 11.4. Relationship between Base Classes and Derived Classes
      5. 11.5. Constructors in Derived Classes
      6. 11.6. Software Engineering with Inheritance
      7. 11.7. Class object
      8. 11.8. Wrap-Up
    19. 12. OOP: Polymorphism, Interfaces and Operator Overloading
      1. 12.1. Introduction
      2. 12.2. Polymorphism Examples
      3. 12.3. Demonstrating Polymorphic Behavior
      4. 12.4. Abstract Classes and Methods
      5. 12.5. Case Study: Payroll System Using Polymorphism
      6. 12.6. sealed Methods and Classes
      7. 12.7. Case Study: Creating and Using Interfaces
      8. 12.8. Operator Overloading
      9. 12.9. Wrap-Up
    20. 13. Exception Handling: A Deeper Look
      1. 13.1. Introduction
      2. 13.2. Example: Divide by Zero without Exception Handling
      3. 13.3. Example: Handling DivideByZeroExceptions and FormatExceptions
      4. 13.4. .NET Exception Hierarchy
      5. 13.5. finally Block
      6. 13.6. The using Statement
      7. 13.7. Exception Properties
      8. 13.8. User-Defined Exception Classes
      9. 13.9. Wrap-Up
    21. 14. Graphical User Interfaces with Windows Forms: Part 1
      1. 14.1. Introduction
      2. 14.2. Windows Forms
      3. 14.3. Event Handling
      4. 14.4. Control Properties and Layout
      5. 14.5. Labels, TextBoxes and Buttons
      6. 14.6. GroupBoxes and Panels
      7. 14.7. CheckBoxes and RadioButtons
      8. 14.8. PictureBoxes
      9. 14.9. ToolTips
      10. 14.10. NumericUpDown Control
      11. 14.11. Mouse-Event Handling
      12. 14.12. Keyboard-Event Handling
      13. 14.13. Wrap-Up
    22. 15. Graphical User Interfaces with Windows Forms: Part 2
      1. 15.1. Introduction
      2. 15.2. Menus
      3. 15.3. MonthCalendar Control
      4. 15.4. DateTimePicker Control
      5. 15.5. LinkLabel Control
      6. 15.6. ListBox Control
      7. 15.7. CheckedListBox Control
      8. 15.8. ComboBox Control
      9. 15.9. TreeView Control
      10. 15.10. ListView Control
      11. 15.11. TabControl Control
      12. 15.12. Multiple Document Interface (MDI) Windows
      13. 15.13. Visual Inheritance
      14. 15.14. User-Defined Controls
      15. 15.15. Wrap-Up
    23. 16. Strings and Characters: A Deeper Look
      1. 16.1. Introduction
      2. 16.2. Fundamentals of Characters and Strings
      3. 16.3. string Constructors
      4. 16.4. string Indexer, Length Property and CopyTo Method
      5. 16.5. Comparing strings
      6. 16.6. Locating Characters and Substrings in strings
      7. 16.7. Extracting Substrings from strings
      8. 16.8. Concatenating strings
      9. 16.9. Miscellaneous string Methods
      10. 16.10. Class StringBuilder
      11. 16.11. Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder
      12. 16.12. Append and AppendFormat Methods of Class StringBuilder
      13. 16.13. Insert, Remove and Replace Methods of Class StringBuilder
      14. 16.14. Char Methods
      15. 16.15. (Online) Introduction to Regular Expression
      16. 16.16. Wrap-Up
    24. 17. Files and Streams
      1. 17.1. Introduction
      2. 17.2. Data Hierarchy
      3. 17.3. Files and Streams
      4. 17.4. Classes File and Directory
      5. 17.5. Creating a Sequential-Access Text File
      6. 17.6. Reading Data from a Sequential-Access Text File
      7. 17.7. Case Study: Credit Inquiry Program
      8. 17.8. Serialization
      9. 17.9. Creating a Sequential-Access File Using Object Serialization
      10. 17.10. Reading and Deserializing Data from a Binary File
      11. 17.11. Wrap-Up
    25. 18. Generics
      1. 18.1. Introduction
      2. 18.2. Motivation for Generic Methods
      3. 18.3. Generic-Method Implementation
      4. 18.4. Type Constraints
      5. 18.5. Overloading Generic Methods
      6. 18.6. Generic Classes
      7. 18.7. Wrap-Up
    26. 19. Collections
      1. 19.1. Introduction
      2. 19.2. Collections Overview
      3. 19.3. Class Array and Enumerators
      4. 19.4. Nongeneric Collections
      5. 19.5. Generic Collections
      6. 19.6. Covariance and Contravariance for Generic Types
      7. 19.7. Wrap-Up
    27. 20. Databases and LINQ
      1. 20.1. Introduction
      2. 20.2. Relational Databases
      3. 20.3. A Books Database
      4. 20.4. LINQ to Entities and the ADO.NET Entity Framework
      5. 20.5. Querying a Database with LINQ
      6. 20.6. Dynamically Binding Query Results
      7. 20.7. Retrieving Data from Multiple Tables with LINQ
      8. 20.8. Creating a Master/Detail View App
      9. 20.9. Address Book Case Study
      10. 20.10. Tools and Web Resources
      11. 20.11. Wrap-Up
    28. 21. Web App Development with ASP.NET
      1. 21.1. Introduction
      2. 21.2. Web Basics
      3. 21.3. Multitier App Architecture
      4. 21.4. Your First Web App
      5. 21.5. Standard Web Controls: Designing a Form
      6. 21.6. Validation Controls
      7. 21.7. Session Tracking
      8. 21.8. Case Study: Database-Driven ASP.NET Guestbook
      9. 21.9. Case Study: ASP.NET AJAX
      10. 21.10. Case Study: Password-Protected Books Database App
      11. 21.11. Wrap-Up
    29. 22. XML and LINQ to XML
      1. 22.1. Introduction
      2. 22.2. XML Basics
      3. 22.3. Structuring Data
      4. 22.4. XML Namespaces
      5. 22.5. Document Type Definitions (DTDs)
      6. 22.6. W3C XML Schema Documents
      7. 22.7. Extensible Stylesheet Language and XSL Transformations
      8. 22.8. LINQ to XML: Document Object Model (DOM)
      9. 22.9. LINQ to XML Class Hierarchy
      10. 22.10. LINQ to XML: Namespaces and Creating Documents
      11. 22.11. XSLT with Class XslCompiledTransform
      12. 22.12. Wrap-Up
    30. 23. Windows 8 UI and XAML
      1. 23.1. Introduction
      2. 23.2. Welcome App: Introduction to XAML Declarative GUI Programming
      3. 23.3. Painter App: Layouts; Event Handling
      4. 23.4. CoverViewer App: Data Binding, Data Templates and Styles
      5. 23.5. App Lifecycle
      6. 23.6. Wrap-Up
    31. 24. Windows 8 Graphics and Multimedia
      1. 24.1. Introduction
      2. 24.2. Basic Shapes
      3. 24.3. Polylines and Polygons
      4. 24.4. SolidColorBrushes and ImageBrushes
      5. 24.5. GradientBrushes
      6. 24.6. Transforms
      7. 24.7. Windows 8 Customization: A Television GUI
      8. 24.8. Wrap-Up
    32. 25. Building a Windows Phone 8 App
      1. 25.1. Introduction
      2. 25.2. Downloading the Windows Phone 8 SDK
      3. 25.3. Tip Calculator App Introduction
      4. 25.4. Test-Driving the Tip Calculator App
      5. 25.5. Tip Calculator Technologies Overview
      6. 25.6. Building the App’s GUI
      7. 25.7. Adding Functionality to the App with C#
      8. 25.8. WMAppManifest.xml
      9. 25.9. Windows Phone Dev Center
      10. 25.10. Selling Your Apps in the Windows Phone Marketplace
      11. 25.11. Other Popular Mobile App Platforms
      12. 25.12. Developer Documentation
      13. 25.13. Additional Windows Phone 8 Resources
      14. 25.14. Wrap-Up
    33. 26. Asynchronous Programming with async and await
      1. 26.1. Introduction
      2. 26.2. Basics of async and await
      3. 26.3. Executing an Asynchronous Task from a GUI App
      4. 26.4. Sequential Execution of Two Compute-Intensive Tasks
      5. 26.5. Asynchronous Execution of Two Compute-Intensive Tasks
      6. 26.6. Invoking a Flickr Web Service Asynchronously with WebClient
      7. 26.7. Wrap-Up
    34. 27. Web App Development with ASP.NET: A Deeper Look
      1. 27.1. Introduction
      2. 27.2. Case Study: Password-Protected Books Database App
      3. 27.3. ASP.NET Ajax
      4. 27.4. Wrap-Up
    35. 28. Web Services
      1. 28.1. Introduction
      2. 28.2. WCF Services Basics
      3. 28.3. HTTP get and post Requests
      4. 28.4. Representational State Transfer (REST)
      5. 28.5. JavaScript Object Notation (JSON)
      6. 28.6. Publishing and Consuming REST-Based XML Web Services
      7. 28.7. Publishing and Consuming REST-Based JSON Web Services
      8. 28.8. Equation Generator: Returning User-Defined Types
      9. 28.9. Wrap-Up
    36. 29. Building a Windows Azure™ Cloud Computing App
      1. 29.1. Introduction
      2. 29.2. Installing the Windows Azure SDK for Visual Studio 2012
      3. 29.3. Windows Azure Cloud Services Accounts
      4. 29.4. Favorite Twitter Searches: Introduction
      5. 29.5. Favorite Twitter Searches: Test-Drive
      6. 29.6. Favorite Twitter Searches: Technologies Overview
      7. 29.7. Favorite Twitter Searches: Code
      8. 29.8. Security, Privacy and Reliability
      9. 29.9. Microsoft Windows Azure Resources
      10. 29.10. Microsoft Windows Azure Code Samples
      11. 29.11. Additional Web Resources
      12. 29.12. Wrap-Up
    37. 30. GUI with Windows Presentation Foundation
      1. 30.1. Introduction
      2. 30.2. Windows Presentation Foundation (WPF)
      3. 30.3. Declarative GUI Programming Using XAML
      4. 30.4. Creating a WPF App
      5. 30.5. Laying Out Controls
      6. 30.6. Event Handling
      7. 30.7. Commands and Common Application Tasks
      8. 30.8. WPF GUI Customization
      9. 30.9. Using Styles to Change the Appearance of Controls
      10. 30.10. Customizing Windows
      11. 30.11. Defining a Control’s Appearance with Control Templates
      12. 30.12. Data-Driven GUIs with Data Binding
      13. 30.13. Wrap-Up
    38. 31. WPF Graphics and Multimedia
      1. 31.1. Introduction
      2. 31.2. Controlling Fonts
      3. 31.3. Basic Shapes
      4. 31.4. Polygons and Polylines
      5. 31.5. Brushes
      6. 31.6. Transforms
      7. 31.7. WPF Customization: A Television GUI
      8. 31.8. Animations
      9. 31.9. Speech Synthesis and Speech Recognition
      10. 31.10. Wrap-Up
    39. 32. ATM Case Study, Part 1: Object-Oriented Design with the UML
      1. 32.1. Introduction
      2. 32.2. Examining the ATM Requirements Document
      3. 32.3. Identifying the Classes in the ATM Requirements Document
      4. 32.4. Identifying Class Attributes
      5. 32.5. Identifying Objects’ States and Activities
      6. 32.6. Identifying Class Operations
      7. 32.7. Identifying Collaboration Among Objects
      8. 32.8. Wrap-Up
    40. 33. ATM Case Study, Part 2: Implementing an OO Design
      1. 33.1. Introduction
      2. 33.2. Starting to Program the Classes of the ATM System
      3. 33.3. Incorporating Inheritance and Polymorphism into the ATM System
      4. 33.4. ATM Case Study Implementation
      5. 33.5. Wrap-Up
    41. A. Operator Precedence Chart
    42. B. Simple Types
      1. Additional Simple Type Information
    43. C. ASCII Character Set
    44. D. Number Systems
      1. D.1. Introduction
      2. D.2. Abbreviating Binary Numbers as Octal and Hexadecimal Numbers
      3. D.3. Converting Octal and Hexadecimal Numbers to Binary Numbers
      4. D.4. Converting from Binary, Octal or Hexadecimal to Decimal
      5. D.5. Converting from Decimal to Binary, Octal or Hexadecimal
      6. D.6. Negative Binary Numbers: Two’s Complement Notation
    45. E. UML 2: Additional Diagram Types
      1. E.1. Introduction
      2. E.2. Additional Diagram Types
    46. F. Unicode®
      1. F.1. Introduction
      2. F.2. Unicode Transformation Formats
      3. F.3. Characters and Glyphs
      4. F.4. Advantages/Disadvantages of Unicode
      5. F.5. Using Unicode
      6. F.6. Character Ranges
    47. Index