Microsoft Visual C# Step by Step Ninth Edition John Sharp Microsoft Visual C# Step by Step, Ninth Edition Published with the authorization of Microsoft Corporation by: Pearson Education, Inc. Copyright © 2018 by Pearson Education, Inc. All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms, and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. ISBN-13: 978-1-5093-0776-0 ISBN-10: 1-5093-0776-1 Library of Congress Control Number: 2018944197 1 18 Trademarks Microsoft and the trademarks listed at http://www.microsoft.com on the “Trademarks” webpage are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author, the publisher, and Microsoft Corporation shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Special Sales For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions about sales outside the U.S., please contact [email protected]. Editor-in-Chief Brett Bartow Acquisitions Editor Trina MacDonald Development Editor Rick Kughen Managing Editor Sandra Schroeder Senior Project Editor Tracey Croom Copy Editor Christopher Morris Indexer Erika Millen Proofreader Jeanine Furino Technical Editor David Franson Editorial Assistant Courtney Martin Cover Designer Twist Creative, Seattle Compositor codemantra Contents at a Glance Acknowledgments About the Author Introduction PART I INTRODUCING MICROSOFT VISUAL C# AND MICROSOFT VISUAL STUDIO 2017 CHAPTER 1 Welcome to C# CHAPTER 2 Working with variables, operators, and expressions CHAPTER 3 Writing methods and applying scope CHAPTER 4 Using decision statements CHAPTER 5 Using compound assignment and iteration statements CHAPTER 6 Managing errors and exceptions PART II UNDERSTANDING THE C# OBJECT MODEL CHAPTER 7 Creating and managing classes and objects CHAPTER 8 Understanding values and references CHAPTER 9 Creating value types with enumerations and structures CHAPTER 10 Using arrays CHAPTER 11 Understanding parameter arrays CHAPTER 12 Working with inheritance CHAPTER 13 Creating interfaces and defining abstract classes CHAPTER 14 Using garbage collection and resource management PART III DEFINING EXTENSIBLE TYPES WITH C# CHAPTER 15 Implementing properties to access fields CHAPTER 16 Handling binary data and using indexers CHAPTER 17 Introducing generics CHAPTER 18 Using collections CHAPTER 19 Enumerating collections CHAPTER 20 Decoupling application logic and handling events CHAPTER 21 Querying in-memory data by using query expressions CHAPTER 22 Operator overloading PART IV BUILDING UNIVERSAL WINDOWS PLATFORM APPLICATIONS WITH C# CHAPTER 23 Improving throughput by using tasks CHAPTER 24 Improving response time by performing asynchronous operations CHAPTER 25 Implementing the user interface for a Universal Windows Platform app CHAPTER 26 Displaying and searching for data in a Universal Windows Platform app CHAPTER 27 Accessing a remote database from a Universal Windows Platform app Index Contents Acknowledgments About the Author Introduction PART I INTRODUCING MICROSOFT VISUAL C# AND MICROSOFT VISUAL STUDIO 2017 Chapter 1 Welcome to C# Beginning programming with the Visual Studio 2017 environment Writing your first program Using namespaces Creating a graphical application Examining the Universal Windows Platform app Adding code to the graphical application Summary Quick reference Chapter 2 Working with variables, operators, and expressions Understanding statements Using identifiers Identifying keywords Using variables Naming variables Declaring variables Specifying numeric values Working with primitive data types Unassigned local variables Displaying primitive data type values Using arithmetic operators Operators and types Examining arithmetic operators Controlling precedence Using associativity to evaluate expressions Associativity and the assignment operator Incrementing and decrementing variables Prefix and postfix Declaring implicitly typed local variables Summary Quick reference Chapter 3 Writing methods and applying scope Creating methods Declaring a method Returning data from a method Using expression-bodied methods Calling methods Specifying the method call syntax Returning multiple values from a method Applying scope Defining local scope Defining class scope Overloading methods Writing methods Refactoring code Nesting methods Using optional parameters and named arguments Defining optional parameters Passing named arguments Resolving ambiguities with optional parameters and named arguments Summary Quick reference Chapter 4 Using decision statements Declaring Boolean variables Using Boolean operators Understanding equality and relational operators Understanding conditional logical operators Short-circuiting Summarizing operator precedence and associativity Using if statements to make decisions Understanding if statement syntax Using blocks to group statements Cascading if statements Using switch statements Understanding switch statement syntax Following the switch statement rules Summary Quick reference Chapter 5 Using compound assignment and iteration statements Using compound assignment operators Writing while statements Writing for statements Understanding for statement scope Writing do statements Summary Quick reference Chapter 6 Managing errors and exceptions Coping with errors Trying code and catching exceptions Unhandled exceptions Using multiple catch handlers Catching multiple exceptions Filtering exceptions Propagating exceptions Using checked and unchecked integer arithmetic Writing checked statements Writing checked expressions Throwing exceptions Using throw exceptions Using a finally block Summary Quick reference PART II UNDERSTANDING THE C# OBJECT MODEL Chapter 7 Creating and managing classes and objects Understanding classification The purpose of encapsulation Defining and using a class Controlling accessibility