Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas

Microsoft Visual Studio C#, Notas de estudo de Cultura

Aprenda C# com Visual Studio da Microsoft.

Tipologia: Notas de estudo

2014

Compartilhado em 22/10/2014

marcos-lima-64
marcos-lima-64 🇧🇷

5

(2)

6 documentos

1 / 781

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Table of Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix
Finding Your Best Starting Point in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Conventions and Features in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
Other Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii
Prerelease Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii
Hardware and Software Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxii
Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii
Installing the Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii
Using the Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiii
Uninstalling the Code Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix
Find Additional Content Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxx
Support for This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxx
Questions and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxx
Introducing Microsoft Visual C# and Microsoft Visual Studio 2010
Welcome .to .C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Beginning Programming with the Visual Studio 2010 Environment . . . . . . . . . . 3
Writing Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Using Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Creating a Graphical Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Chapter 1 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Working .with .Variables, .Operators, .and .Expressions . . . . . . . . . . . . . 27
Understanding Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Using Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Identifying Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Working with Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Unassigned Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Displaying Primitive Data Type Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Using Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Operators and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Examining Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Controlling Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Using Associativity to Evaluate Expressions . . . . . . . . . . . . . . . . . . . . . . . .42
Associativity and the Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . .42
Incrementing and Decrementing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Prefix and Postfix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Declaring Implicitly Typed Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Chapter 2 Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Writing .Methods .and .Applying .Scope . . . . . . . . . . . . . . . . . . . . . . . . . 47
Creating Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Declaring a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Returning Data from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
Calling Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe Microsoft Visual Studio C# e outras Notas de estudo em PDF para Cultura, somente na Docsity!

Microsoft

®

Visual C#

®

Step by Step

John Sharp

iii

Contents at a Glance

iv Contents at a Glance

Part IV Building Windows Presentation Foundation

Applications

22 Introducing Windows Presentation Foundation 443

23 Gathering User Input 477

24 Performing Validation 509

Part V Managing Data

25 Querying Information in a Database 535

26 Displaying and Editing Data by Using the Entity

Framework and Data Binding 565

Part VI Building Professional Solutions with

Visual Studio 2010

27 Introducing the Task Parallel Library 599

28 Performing Parallel Data Access 649

29 Creating and Using a Web Service 683

Appendix

Interoperating with Dynamic Languages 717

vi Table of Contents

Part IV Building Windows Presentation Foundation

Table of Contents xv

 - Visual Studio Part I Introducing Microsoft Visual C# and Microsoft - 1 Welcome to C# - 2 Working with Variables, Operators, and Expressions - 3 Writing Methods and Applying Scope - 4 Using Decision Statements - 5 Using Compound Assignment and Iteration Statements - 6 Managing Errors and Exceptions - 7 Creating and Managing Classes and Objects Part II Understanding the C# Language - 8 Understanding Values and References - 9 Creating Value Types with Enumerations and Structures 
  • 10 Using Arrays and Collections
    • 11 Understanding Parameter Arrays
  • 12 Working with Inheritance
    • 13 Creating Interfaces and Defining Abstract Classes
  • 14 Using Garbage Collection and Resource Management
    • 15 Implementing Properties to Access Fields Part III Creating Components
    • 16 Using Indexers
    • 17 Interrupting Program Flow and Handling Events
  • 18 Introducing Generics
    • 19 Enumerating Collections
  • 20 Querying In-Memory Data by Using Query Expressions
    • 21 Operator Overloading
    • Incrementing and Decrementing Variables
      • Prefix and Postfix
    • Declaring Implicitly Typed Local Variables
    • Chapter 2 Quick Reference
  • 3 Writing Methods and Applying Scope
    • Creating Methods
      • Declaring a Method
      • Returning Data from a Method
      • Calling Methods
      • Specifying the Method Call Syntax
    • Applying Scope
      • Defining Local Scope
      • Defining Class Scope
      • Overloading Methods
    • Writing Methods
    • Using Optional Parameters and Named Arguments
      • Defining Optional Parameters
      • Passing Named Arguments
      • Named Arguments Resolving Ambiguities with Optional Parameters and
    • Chapter 3 Quick Reference
  • 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
    • Chapter 4 Quick Reference
  • 5 Using Compound Assignment and Iteration Statements Table of Contents vii
    • Using Compound Assignment Operators
    • Writing while Statements
    • Writing for Statements
      • Understanding for Statement Scope
    • Writing do Statements
    • Chapter 5 Quick Reference
  • 6 Managing Errors and Exceptions
    • Coping with Errors
    • Trying Code and Catching Exceptions
      • Unhandled Exceptions
      • Using Multiple catch Handlers
      • Catching Multiple Exceptions
    • Using Checked and Unchecked Integer Arithmetic
      • Writing Checked Statements
      • Writing Checked Expressions
    • Throwing Exceptions
    • Using a finally Block
    • Chapter 6 Quick Reference
  • 7 Creating and Managing Classes and Objects Part II Understanding the C# Language
    • 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
      • Static Classes
      • Anonymous Classes
    • Chapter 7 Quick Reference
    • 8 Understanding Values and References viii Table of Contents
      • 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
      • Chapter 8 Quick Reference
    • 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 Structure and Class Differences
        • Declaring Structure Variables
        • Understanding Structure Initialization
        • Copying Structure Variables
      • Chapter 9 Quick Reference
  • 10 Using Arrays and Collections - What Is an Array? - Declaring Array Variables - Creating an Array Instance - Initializing Array Variables - Creating an Implicitly Typed Array Table of Contents ix - Accessing an Individual Array Element - Iterating Through an Array - Copying Arrays - Using Multidimensional Arrays - Using Arrays to Play Cards - What Are Collection Classes? - The ArrayList Collection Class - The Queue Collection Class - The Stack Collection Class - The Hashtable Collection Class - The SortedList Collection Class - Using Collection Initializers - Comparing Arrays and Collections - Using Collection Classes to Play Cards - Chapter 10 Quick Reference
    • 11 Understanding Parameter Arrays
      • Using Array Arguments
        • Declaring a params Array
        • Using params object[ ]
        • Using a params Array
      • Comparing Parameters Arrays and Optional Parameters
      • Chapter 11 Quick Reference
  • 12 Working with Inheritance - What Is Inheritance? - Using Inheritance - Calling Base Class Constructors - Assigning Classes - Declaring new Methods - Declaring Virtual Methods - Declaring override Methods - Understanding protected Access - Understanding Extension Methods - Chapter 12 Quick Reference
    • 13 Creating Interfaces and Defining Abstract Classes x Table of Contents
      • 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
      • Chapter 13 Quick Reference
  • 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 - Calling the Dispose Method from a Destructor - Implementing Exception-Safe Disposal - Chapter 14 Quick Reference
    • 15 Implementing Properties to Access Fields Part III Creating Components
      • Implementing Encapsulation by Using Methods
      • What Are Properties?
        • Using Properties
        • Read-Only Properties
      • Write-Only Properties Table of Contents xi
      • Property Accessibility
    • Understanding the Property Restrictions
    • Declaring Interface Properties
      • Using Properties in a Windows Application
    • Generating Automatic Properties
    • Initializing Objects by Using Properties
    • Chapter 15 Quick Reference
  • 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
    • Chapter 16 Quick Reference
  • 17 Interrupting Program Flow and Handling Events
    • Declaring and Using Delegates
      • The Automated Factory Scenario
      • Implementing the Factory Without Using Delegates
      • Implementing the Factory by Using a Delegate
      • Using Delegates
    • Lambda Expressions and Delegates
      • Creating a Method Adapter
      • Using a Lambda Expression as an Adapter
      • The Form of Lambda Expressions
    • Enabling Notifications with Events
      • Declaring an Event
      • Subscribing to an Event
      • Unsubscribing from an Event
      • Raising an Event
    • Understanding WPF User Interface Events
      • Using Events
    • Chapter 17 Quick Reference
  • 18 Introducing Generics xii Table of Contents - The Problem with objects - 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 - Chapter 18 Quick Reference
    • 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
        • by Using an Iterator Defining an Enumerator for the Tree < TItem > Class
      • Chapter 19 Quick Reference
  • 20 Querying In-Memory Data by Using Query Expressions - What Is Language Integrated Query? - 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 - Chapter 20 Quick Reference
    • 21 Operator Overloading Table of Contents xiii
      • 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
      • Chapter 21 Quick Reference
  • 22 Introducing Windows Presentation Foundation Applications - Creating a WPF Application - Building the WPF Application - Adding Controls to the Form - Using WPF Controls - Changing Properties Dynamically - Handling Events in a WPF Form - Processing Events in Windows Forms - Chapter 22 Quick Reference
    • 23 Gathering User Input
      • Menu Guidelines and Style
      • Menus and Menu Events
        • Creating a Menu
        • Handling Menu Events
      • Shortcut Menus
        • Creating Shortcut Menus
      • Windows Common Dialog Boxes xiv Table of Contents
        • Using the SaveFileDialog Class
      • Improving Responsiveness in a WPF Application
      • Chapter 23 Quick Reference
  • 24 Performing Validation - Validating Data - Strategies for Validating User Input - An Example—Order Tickets for Events - Performing Validation by Using Data Binding - Changing the Point at Which Validation Occurs - Chapter 24 Quick Reference
  • 25 Querying Information in a Database Part V Managing Data - Querying a Database by Using ADO NET - The Northwind Database - Creating the Database - Using ADO NET to Query Order Information - Querying a Database by Using LINQ to SQL - Defining an Entity Class - Creating and Running a LINQ to SQL Query - Deferred and Immediate Fetching - Joining Tables and Creating Relationships - Deferred and Immediate Fetching Revisited - Defining a Custom DataContext Class - Using LINQ to SQL to Query Order Information - Chapter 25 Quick Reference
    • Framework and Data Binding 26 Displaying and Editing Data by Using the Entity
      • Using Data Binding with the Entity Framework
      • Using Data Binding to Modify Data
        • Updating Existing Data
        • Handling Conflicting Updates
        • Adding and Deleting Data
      • Chapter 26 Quick Reference
    • Visual Studio Part VI Building Professional Solutions with
  • 27 Introducing the Task Parallel Library - Why Perform Multitasking by Using Parallel Processing? - The Rise of the Multicore Processor - Implementing Multitasking in a Desktop Application - Tasks, Threads, and the ThreadPool - Creating, Running, and Controlling Tasks - Using the Task Class to Implement Parallelism - Abstracting Tasks by Using the Parallel Class - Returning a Value from a Task - Using Tasks and User Interface Threads Together - Canceling Tasks and Handling Exceptions - The Mechanics of Cooperative Cancellation - Handling Task Exceptions by Using the AggregateException Class - Using Continuations with Canceled and Faulted Tasks - Chapter 27 Quick Reference
  • 28 Performing Parallel Data Access - Using PLINQ to Parallelize Declarative Data Access - Collection Using PLINQ to Improve Performance While Iterating Through a - Specifying Options for a PLINQ Query - Canceling a PLINQ Query - Synchronizing Concurrent Imperative Data Access - Locking Data - Synchronization Primitives in the Task Parallel Library - Cancellation and the Synchronization Primitives - The Concurrent Collection Classes - Thread-Safe Data Access Using a Concurrent Collection and a Lock to Implement - Chapter 28 Quick Reference

xvi Table of Contents

29 Creating and Using a Web Service 683

What Is a Web Service? 684 The Role of Windows Communication Foundation 684 Web Service Architectures 684 SOAP Web Services 685 REST Web Services 687 Building Web Services 688 Creating the ProductInformation SOAP Web Service 689 SOAP Web Services, Clients, and Proxies 697 Consuming the ProductInformation SOAP Web Service 698 Creating the ProductDetails REST Web Service 704 Consuming the ProductDetails REST Web Service 711 Chapter 29 Quick Reference 715

Appendix

Interoperating with Dynamic Languages 717

Index 727

Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: www.microsoft.com/learning/booksurvey/ What do you think of this book? We want to hear from you!

and had to ask her how she would explain Barrier methods She looked at me quizzically, and gave a reply that although anatomically correct if I was in a doctor’s surgery, indicated that either I had not phrased the question very carefully or that she had completely mis- understood what I was asking! James has now grown up and will soon have to learn what real work entails if he is to keep Diana and myself in the manner to which we would like to become accustomed in our dotage Francesca has also grown up, and seems to have refined a strategy for getting all she wants without doing anything other than looking at me with wide, bright eyes, and smiling Finally, “Up the Gills!” John Sharp

xix

Introduction

Microsoft Visual C# is a powerful but simple language aimed primarily at developers creating applications by using the Microsoft NET Framework It inherits many of the best features of C++ and Microsoft Visual Basic, but few of the inconsistencies and anachronisms, resulting in a cleaner and more logical language C# 1 0 made its public debut in 2001 The advent of C# 2 0 with Visual Studio 2005 saw several important new features added to the language, including Generics, Iterators, and anonymous methods C# 3 0 which was released with Visual Studio 2008, added extension methods, lambda expressions, and most famously of all, the Language Integrated Query facility, or LINQ The latest incarnation of the language, C# 4 0, provides further enhancements that improve its interoperability with other languages and technologies These features include support for named and optional arguments, the dynamic type which indicates that the language runtime should implement late binding for an object, and variance which resolves some issues in the way in which generic interfaces are defined C# 4 0 takes advantage of the latest version of the NET Framework, also version 4 0 There are many additions to the NET Framework in this release, but arguably the most significant are the classes and types that constitute the Task Parallel Library (TPL) Using the TPL, you can now build highly scalable applications that can take full advantage of multi-core processors quickly and easily The support for Web services and Windows Communication Foundation (WCF) has also been extended; you can now build services that follow the REST model as well as the more traditional SOAP scheme The development environment provided by Microsoft Visual Studio 2010 makes all these powerful features easy to use, and the many new wizards and enhancements included in Visual Studio 2010 can greatly improve your productivity as a developer Who This Book Is For This book assumes that you are a developer who wants to learn the fundamentals of programming with C# by using Visual Studio 2010 and the NET Framework version 4 0 In this book, you will learn the features of the C# language, and then use them to build applica- tions running on the Microsoft Windows operating system By the time you complete this book, you will have a thorough understanding of C# and will have used it to build Windows Presentation Foundation applications, access Microsoft SQL Server databases by using ADO NET and LINQ, build responsive and scalable applications by using the TPL, and create REST and SOAP Web services by using WCF