logo资料库

Microsoft Visual C# Step by Step, 8th Edition.pdf

第1页 / 共1968页
第2页 / 共1968页
第3页 / 共1968页
第4页 / 共1968页
第5页 / 共1968页
第6页 / 共1968页
第7页 / 共1968页
第8页 / 共1968页
资料共1968页,剩余部分请下载后查看
Title Page
Copyright Page
Contents at a glance
Contents
Introduction
Who should read this book
Who should not read this book
Organization of this book
Finding your best starting point in this book
Conventions and features in this book
System requirements
Code samples
Installing the code samples
Using the code samples
Acknowledgments
Errata and book support
We want to hear from you
Stay in touch
Part I: Introducing Microsoft Visual C# and Microsoft Visual Studio 2015
Chapter 1. Welcome to C#
Beginning programming with the Visual Studio 2015 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
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
Applying scope
Defining local scope
Defining class scope
Overloading methods
Writing 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
Propagating exceptions
Using checked and unchecked integer arithmetic
Writing checked statements
Writing checked expressions
Throwing 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
Working with constructors
Overloading constructors
Understanding static methods and data
Creating a shared field
Creating a static field by using the const keyword
Understanding static classes
Static using statements
Anonymous classes
Summary
Quick reference
Chapter 8. Understanding values and references
Copying value type variables and classes
Understanding null values and nullable types
Using nullable types
Understanding the properties of nullable types
Using ref and out parameters
Creating ref parameters
Creating out parameters
How computer memory is organized
Using the stack and the heap
The System.Object class
Boxing
Unboxing
Casting data safely
The is operator
The as operator
Summary
Quick reference
Chapter 9. Creating value types with enumerations and structures
Working with enumerations
Declaring an enumeration
Using an enumeration
Choosing enumeration literal values
Choosing an enumeration’s underlying type
Working with structures
Declaring a structure
Understanding differences between structures and classes
Declaring structure variables
Understanding structure initialization
Copying structure variables
Summary
Quick reference
Chapter 10. Using arrays
Declaring and creating an array
Declaring array variables
Creating an array instance
Populating and using an array
Creating an implicitly typed array
Accessing an individual array element
Iterating through an array
Passing arrays as parameters and return values for a method
Copying arrays
Using multidimensional arrays
Creating jagged arrays
Summary
Quick reference
Chapter 11. Understanding parameter arrays
Overloading—a recap
Using array arguments
Declaring a params array
Using params object[]
Using a params array
Comparing parameter arrays and optional parameters
Summary
Quick reference
Chapter 12. Working with inheritance
What is inheritance?
Using inheritance
The System.Object class revisited
Calling base-class constructors
Assigning classes
Declaring new methods
Declaring virtual methods
Declaring override methods
Understanding protected access
Understanding extension methods
Summary
Quick reference
Chapter 13. Creating interfaces and defining abstract classes
Understanding interfaces
Defining an interface
Implementing an interface
Referencing a class through its interface
Working with multiple interfaces
Explicitly implementing an interface
Interface restrictions
Defining and using interfaces
Abstract classes
Abstract methods
Sealed classes
Sealed methods
Implementing and using an abstract class
Summary
Quick reference
Chapter 14. Using garbage collection and resource management
The life and times of an object
Writing destructors
Why use the garbage collector?
How does the garbage collector work?
Recommendations
Resource management
Disposal methods
Exception-safe disposal
The using statement and the IDisposable interface
Calling the Dispose method from a destructor
Implementing exception-safe disposal
Summary
Quick reference
Part III: Defining extensible types with C#
Chapter 15. Implementing properties to access fields
Implementing encapsulation by using methods
What are properties?
Using properties
Read-only properties
Write-only properties
Property accessibility
Understanding the property restrictions
Declaring interface properties
Replacing methods with properties
Generating automatic properties
Initializing objects by using properties
Summary
Quick reference
Chapter 16. Using indexers
What is an indexer?
An example that doesn’t use indexers
The same example using indexers
Understanding indexer accessors
Comparing indexers and arrays
Indexers in interfaces
Using indexers in a Windows application
Summary
Quick reference
Chapter 17. Introducing generics
The problem with the object type
The generics solution
Generics vs. generalized classes
Generics and constraints
Creating a generic class
The theory of binary trees
Building a binary tree class by using generics
Creating a generic method
Defining a generic method to build a binary tree
Variance and generic interfaces
Covariant interfaces
Contravariant interfaces
Summary
Quick reference
Chapter 18. Using collections
What are collection classes?
The List collection class
The LinkedList collection class
The Queue collection class
The Stack collection class
The Dictionary collection class
The SortedList collection class
The HashSet collection class
Using collection initializers
The Find methods, predicates, and lambda expressions
The forms of lambda expressions
Comparing arrays and collections
Using collection classes to play cards
Summary
Quick reference
Chapter 19. Enumerating collections
Enumerating the elements in a collection
Manually implementing an enumerator
Implementing the IEnumerable interface
Implementing an enumerator by using an iterator
A simple iterator
Defining an enumerator for the Tree class by using an iterator
Summary
Quick reference
Chapter 20. Decoupling application logic and handling events
Understanding delegates
Examples of delegates in the .NET Framework class library
The automated factory scenario
Implementing the factory control system without using delegates
Implementing the factory by using a delegate
Declaring and using delegates
Lambda expressions and delegates
Creating a method adapter
Enabling notifications by using events
Declaring an event
Subscribing to an event
Unsubscribing from an event
Raising an event
Understanding user interface events
Using events
Summary
Quick reference
Chapter 21. Querying in-memory data by using query expressions
What is LINQ?
Using LINQ in a C# application
Selecting data
Filtering data
Ordering, grouping, and aggregating data
Joining data
Using query operators
Querying data in Tree objects
LINQ and deferred evaluation
Summary
Quick reference
Chapter 22. Operator overloading
Understanding operators
Operator constraints
Overloaded operators
Creating symmetric operators
Understanding compound assignment evaluation
Declaring increment and decrement operators
Comparing operators in structures and classes
Defining operator pairs
Implementing operators
Understanding conversion operators
Providing built-in conversions
Implementing user-defined conversion operators
Creating symmetric operators, revisited
Writing conversion operators
Summary
Quick reference
Part IV: Building Universal Windows Platform applications with C#
Chapter 23. Improving throughput by using tasks
Why perform multitasking by using parallel processing?
The rise of the multicore processor
Implementing multitasking by using the Microsoft .NET Framework
Tasks, threads, and the ThreadPool
Creating, running, and controlling tasks
Using the Task class to implement parallelism
Abstracting tasks by using the Parallel class
When not to use the Parallel class
Canceling tasks and handling exceptions
The mechanics of cooperative cancellation
Using continuations with canceled and faulted tasks
Summary
Quick reference
Chapter 24. Improving response time by performing asynchronous operations
Implementing asynchronous methods
Defining asynchronous methods: The problem
Defining asynchronous methods: The solution
Defining asynchronous methods that return values
Asynchronous method gotchas
Asynchronous methods and the Windows Runtime APIs
Using PLINQ to parallelize declarative data access
Using PLINQ to improve performance while iterating through a collection
Canceling a PLINQ query
Synchronizing concurrent access to data
Locking data
Synchronization primitives for coordinating tasks
Canceling synchronization
The concurrent collection classes
Using a concurrent collection and a lock to implement thread-safe data access
Summary
Quick reference
Chapter 25. Implementing the user interface for a Universal Windows Platform app
Features of a Universal Windows Platform app
Using the Blank App template to build a Universal Windows Platform app
Implementing a scalable user interface
Applying styles to a UI
Summary
Quick reference
Chapter 26. Displaying and searching for data in a Universal Windows Platform app
Implementing the Model-View-ViewModel pattern
Displaying data by using data binding
Modifying data by using data binding
Using data binding with a ComboBox control
Creating a ViewModel
Adding commands to a ViewModel
Searching for data using Cortana
Providing a vocal response to voice commands
Summary
Quick reference
Chapter 27. Accessing a remote database from a Universal Windows Platform app
Retrieving data from a database
Creating an entity model
Creating and using a REST web service
Inserting, updating, and deleting data through a REST web service
Reporting errors and updating the UI
Summary
Quick reference
Index
About the author
Visit us today at microsoftpressstore.com
Wait, there’s more...
Free ebooks
Survey
Code Snippets
Microsoft Visual C# Step by Step, 8th Edition JOHN SHARP
PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2015 by CM Group, Ltd. All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2015940217 ISBN: 978-1-5093-0104-1 Printed and bound in the United States of America. First Printing Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Support at mspinput@microsoft.com. Please tell us what you think of this book at http://aka.ms/tellpress. This book is provided “as-is” and expresses the author’s views and opinions. The views, opinions and information expressed in this book, including URL and other Internet website references, may change without notice. Some examples depicted herein are provided for illustration only and are fctitious. No real association or connection is intended or should be inferred. Microsoft and the trademarks listed at www.microsoft.com on the “Trademarks” webpage are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. Acquisitions and Developmental Editor: Devon Musgrave Project Editor: John Pierce Editorial Production: Rob Nance and John Pierce Technical Reviewer: Marc Young Copyeditor: John Pierce Indexer: Christina Yeager, Emerald Editorial Services Cover: Twist Creative • Seattle and Joel Panchot
Contents at a glance Introduction PART I INTRODUCING MICROSOFT VISUAL C# AND MICROSOFT VISUAL STUDIO 2015 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 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 Introduction PART I INTRODUCING MICROSOFT VISUAL C# AND MICROSOFT VISUAL STUDIO 2015 Chapter 1 Welcome to C# Beginning programming with the Visual Studio 2015 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 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 Applying scope Defining local scope Defining class scope Overloading methods Writing 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 Propagating exceptions Using checked and unchecked integer arithmetic Writing checked statements Writing checked expressions Throwing 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 Working with constructors
分享到:
收藏