Home Page Icon
Home Page
Table of Contents for
Index
Close
Index
by Harvey Deitel, Paul Deitel
Visual C# How to Program
Visual C#® HOW TO PROGRAM
Deitel® Series Page
Visual C#® HOW TO PROGRAM
Trademarks
Contents
Online Topics
Preface
Contacting the Authors
Join the Deitel & Associates, Inc. Social Media Communities
Object-Oriented Programming with an Early Objects Approach
New C# 6 Features
Interesting, Entertaining and Challenging Exercises
A Tour of the Book
A Tour of the Online Content
Teaching Approach
Obtaining the Software Used in Visual C# How to Program, 6/e
Instructor Supplements
Microsoft DreamSpark™
Reviewers
About Deitel & Associates, Inc.
Before You Begin
1 Introduction to Computers, the Internet and Visual C#
Objectives
Outline
1.1 Introduction
1.2 Computers and the Internet in Industry and Research
1.3 Hardware and Software
1.3.1 Moore’s LawMoore’s Law
1.3.2 Computer Organization
1.4 Data Hierarchy
1.5 Machine Languages, Assembly Languages and High-Level Languages
1.6 Object Technology
1.7 Internet and World Wide Web
1.8 C#
1.8.1 Object-Oriented Programming
1.8.2 Event-Driven Programming
1.8.3 Visual Programming
1.8.4 Generic and Functional Programming
Generic Programming
Functional Programming
1.8.5 An International Standard
1.8.6 C# on Non-Windows Platforms
1.8.7 Internet and Web Programming
1.8.8 Asynchronous Programming with async and await
1.8.9 Other Key Programming Languages
1.9 Microsoft’s .NETMicrosoft’s .NET
1.9.1 .NET Framework
1.9.2 Common Language Runtime
1.9.3 Platform Independence
1.9.4 Language Interoperability
1.10 Microsoft’s Windows® Operating SystemMicrosoft’s Windows® Operating System
1.11 Visual Studio Integrated Development Environment
1.12 Painter Test-Drive in Visual Studio Community
Self-Review Exercises
Exercises
Making-a-Difference Exercises
Making-a-Difference Resources
2 Introduction to Visual Studio and Visual Programming
Objectives
Outline
2.1 Introduction
2.2 Overview of the Visual Studio Community 2015 IDE
2.2.1 Introduction to Visual Studio Community 2015
2.2.2 Visual Studio Themes
2.2.3 Links on the Start Page
2.2.4 Creating a New Project
2.2.5 New Project Dialog and Project Templates
2.2.6 Forms and Controls
2.3 Menu Bar and Toolbar
2.4 Navigating the Visual Studio IDE
2.4.1 Solution Explorer
2.4.2 Toolbox
2.4.3 Properties Window
2.5 Help Menu and Context-Sensitive Help
2.6 Visual Programming: Creating a Simple App that Displays Text and an Image
2.7 Wrap-Up
2.8 Web Resources
Summary
Section 2.1 Introduction
Section 2.2 Overview of the Visual Studio Community 2015 IDE
Section 2.3 Menu Bar and Toolbar
Section 2.4 Navigating the Visual Studio IDE
Section 2.5 Help Menu and Context-Sensitive Help
Section 2.6 Visual Programming: Creating a Simple App that Displays Text and an Image
Terminology
Self-Review Exercises
Exercises
Note Regarding Exercises 2.7–2.11
3 Introduction to C# App Programming
Objectives
Outline
3.1 Introduction
3.2 Simple App: Displaying a Line of Text
3.2.1 Comments
3.2.2 using Directive
3.2.3 Blank Lines and Whitespace
3.2.4 Class Declaration
Class Name Convention
Class Declaration’s File Name
Body of a Class Declaration
3.2.5 Main Method
Body of a Method Declaration
3.2.6 Displaying a Line of Text
Statements
3.2.7 Matching Left ({) and Right (}) Braces
3.3 Creating a Simple App in Visual Studio
3.3.1 Creating the Console App
3.3.2 Changing the Name of the App File
3.3.3 Writing Code and Using IntelliSense
3.3.4 Compiling and Running the App
3.3.5 Errors, Error Messages and the Error List Window
3.4 Modifying Your Simple C# App
3.4.1 Displaying a Single Line of Text with Multiple Statements
3.4.2 Displaying Multiple Lines of Text with a Single Statement
3.5 String Interpolation
3.6 Another C# App: Adding Integers
3.6.1 Declaring the int Variable number1
3.6.2 Declaring Variables number2 and sum
3.6.3 Prompting the User for Input
3.6.4 Reading a Value into Variable number1
Possible Erroneous User Input
Assigning a Value to a Variable
3.6.5 Prompting the User for Input and Reading a Value into number2
3.6.6 Summing number1 and number2
3.6.7 Displaying the sum with string Interpolation
3.6.8 Performing Calculations in Output Statements
3.7 Memory Concepts
3.8 Arithmetic
3.8.1 Arithmetic Expressions in Straight-Line Form
3.8.2 Parentheses for Grouping Subexpressions
3.8.3 Rules of Operator Precedence
3.8.4 Sample Algebraic and C# Expressions
Evaluation of a Second-Degree Polynomial
3.8.5 Redundant Parentheses
3.9 Decision Making: Equality and Relational Operators
3.10 Wrap-Up
Summary
Section 3.2.1 Comments
Section 3.2.2 using Directive
Section 3.2.3 Blank Lines and Whitespace
Section 3.2.4 Class Declaration
Section 3.2.5 Main Method
Section 3.2.6 Displaying a Line of Text
Section 3.3.1 Creating the Console App
Section 3.3.3 Writing Code and Using IntelliSense
Section 3.4.1 Displaying a Single Line of Text with Multiple Statements
Section 3.4.2 Displaying Multiple Lines of Text with a Single Statement
Section 3.5 String Interpolation
Section 3.6.1 Declaring the int Variable number1
Section 3.6.3 Prompting the User for Input
Section 3.6.6 Summing number1 and number2
Section 3.7 Memory Concepts
Section 3.8 Arithmetic
Section 3.8.1 Arithmetic Expressions in Straight-Line Form
Section 3.8.2 Parentheses for Grouping Subexpressions
Section 3.8.3 Rules of Operator Precedence
Section 3.8.5 Redundant Parentheses
Section 3.9 Decision Making: Equality and Relational Operators
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
4 Introduction to Classes, Objects, Methods and strings
Objectives
Outline
4.1 Introduction
4.2 Test-Driving an Account Class
4.2.1 Instantiating an Object—Keyword Instantiating an Object—Keyword new
4.2.2 Calling Class Account’s ’s GetName Method
4.2.3 Inputting a Name from the User
4.2.4 Calling Class Account’s ’s SetName Method
Displaying the Name That Was Entered by the User
4.3 Account Class with an Instance Variable and Set and Get Methods
4.3.1 Account Class Declaration
4.3.2 Keyword class and the Class Body
Identifiers and Camel-Case Naming
4.3.3 Instance Variable name of Type string
null—the Default Initial Value for —the Default Initial Value for string Variables
4.3.4 SetName Method
SetName’s Parameter’s Parameter
SetName Parameter List
SetName Method Body
Parameters Are Local Variables
4.3.5 GetName Method
4.3.6 Access Modifiers private and public
Default Access for Class Members
4.3.7 Account UML Class Diagram
Top Compartment
Middle Compartment
Bottom Compartment
Return Types
Parameters
4.4 Creating, Compiling and Running a Visual C# Project with Two Classes
4.5 Software Engineering with Set and Get Methods
4.6 Account Class with a Property Rather Than Set and Get Methods
4.6.1 Class AccountTest Using Account’s Using Account’s Name Property
Invoking Class Account’s ’s Name Property to Get the Name
Invoking Class Account’s ’s Name Property to Set the Name
4.6.2 Account Class with an Instance Variable and a Property
Property Name’s Declaration’s Declaration
Property Name’s ’s get Accessor
Property Name’s ’s set Accessor
4.6.3 Account UML Class Diagram with a Property
4.7 Auto-Implemented Properties
4.8 Account Class: Initializing Objects with Constructors
4.8.1 Declaring an Account Constructor for Custom Object Initialization
Account Constructor Declaration
Constructor Body
4.8.2 Class AccountTest: Initializing Account Objects When They’re Created Objects When They’re Created
Default Constructor
There’s No Default Constructor in a Class That Declares a Constructor
Adding the Constructor to Class Account’s UML Class Diagram’s UML Class Diagram
4.9 Account Class with a Balance; Processing Monetary Amounts
4.9.1 Account Class with a decimal balance Instance Variable
Account Class Two-Parameter Constructor
Account Property Balance
set and get Accessors with Different Access Modifiers
Account Class Deposit Method
4.9.2 AccountTest Class That Uses Account Objects with Balances
Displaying the Account Objects’ Initial Balances Objects’ Initial Balances
string Interpolation Expressions with Formatting
Reading a decimal Value from the User
Making a Deposit
Reading a decimal Value and Depositing into account2
Duplicated Code in Method Main
UML Class Diagram for Class Account
4.10 Wrap-Up
Summary
Section 4.1 Introduction
Section 4.2 Test-Driving an Account Class
Section 4.2.1 Instantiating an Object—Keyword Instantiating an Object—Keyword new and Constructors
Section 4.2.2 Calling Class Account’s ’s GetName Method
Section 4.2.3 Inputting a Name from the User
Section 4.2.4 Calling Class Account’s ’s SetName Method
Section 4.3 Account Class with an Instance Variable and Set and Get Methods
Section 4.3.1 Account Class Declaration
Section 4.3.2 Keyword class and the Class Body
Section 4.3.3 Instance Variable name of Type string
Section 4.3.4 SetName Method
Section 4.3.5 GetName Method
Section 4.3.6 Access Modifiers private and public
Section 4.3.7 Account UML Class Diagram
Section 4.4 Creating, Compiling and Running a Visual C# Project with Two Classes
Section 4.5 Software Engineering with Set and Get Methods
Section 4.6 Account Class with a Property Rather Than Set and Get Methods
Section 4.6.1 Class AccountTest Using Account’s Using Account’s Name Property
Section 4.6.2 Account Class with an Instance Variable and a Property
Section 4.6.3 Account UML Class Diagram with a Property
Section 4.7 Auto-Implemented Properties
Section 4.8 Account Class: Initializing Objects with Constructors
Section 4.8.1 Declaring an Account Constructor for Custom Object Initialization
Section 4.8.2 Class AccountTest: Initializing Account Objects When They’re Created Objects When They’re Created
Section 4.9 Account Class with a Balance; Processing Monetary Amounts
Section 4.9.1 Account Class with a decimal balance Instance Variable
Section 4.9.2 AccountTest Class That Creates and Uses Account Objects
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
5 Algorithm Development and Control Statements: Part 1
Objectives
Outline
5.1 Introduction
5.2 Algorithms
5.3 Pseudocode
5.4 Control Structures
5.4.1 Sequence Structure
5.4.2 Selection Statements
5.4.3 Iteration Statements
5.4.4 Summary of Control Statements
5.5 if Single-Selection Statement
5.6 if……else Double-Selection Statement
5.6.1 Nested if……else Statements
5.6.2 Dangling-else Problem
5.6.3 Blocks
Syntax and Logic Errors
Empty Statement
5.6.4 Conditional Operator (?:)
5.7 Student Class: Nested if……else Statements
5.8 while Iteration Statement
5.9 Formulating Algorithms: Counter-Controlled Iteration
5.9.1 Pseudocode Algorithm with Counter-Controlled Iteration
5.9.2 Implementing Counter-Controlled Iteration
Local Variables in Main
Initialization Phase: Initializing Variables total and gradeCounter
Processing Phase: Reading 10 Grades from the User
Termination Phase: Calculating and Displaying the Class Average
5.9.3 Integer Division and Truncation
5.10 Formulating Algorithms: Sentinel-Controlled Iteration
5.10.1 Top-Down, Stepwise Refinement: The Top and First Refinement
5.10.2 Second Refinement
5.10.3 Implementing Sentinel-Controlled Iteration
5.10.4 Program Logic for Sentinel-Controlled Iteration
5.10.5 Braces in a while Statement
5.10.6 Converting Between Simple Types Explicitly and Implicitly
Cast Operator
Promotions
Cast Operators for Any Type
5.10.7 Formatting Floating-Point Numbers
Rounding Floating-Point Numbers
5.11 Formulating Algorithms: Nested Control Statements
5.11.1 Problem Statement
Problem Statement Observations
5.11.2 Top-Down, Stepwise Refinement: Pseudocode Representation of the Top
5.11.3 Top-Down, Stepwise Refinement: First Refinement
5.11.4 Top-Down, Stepwise Refinement: Second Refinement
5.11.5 Complete Second Refinement of the Pseudocode
5.11.6 App That Implements the Pseudocode Algorithm
5.12 Compound Assignment Operators
5.13 Increment and Decrement Operators
5.13.1 Prefix Increment vs. Postfix Increment
5.13.2 Simplifying Increment Statements
5.13.3 Operator Precedence and Associativity
5.14 Simple Types
5.15 Wrap-Up
Summary
Section 5.2 Algorithms
Section 5.3 Pseudocode
Section 5.4 Control Structures
Section 5.4.1 Sequence Structure
Section 5.4.2 Selection Statements
Section 5.4.3 Iteration Statements
Section 5.4.4 Summary of Control Statements
Section 5.5 if Single-Selection Statement
Section 5.6 if……else Double-Selection Statement
Section 5.6.3 Blocks
Section 5.6.4 Conditional Operator (?:)
Section 5.7 Student Class: Nested if……else Statements
Section 5.8 while Iteration Statement
Section 5.9.1 Pseudocode Algorithm with Counter-Controlled Iteration
Section 5.9.2 Implementing Counter-Controlled Iteration
Section 5.9.3 Integer Division and Truncation
Section 5.10 Formulating Algorithms: Sentinel-Controlled Iteration
Section 5.10.1 Top-Down, Stepwise Refinement: The Top and First Refinement
Section 5.10.2 Second Refinement
Section 5.10.3 Implementing Sentinel-Controlled Iteration
Section 5.10.6 Converting Between Simple Types Explicitly and Implicitly
Section 5.10.7 Formatting Floating-Point Numbers
Section 5.12 Compound Assignment Operators
Section 5.13 Increment and Decrement Operators
Section 5.14 Simple Types
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
6 Control Statements: Part 2
Objectives
Outline
6.1 Introduction
6.2 Essentials of Counter-Controlled Iteration
6.3 for Iteration Statement
6.3.1 A Closer Look at the for Statement’s Header Statement’s Header
6.3.2 General Format of a for Statement
6.3.3 Scope of a for Statement’s Control Variable Statement’s Control Variable
6.3.4 Expressions in a for Statement’s Header Are Optional Statement’s Header Are Optional
6.3.5 Placing Arithmetic Expressions in a for Statement’s Header Statement’s Header
6.3.6 Using a for Statement’s Control Variable in the Statement’s Body Statement’s Control Variable in the Statement’s Body
6.3.7 UML Activity Diagram for the for Statement
6.4 Examples Using the for Statement
6.5 App: Summing Even Integers
6.6 App: Compound-Interest Calculations
6.6.1 Performing the Interest Calculations with Math Method pow
6.6.2 Formatting with Field Widths and Alignment
6.6.3 Caution: Do Not Use float or double for Monetary Amounts
Applications of Floating-Point Numbers
A Warning About Displaying Rounded Values
6.7 do……while Iteration Statement
6.8 switch Multiple-Selection Statement
6.8.1 Using a switch Statement to Count A, B, C, D and F Grades
Reading Grades from the User
Processing the Grades
The switch Statement
Consecutive case Labels
The default Case
No “Fall Through” in the C# switch Statement
Displaying the Grade Report
6.8.2 switch Statement UML Activity Diagram
6.8.3 Notes on the Expression in Each case of a switch
6.9 Class AutoPolicy Case Study: strings in switch Statements
6.10 break and continue Statements
6.10.1 break Statement
6.10.2 continue Statement
6.11 Logical Operators
6.11.1 Conditional AND (&&) Operator
6.11.2 Conditional OR (||) Operator
6.11.3 Short-Circuit Evaluation of Complex Conditions
6.11.4 Boolean Logical AND (&) and Boolean Logical OR (|) Operators
6.11.5 Boolean Logical Exclusive OR (^)
6.11.6 Logical Negation (!) Operator
6.11.7 Logical Operators Example
Precedence and Associativity of the Operators Presented So Far
6.12 Structured-Programming Summary
6.13 Wrap-Up
Summary
Section 6.2 Essentials of Counter-Controlled Iteration
Section 6.3 for Iteration Statement
Section 6.3.1 A Closer Look at the for Statement’s Header Statement’s Header
Section 6.3.2 General Format of a for Statement
Section 6.3.3 Scope of a for Statement’s Control Variable Statement’s Control Variable
Section 6.3.5 Placing Arithmetic Expressions in a for Statement’s Header Statement’s Header
Section 6.6 App: Compound-Interest Calculations
Section 6.6.1 Performing the Interest Calculations with Math Method pow
Section 6.6.2 Formatting with Field Widths and Alignment
Section 6.6.3 Caution: Do Not Use float or double for Monetary Amounts
Section 6.7 do……while Iteration Statement
Section 6.8 switch Multiple-Selection Statement
Section 6.8.1 Using a switch Statement to Count A, B, C, D and F Grades.
Section 6.10.1 break Statement
Section 6.10.2 continue Statement
Section 6.11 Logical Operators
Section 6.11.1 Conditional AND (&&) Operator
Section 6.11.2 Conditional OR (||) Operator
Section 6.11.3 Short-Circuit Evaluation of Complex Conditions
Section 6.11.4 Boolean Logical AND (&) and Boolean Logical OR (|) Operators
Section 6.11.5 Boolean Logical Exclusive OR (^)
Section 6.11.6 Logical Negation (!) Operator
Section 6.12 Structured-Programming Summary
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
7 Methods: A Deeper Look
Objectives
Outline
7.1 Introduction
7.2 Packaging Code in C#
7.2.1 Modularizing Programs
7.2.2 Calling Methods
7.3 static Methods, static Variables and Class Math
7.3.1 Math Class Methods
7.3.2 Math Class Constants PI and E
7.3.3 Why Is Main Declared static?
7.3.4 Additional Comments About Main
7.4 Methods with Multiple Parameters
7.4.1 Keyword static
7.4.2 Method Maximum
Logic of Determining the Maximum Value
7.4.3 Assembling strings with Concatenation
Anything Can Be Converted to a string
Formatting strings with string Interpolation
7.4.4 Breaking Apart Large string Literals
7.4.5 When to Declare Variables as Fields
7.4.6 Implementing Method Maximum by Reusing Method Math.Max
7.5 Notes on Using Methods
Three Ways to Call a Method
Three Ways to Return from a Method
static Members Can Access Only the Class’s Other Members Can Access Only the Class’s Other static Members Directly
7.6 Argument Promotion and Casting
7.6.1 Promotion Rules
7.6.2 Sometimes Explicit Casts Are Required
7.7 The .NET Framework Class Library
7.8 Case Study: Random-Number Generation
7.8.1 Creating an Object of Type Random
7.8.2 Generating a Random Integer
7.8.3 Scaling the Random-Number Range
7.8.4 Shifting Random-Number Range
7.8.5 Combining Shifting and Scaling
7.8.6 Rolling a Six-Sided Die
Rolling a Six-Sided Die 60,000,000 Times
7.8.7 Scaling and Shifting Random Numbers
7.8.8 Repeatability for Testing and Debugging
7.9 Case Study: A Game of Chance; Introducing Enumerations
7.9.1 Method RollDice
7.9.2 Method Main’s Local Variables’s Local Variables
7.9.3 enum Type Status
7.9.4 The First Roll
7.9.5 enum Type DiceNames
7.9.6 Underlying Type of an enum
7.9.7 Comparing Integers and enum Constants
Additional Rolls of the Dice
Control Statements in the Craps Example
Code Snippets for Auto-Implemented Properties
7.10 Scope of Declarations
7.11 Method-Call Stack and Activation Records
7.11.1 Method-Call Stack
7.11.2 Stack Frames
7.11.3 Local Variables and Stack Frames
7.11.4 Stack Overflow
7.11.5 Method-Call Stack in Action
7.12 Method Overloading
7.12.1 Declaring Overloaded Methods
7.12.2 Distinguishing Between Overloaded Methods
7.12.3 Return Types of Overloaded Methods
7.13 Optional Parameters
7.14 Named Parameters
7.15 C# 6 Expression-Bodied Methods and Properties
7.16 Recursion
7.16.1 Base Cases and Recursive Calls
7.16.2 Recursive Factorial Calculations
7.16.3 Implementing Factorial Recursively
7.17 Value Types vs. Reference Types
7.18 Passing Arguments By Value and By Reference
7.18.1 ref and out Parameters
ref Parameters
out Parameters
Passing Reference-Type Variables by Reference
7.18.2 Demonstrating ref, out and Value Parameters
7.19 Wrap-Up
Summary
Section 7.1 Introduction
Section 7.2 Packaging Code in C#
Section 7.2.1 Modularizing Programs
Section 7.3 static Methods, static Variables and Class Math
Section 7.3.1 Math Class Methods
Section 7.3.2 Math Class Constants PI and E
Section 7.3.4 Additional Comments About Main
Section 7.4.2 Method Maximum
Section 7.4.3 Assembling strings with Concatenation
Section 7.4.4 Breaking Apart Large string Literals
Section 7.5 Notes on Declaring and Using Methods
Section 7.6 Argument Promotion and Casting
Section 7.6.1 Promotion Rules
Section 7.6.2 Sometimes Explicit Casts Are Required
Section 7.7 The .NET Framework Class Library
Section 7.8.2 Generating a Random Integer
Section 7.8.3 Scaling the Random-Number Range
Section 7.8.8 Repeatability for Testing and Debugging
Section 7.9.3 enum Type Status
Section 7.9.5 enum Type DiceNames
Section 7.9.6 Underlying Type of an enum
Section 7.10 Scope of Declarations
Section 7.11 Method-Call Stack and Activation Records
Section 7.11.1 Method-Call Stack
Section 7.11.2 Stack Frames
Section 7.11.3 Local Variables and Stack Frames
Section 7.11.4 Stack Overflow
Section 7.12.1 Declaring Overloaded Methods
Section 7.12.2 Distinguishing Between Overloaded Methods
Section 7.12.3 Return Types of Overloaded Methods
Section 7.13 Optional Parameters
Section 7.14 Named Parameters
Section 7.15 C# 6 Expression-Bodied Methods and Properties
Section 7.16 Recursion
Section 7.16.1 Base Cases and Recursive Calls
Section 7.16.2 Recursive Factorial Calculations
Section 7.17 Value Types vs. Reference Types
Section 7.18 Passing Arguments by Value and by Reference
Section 7.18.1 ref and out Parameters
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
8 Arrays; Introduction to Exception Handling
Objectives
Outline
8.1 Introduction
8.2 Arrays
8.3 Declaring and Creating Arrays
8.4 Examples Using Arrays
8.4.1 Creating and Initializing an Array
8.4.2 Using an Array Initializer
8.4.3 Calculating a Value to Store in Each Array Element
Declaring a Named Constant with const
8.4.4 Summing the Elements of an Array
8.4.5 Iterating Through Arrays with foreach
foreach vs. for
8.4.6 Using Bar Charts to Display Array Data Graphically; Introducing Type Inference with var
Implicitly Typed Local Variables and Keyword var
More on Implicitly Typed Local Variables
8.4.7 Using the Elements of an Array as Counters
8.5 Using Arrays to Analyze Survey Results; Intro to Exception Handling
8.5.1 Summarizing the Results
8.5.2 Exception Handling: Processing the Incorrect Response
8.5.3 The try Statement
8.5.4 Executing the catch Block
8.5.5 Message Property of the Exception Parameter
8.6 Case Study: Card Shuffling and Dealing Simulation
8.6.1 Class Card and Getter-Only Auto-Implemented Properties
8.6.2 Class DeckOfCards
Class DeckOfCards: Constructor
Class DeckOfCards: Shuffle Method
Recommendation: Use an Unbiased Shuffling Algorithm
Class DeckOfCards: DealCard Method
8.6.3 Shuffling and Dealing Cards
8.7 Passing Arrays and Array Elements to Methods
8.8 Case Study: GradeBook Using an Array to Store Grades
8.9 Multidimensional Arrays
8.9.1 Rectangular Arrays
Array-Access Expression for a Two-Dimensional Rectangular Array
Array Initializer for a Two-Dimensional Rectangular Array
8.9.2 Jagged Arrays
Array Initializer for a Two-Dimensional Jagged Array
Diagram of a Two-Dimensional Jagged Array in Memory
Creating Two-Dimensional Arrays with Array-Creation Expressions
8.9.3 Two-Dimensional Array Example: Displaying Element Values
Overloaded Method OutputArray
Method OutputArray for Rectangular Arrays
Method OutputArray for Jagged Arrays
Common Multidimensional-Array Manipulations Performed with for Statements
8.10 Case Study: GradeBook Using a Rectangular Array
8.11 Variable-Length Argument Lists
8.12 Using Command-Line Arguments
8.13 (Optional) Passing Arrays by Value and by Reference
8.14 Wrap-Up
Summary
Section 8.1 Introduction
Section 8.2 Arrays
Section 8.3 Declaring and Creating Arrays
Section 8.4.2 Using an Array Initializer
Section 8.4.3 Calculating a Value to Store in Each Array Element
Section 8.4.5 Iterating Through Arrays with foreach
Section 8.4.6 Using Bar Charts to Display Array Data Graphically; Introducing Type Inference with var
Section 8.5.2 Exception Handling: Processing the Incorrect Response
Section 8.5.3 The try Statement
Section 8.5.4 Executing the catch Block
Section 8.5.5 Message Property of the Exception Parameter
Section 8.6.1 Class Card and Getter-Only Auto-Implemented Properties
Section 8.7 Passing Arrays and Array Elements to Methods
Section 8.9 Multidimensional Arrays
Section 8.10 Case Study: GradeBook Using a Rectangular Array
Section 8.11 Variable-Length Argument Lists
Section 8.12 Using Command-Line Arguments
Section 8.13 (Optional) Passing Arrays by Value and by Reference
Terminology
Self-Review Exercises
Exercises
Special Section: Building Your Own Computer
Making-a-Difference Exercise
9 Introduction to LINQ and the List Collection
Objectives
Outline
9.1 Introduction
9.2 Querying an Array of int Values Using LINQ
9.2.1 The from Clause
Implicitly Typed Local Variables
9.2.2 The where Clause
9.2.3 The select Clause
9.2.4 Iterating Through the Results of the LINQ Query
LINQ vs. Iteration Statements
9.2.5 The orderby Clause
9.2.6 Interface IEnumerable<T>
9.3 Querying an Array of Employee Objects Using LINQ
9.3.1 Accessing the Properties of a LINQ Query’s Range VariableAccessing the Properties of a LINQ Query’s Range Variable
9.3.2 Sorting a LINQ Query’s Results by Multiple PropertiesSorting a LINQ Query’s Results by Multiple Properties
9.3.3 Any, First and Count Extension Methods
9.3.4 Selecting a Property of an Object
9.3.5 Creating New Types in the select Clause of a LINQ Query
Projections
Changing the Names of Properties in Anonymous Types
9.4 Introduction to Collections
9.4.1 List<T> Collection
9.4.2 Dynamically Resizing a List<T> Collection
Adding and Inserting Elements
Count and Capacity
Iterating Through a List’s Contents
Adding More Elements and Growing the List
Removing Elements
Determining Whether an Element Is in the List
Adding More Elements and Growing the List
Doubling the Capacity
9.5 Querying the Generic List Collection Using LINQ
9.5.1 The let Clause
9.5.2 Deferred Execution
9.5.3 Extension Methods ToArray and ToList
9.5.4 Collection Initializers
9.6 Wrap-Up
9.7 Deitel LINQ Resource Center
Summary
Section 9.1 Introduction
Section 9.2 Querying an Array of int Values Using LINQ
Section 9.2.1 The from Clause
Section 9.2.2 The where Clause
Section 9.2.3 The select Clause
Section 9.2.4 Iterating Through the Results of the LINQ Query
Section 9.2.5 The orderby Clause
Section 9.2.6 Interface IEnumerable<T>
Section 9.3 Querying an Array of Employee Objects Using LINQ
Section 9.3.2 Sorting a LINQ Query’s Results by Multiple PropertiesSorting a LINQ Query’s Results by Multiple Properties
Section 9.3.3 Any, First and Count Extension Methods
Section 9.3.4 Selecting a Property of an Object
Section 9.3.5 Creating New Types in the select Clause of a LINQ Query
Section 9.4 Introduction to Collections
Section 9.4.1 List<T> Collection
Section 9.5 Querying the Generic List Collection Using LINQ
Section 9.5.1 The let Clause
Section 9.5.2 Deferred Execution
Section 9.5.3 Extension Methods ToArray and ToList
Section 9.5.4 Collection Initializers
Terminology
Self-Review Exercises
Exercises
10 Classes and Objects: A Deeper Look
Objectives
Outline
10.1 Introduction
10.2 Time Class Case Study; Throwing Exceptions
10.2.1 Time1 Class Declaration
public Class
Method SetTime and Throwing Exceptions
Method ToUniversalString
Method ToString
10.2.2 Using Class Time1
Calling Time Method SetTime with Invalid Values
Notes on the Time1 Class Declaration
10.3 Controlling Access to Members
10.4 Referring to the Current Object’s Members with the Referring to the Current Object’s Members with the this Reference
10.5 Time Class Case Study: Overloaded Constructors
10.5.1 Class Time2 with Overloaded Constructors
Class Time2’s Three-Argument Constructor’s Three-Argument Constructor
Constructor Initializers
SetTime Method and the Hour, Minute and Second Properties
Notes Regarding Class Time2’s Methods, Properties and Constructors’s Methods, Properties and Constructors
10.5.2 Using Class Time2’s Overloaded Constructors’s Overloaded Constructors
10.6 Default and Parameterless Constructors
10.7 Composition
10.7.1 Class Date
Constructor
private set Accessors
10.7.2 Class Employee
10.7.3 Class EmployeeTest
10.8 Garbage Collection and Destructors
10.9 static Class Members
10.10 readonly Instance Variables
10.11 Class View and Object Browser
10.11.1 Using the Class View Window
10.11.2 Using the Object Browser
10.12 Object Initializers
10.13 Operator Overloading; Introducing struct
10.13.1 Creating Value Types with struct
When to Declare a struct Type
10.13.2 Value Type ComplexNumber
Constructor
Overloaded Operators
10.13.3 Class ComplexTest
10.14 Time Class Case Study: Extension Methods
10.15 Wrap-Up
Summary
Section 10.2.1 Time1 Class Declaration
Section 10.2.2 Using Class Time1
Section 10.3 Controlling Access to Members
Section 10.4 Referring to the Current Object’s Members with the Referring to the Current Object’s Members with the this Reference
Section 10.5 Time Class Case Study: Overloaded Constructors
Section 10.5.1 Class Time2 with Overloaded Constructors
Section 10.6 Default and Parameterless Constructors
Section 10.7 Composition
Section 10.8 Garbage Collection and Destructors
Section 10.9 static Class Members
Section 10.10 readonly Instance Variables
Section 10.11.1 Using the Class View Window
Section 10.11.2 Using the Object Browser
Section 10.12 Object Initializers
Section 10.13 Operator Overloading; Introducing struct
Section 10.13.1 Creating Value Types with struct
Section 10.13.2 Value Type ComplexNumber
Section 10.14 Time Class Case Study: Extension Methods
Terminology
Self-Review Exercises
Exercises
11 Object-Oriented Programming: Inheritance
Objectives
Outline
11.1 Introduction
11.2 Base Classes and Derived Classes
11.3 protected Members
11.4 Relationship between Base Classes and Derived Classes
11.4.1 Creating and Using a CommissionEmployee Class
CommissionEmployee Class Overview
CommissionEmployee Constructor
CommissionEmployee Method Earnings
CommissionEmployee Method ToString
Class CommissionEmployeeTest
11.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance
Class BasePlusCommissionEmployeeTest
Code Duplication
11.4.3 Creating a CommissionEmployee––BasePlusCommissionEmployee Inheritance Hierarchy
A Derived Class’s Constructor Must Call Its Base Class’s Constructor
BasePlusCommissionEmployee Method Earnings
11.4.4 CommissionEmployee––BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables
Class BasePlusCommissionEmployee
Testing Class BasePlusCommissionEmployee
public vs. protected Data
Problems with protected Instance Variables
11.4.5 CommissionEmployee––BasePlusCommissionEmployee Inheritance Hierarchy Using private Instance Variables
Base Class CommissionEmployee
Derived Class BasePlusCommissionEmployee
BasePlusCommissionEmployee Method Earnings
BasePlusCommissionEmployee Method ToString
Testing Class BasePlusCommissionEmployee
11.5 Constructors in Derived Classes
11.6 Software Engineering with Inheritance
11.7 Class object
11.8 Wrap-Up
Summary
Section 11.1 Introduction
Section 11.2 Base Classes and Derived Classes
Section 11.3 protected Members
Section 11.4.1 Creating and Using a CommissionEmployee Class
Section 11.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance
Section 11.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy
Section 11.4.4CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables
Section 11.4.5CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Instance Variables
Section 11.5 Constructors in Derived Classes
Section 11.6 Software Engineering with Inheritance
Section 11.7 Class object
Terminology
Self-Review Exercises
Exercises
12 OOP: Polymorphism and Interfaces
Objectives
Outline
12.1 Introduction
12.2 Polymorphism Examples
12.3 Demonstrating Polymorphic Behavior
12.4 Abstract Classes and Methods
12.5 Case Study: Payroll System Using Polymorphism
12.5.1 Creating Abstract Base Class Employee
Class Employee
12.5.2 Creating Concrete Derived Class SalariedEmployee
12.5.3 Creating Concrete Derived Class HourlyEmployee
12.5.4 Creating Concrete Derived Class CommissionEmployee
12.5.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee
12.5.6 Polymorphic Processing, Operator is and Downcasting
Assigning Derived-Class Objects to Base-Class References
Polymorphic Processing of Employees
Giving BasePlusCommissionEmployees 10% Raises
Every Object Knows Its Own Type
Avoiding Compilation Errors with Downcasting
12.5.7 Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables
12.6 sealed Methods and Classes
12.7 Case Study: Creating and Using Interfaces
12.7.1 Developing an IPayable Hierarchy
UML Diagram Containing an Interface
12.7.2 Declaring Interface IPayable
12.7.3 Creating Class Invoice
12.7.4 Modifying Class Employee to Implement Interface IPayable
Derived Classes of Employee and Interface IPayable
12.7.5 Using Interface IPayable to Process Invoices and Employees Polymorphically
12.7.6 Common Interfaces of the .NET Framework Class Library
12.8 Wrap-Up
Summary
Section 12.1 Introduction
Section 12.2 Polymorphism Examples
Section 12.3 Demonstrating Polymorphic Behavior
Section 12.4 Abstract Classes and Methods
Section 12.5 Case Study: Payroll System Using Polymorphism
Section 12.6 sealed Methods and Classes
Section 12.7 Case Study: Creating and Using Interfaces
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercise
13 Exception Handling: A Deeper Look
Objectives
Outline
13.1 Introduction
13.2 Example: Divide by Zero without Exception Handling
13.2.1 Dividing By Zero
13.2.2 Enter a Non-Numeric Denominator
13.2.3 Unhandled Exceptions Terminate the App
13.3 Example: Handling DivideByZeroExceptions and FormatExceptions
Sample Outputs
Another Way to Convert Strings to Integers
13.3.1 Enclosing Code in a try Block
13.3.2 Catching Exceptions
13.3.3 Uncaught Exceptions
13.3.4 Termination Model of Exception Handling
13.3.5 Flow of Control When Exceptions Occur
13.4 .NET Exception Hierarchy
13.4.1 Class SystemException
13.4.2 Which Exceptions Might a Method Throw?
13.5 finally Block
13.5.1 Moving Resource-Release Code to a finally Block
13.5.2 Demonstrating the finally Block
13.5.3 Throwing Exceptions Using the throw Statement
13.5.4 Rethrowing Exceptions
13.5.5 Returning After a finally Block
13.6 The using Statement
13.7 Exception Properties
13.7.1 Property InnerException
13.7.2 Other Exception Properties
13.7.3 Demonstrating Exception Properties and Stack Unwinding
13.7.4 Throwing an Exception with an InnerException
13.7.5 Displaying Information About the Exception
13.8 User-Defined Exception Classes
Class NegativeNumberException
Using Class NegativeNumberException
13.9 Checking for null References; Introducing C# 6’s References; Introducing C# 6’s ?. Operator
13.9.1 Null-Conditional Operator (?.)
13.9.2 Revisiting Operators is and as
13.9.3 Nullable Types
13.9.4 Null Coalescing Operator (??)
13.10 Exception Filters and the C# 6 when Clause
13.11 Wrap-Up
Summary
Section 13.1 Introduction
Section 13.2 Example: Divide by Zero without Exception Handling
Section 13.2.1 Dividing By Zero
Section 13.2.2 Enter a Non-Numeric Denominator
Section 13.3.1 Enclosing Code in a try Block
Section 13.3.2 Catching Exceptions
Section 13.3.3 Uncaught Exceptions
Section 13.3.4 Termination Model of Exception Handling
Section 13.3.5 Flow of Control When Exceptions Occur
Section 13.4 .NET Exception Hierarchy
Section 13.4.1 Class SystemException
Section 13.5 finally Block
Section 13.5.1 Moving Resource-Release Code to a finally Block
Section 13.6 The using Statement
Section 13.7 Exception Properties
Section 13.7.1 Property InnerException
Section 13.7.3 Demonstrating Exception Properties and Stack Unwinding
Section 13.8 User-Defined Exception Classes
Section 13.9.1 Null-Conditional Operator (?.)
Section 13.9.3 Nullable Types
Section 13.9.4 Null Coalescing Operator (??)
Section 13.10 Exception Filters and the C# 6 when Clause
Terminology
Self-Review Exercises
Exercises
14 Graphical User Interfaces with Windows Forms: Part 1
Objectives
Outline
14.1 Introduction
14.2 Windows Forms
14.3 Event Handling
14.3.1 A Simple Event-Driven GUI
Renaming the Form1.cs File
Adding a Button to the Form
Adding an Event Handler for the Button’s ’s Click Event
Event Handler Parameters
Displaying a MessageBox
14.3.2 Auto-Generated GUI Code
14.3.3 Delegates and the Event-Handling Mechanism
Delegates
Indicating the Method that a Delegate Should Call
(Optional) Multicast Delegates
14.3.4 Another Way to Create Event Handlers
Using the Properties Window to Create Event Handlers
14.3.5 Locating Event Information
14.4 Control Properties and Layout
14.4.1 Anchoring and Docking
Anchoring Demonstration
14.4.2 Using Visual Studio To Edit a GUI’s LayoutUsing Visual Studio To Edit a GUI’s Layout
14.5 Labels, TextBoxes and Buttons
14.6 GroupBoxes and Panels
14.7 CheckBoxes and RadioButtons
14.7.1 CheckBoxes
14.7.2 Combining Font Styles with Bitwise Operators
14.7.3 RadioButtons
14.8 PictureBoxes
Using Resources Programmatically
14.9 ToolTips
14.10 NumericUpDown Control
14.11 Mouse-Event Handling
14.12 Keyboard-Event Handling
14.13 Wrap-Up
Summary
Section 14.1 Introduction
Section 14.2 Windows Forms
Section 14.3 Event Handling
Section 14.3.1 A Simple Event-Driven GUI
Section 14.3.2 Auto-Generated GUI Code
Section 14.3.3 Delegates and the Event-Handling Mechanism
Section 14.3.4 Another Way to Create Event Handlers
Section 14.3.5 Locating Event Information
Section 14.4 Control Properties and Layout
Section 14.5 Labels, TextBoxes and Buttons
Section 14.6 GroupBoxes and Panels
Section 14.7 CheckBoxes and RadioButtons
Section 14.8 PictureBoxes
Section 14.9 ToolTips
Section 14.10 NumericUpDown Control
Section 14.11 Mouse-Event Handling
Section 14.12 Keyboard-Event Handling
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
15 Graphical User Interfaces with Windows Forms: Part 2
Objectives
Outline
15.1 Introduction
15.2 Menus
15.3 MonthCalendar Control
15.4 DateTimePicker Control
15.5 LinkLabel Control
15.6 ListBox Control
15.7 CheckedListBox Control
15.8 ComboBox Control
15.9 TreeView Control
15.10 ListView Control
15.11 TabControl Control
15.12 Multiple Document Interface (MDI) Windows
15.13 Visual Inheritance
15.14 User-Defined Controls
15.15 Wrap-Up
Summary
Section 15.2 Menus
Section 15.3 MonthCalendar Control
Section 15.4 DateTimePicker Control
Section 15.5 LinkLabel Control
Section 15.6 ListBox Control
Section 15.7 CheckedListBox Control
Section 15.8 ComboBox Control
Section 15.9 TreeView Control
Section 15.10 ListView Control
Section 15.11 TabControl Control
Section 15.12 Multiple Document Interface (MDI) Windows
Section 15.13 Visual Inheritance
Section 15.14 User-Defined Controls
Terminology
Self-Review Exercises
Exercises
16 Strings and Characters: A Deeper Look
Objectives
Outline
16.1 Introduction
16.2 Fundamentals of Characters and Strings
16.3 string Constructors
16.4 string Indexer, Length Property and CopyTo Method
16.5 Comparing strings
16.6 Locating Characters and Substrings in strings
16.7 Extracting Substrings from strings
16.8 Concatenating strings
16.9 Miscellaneous string Methods
16.10 Class StringBuilder
16.11 Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder
16.12 Append and AppendFormat Methods of Class StringBuilder
16.13 Insert, Remove and Replace Methods of Class StringBuilder
16.14 Char Methods
16.15 Introduction to Regular Expressions (Online)
16.16 Wrap-Up
Summary
Section 16.2 Fundamentals of Characters and Strings
Section 16.3 string Constructors
Section 16.4 string Indexer, Length Property and CopyTo Method
Section 16.5 Comparing strings
Section 16.6 Locating Characters and Substrings in strings
Section 16.7 Extracting Substrings from strings
Section 16.8 Concatenating strings
Section 16.10 Class StringBuilder
Section 16.11 Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder
Section 16.12 Append and AppendFormat Methods of Class StringBuilder
Section 16.13 Insert, Remove and Replace Methods of Class StringBuilder
Section 16.14 Char Methods
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
17 Files and Streams
Objectives
Outline
17.1 Introduction
17.2 Files and Streams
17.3 Creating a Sequential-Access Text File
17.4 Reading Data from a Sequential-Access Text File
17.5 Case Study: Credit-Inquiry Program
17.6 Serialization
17.7 Creating a Sequential-Access File Using Object Serialization
17.8 Reading and Deserializing Data from a Binary File
17.9 Classes File and Directory
17.9.1 Demonstrating Classes File and Directory
17.9.2 Searching Directories with LINQ
Method SearchDirectory
Method CleanDirectory
17.10 Wrap-Up
Summary
Section 17.1 Introduction
Section 17.2 Files and Streams
Section 17.3 Creating a Sequential-Access Text File
Section 17.4 Reading Data from a Sequential-Access Text File
Section 17.5 Case Study: Credit-Inquiry Program
Section 17.6 Serialization
Section 17.7 Creating a Sequential-Access File Using Object Serialization
Section 17.8 Reading and Deserializing Data from a Binary File
Section 17.9 Classes File and Directory
Terminology
Self-Review Exercises
Exercises
Making-a-Difference Exercises
18 Searching and Sorting
Objectives
Outline
18.1 Introduction
18.2 Searching Algorithms
18.2.1 Linear Search
Performing a Linear Search
Method LinearSearch
Efficiency of Linear Search
Constant Runtime
Linear Runtime
Quadratic Runtime
Linear Search Runtime
18.2.2 Binary Search
Implementing Binary Search
Method BinarySearch
Efficiency of Binary Search
18.3 Sorting Algorithms
18.3.1 Selection Sort
Implementing Selection Sort
Method SelectionSort
Efficiency of Selection Sort
18.3.2 Insertion Sort
Implementing Insertion Sort
Efficiency of Insertion Sort
18.3.3 Merge Sort
Implementing Merge Sort
Method SortArray
Method Merge
Efficiency of Merge Sort
18.4 Summary of the Efficiency of Searching and Sorting Algorithms
18.5 Wrap-Up
Summary
Section 18.1 Introduction
Section 18.2.1 Linear Search
Section 18.2.2 Binary Search
Section 18.3.1 Selection Sort
Section 18.3.2 Insertion Sort
Section 18.3.3 Merge Sort
Terminology
Self-Review Exercises
Exercises
19 Custom Linked Data Structures
Objectives
Outline
19.1 Introduction
19.2 Simple-Type structs, Boxing and Unboxing
19.3 Self-Referential Classes
19.4 Linked Lists
19.5 Stacks
19.6 Queues
19.7 Trees
19.7.1 Binary Search Tree of Integer Values
Inorder Traversal Algorithm
Preorder Traversal Algorithm
Postorder Traversal Algorithm
Duplicate Elimination
Overview of the Level-Order Binary-Tree Exercise
19.7.2 Binary Search Tree of IComparable Objects
19.8 Wrap-Up
Summary
Section 19.1 Introduction
Section 19.2 Simple-Type structs, Boxing and Unboxing
Section 19.3 Self-Referential Classes
Section 19.4 Linked Lists
Section 19.5 Stacks
Section 19.6 Queues
Section 19.7 Trees
Terminology
Self-Review Exercises
Exercises
Special Section: Building Your Own Compiler
20 Generics
Objectives
Outline
20.1 Introduction
20.2 Motivation for Generic Methods
20.3 Generic-Method Implementation
20.4 Type Constraints
20.4.1 IComparable<T> InterfaceIComparable<T> Interface
20.4.2 Specifying Type Constraints
Specifying the Type Constraint with a where Clause
Kinds of Type Constraints
Applying Multiple Type Constraints
Analyzing the Code
Value Types vs. Reference Types in Generics
20.5 Overloading Generic Methods
20.6 Generic Classes
20.7 Wrap-Up
Summary
Section 20.1 Introduction
Section 20.2 Motivation for Generic Methods
Section 20.3 Generic-Method Implementation
Section 20.4 Type Constraints
Section 20.5 Overloading Generic Methods
Section 20.6 Generic Classes
Terminology
Self-Review Exercises
Exercises
21 Generic Collections; Functional Programming with LINQ/PLINQ
Objectives
Outline
21.1 Introduction
21.2 Collections Overview
21.3 Class Array and Enumerators
21.3.1 C# 6 using static Directive
21.3.2 Class UsingArray’s ’s static Fields
21.3.3 Array Method Sort
21.3.4 Array Method Copy
21.3.5 Array Method BinarySearch
21.3.6 Array Method GetEnumerator and Interface IEnumerator
21.3.7 Iterating Over a Collection with foreach
21.3.8 Array Methods Clear, IndexOf, LastIndexOf and Reverse
21.4 Dictionary Collections
21.4.1 Dictionary Fundamentals
Hashing
Collisions
Load Factor
Hash Function
21.4.2 Using the SortedDictionary Collection
Method CollectWords
SortedDictionary Methods ContainsKey and Add
SortedDictionary Indexer
Method DisplayDictionary
Iterating Over a SortedDictionary’s ’s KeyValuePairs
SortedDictionary’s ’s Values Property
21.5 Generic LinkedList Collection
21.6 C# 6 Null Conditional Operator ?[]
21.7 C# 6 Dictionary Initializers and Collection Initializers
21.8 Delegates
21.8.1 Declaring a Delegate Type
21.8.2 Declaring a Delegate Variable
21.8.3 Delegate Parameters
21.8.4 Passing a Method Name Directly to a Delegate Parameter
21.9 Lambda Expressions
21.9.1 Expression Lambdas
21.9.2 Assigning Lambdas to Delegate Variables
21.9.3 Explicitly Typed Lambda Parameters
21.9.4 Statement Lambdas
21.10 Introduction to Functional Programming
21.11 Functional Programming with LINQ Method-Call Syntax and Lambdas
21.11.1 LINQ Extension Methods Min, Max, Sum and Average
Iteration and Mutation Are Hidden from You
21.11.2 Aggregate Extension Method for Reduction Operations
Summing the Squares of the Values with Method Aggregate
Calculating the Product of the Values with Method Aggregate
21.11.3 The Where Extension Method for Filtering Operations
Sorting the Results
Deferred Execution
21.11.4 Select Extension Method for Mapping Operations
21.12 PLINQ: Improving LINQ to Objects Performance with Multicore
21.13 (Optional) Covariance and Contravariance for Generic Types
21.14 Wrap-Up
Summary
Section 21.1 Introduction
Section 21.2 Collections Overview
Section 21.3 Class Array and Enumerators
Section 21.3.1 C# 6 using static Directive
Section 21.3.3 Array Method Sort
Section 21.3.4 Array Method Copy
Section 21.3.5 Array Method BinarySearch
Section 21.3.6 Array Method GetEnumerator and Interface IEnumerator
Section 21.3.7 Iterating Over a Collection with foreach
Section 21.3.8 Array Methods Clear, IndexOf, LastIndexOf and Reverse
Section 21.4 Dictionary Collections
Section 21.4.1 Dictionary Fundamentals
Section 21.4.2 Using the SortedDictionary Collection
Section 21.5 Generic LinkedList Collection
Section 21.6 C# 6 Null Conditional Operator ?[]
Section 21.7 C# 6 Dictionary Initializers and Collection Initializers
Section 21.8 Delegates
Section 21.8.1 Declaring a Delegate Type
Section 21.8.2 Declaring a Delegate Variable
Section 21.8.3 Delegate Parameters
Section 21.8.4 Passing a Method Name Directly to a Delegate Parameter
Section 21.9 Lambda Expressions
Section 21.9.1 Expression Lambdas
Section 21.9.2 Assigning Lambdas to Delegate Variables
Section 21.9.3 Explicitly Typed Lambda Parameters
Section 21.9.4 Statement Lambdas
Section 21.10 Introduction to Functional Programming
Section 21.11 Functional Programming with LINQ Method-Call Syntax and Lambdas
Section 21.11.1 LINQ Extension Methods Min, Max, Sum and Average
Section 21.11.2 Aggregate Extension Method for Reduction Operations
Section 21.11.3 The Where Extension Method for Filtering Operations
Section 21.11.4 Select Extension Method for Mapping Operations
Section 21.12 PLINQ: Improving LINQ to Objects Performance with Multicore
Section 21.13 (Optional) Covariance and Contravariance for Generic Types
Terminology
Self-Review Exercises
Exercises
Functional-Programming Exercises
22 Databases and LINQ
Objectives
Outline
22.1 Introduction
22.2 Relational Databases
22.3 A Books Database
22.4 LINQ to Entities and the ADO.NET Entity Framework
22.5 Querying a Database with LINQ
22.5.1 Creating the ADO.NET Entity Data Model Class Library
Step 1: Creating a Class Library Project for the ADO.NET Entity Data Model
Step 2: Adding the ADO.NET Entity Data Model to the Class Library
22.5.2 Creating a Windows Forms Project and Configuring It to Use the Entity Data Model
Step 1: Creating the Project
Step 2: Adding a Reference to the BooksExamples Class Library
Step 3: Adding a Reference to EntityFramework
Step 4: Adding the Connection String to the Windows Forms App
22.5.3 Data Bindings Between Controls and the Entity Data Model
Step 1: Adding a Data Source for the Authors Table
Step 2: Creating GUI Elements
Step 3: Connecting the Data Source to the authorBindingSource
Creating the DbContext Object
DisplayAuthorsTable_Load Event Handler
authorBindingNavigatorSaveItem_Click Event Handler: Saving Modifications to the Database
22.6 Dynamically Binding Query Results
22.6.1 Creating the Display Query Results GUI
Step 1: Creating the Project
Step 2: Creating a DataGridView to Display the Titles Table
Step 3: Adding a ComboBox to the Form
22.6.2 Coding the Display Query Results App
Customizing the Form’s ’s Load Event Handler
queriesComboBox_SelectedIndexChanged Event Handler
Ordering the Books By Title
Selecting Books with 2016 Copyright
Selecting Books with Titles That End in “How to Program”
22.7 Retrieving Data from Multiple Tables with LINQ
22.8 Creating a Master/Detail View App
22.8.1 Creating the Master/Detail GUI
Step 1: Creating the Project
Step 2: Adding a Data Source for the Authors Table
Step 3: Creating GUI Elements
22.8.2 Coding the Master/Detail App
22.9 Address Book Case Study
22.9.1 Creating the Address Book App’s GUI Creating the Address Book App’s GUI
Step 1: Creating a Class Library Project for the Entity Data Model
Step 2: Creating a Windows Forms Application Project for the AddressBook App
Step 3: Adding the Address Object as a Data Source
Step 4: Displaying the Details of Each Row
Step 5: Dragging the Address Data-Source Node to the Form
Step 5: Making the AddressID TextBox ReadOnly
Step 6: Adding Controls to Allow Users to Specify a Last Name to Locate
Step 7: Allowing the User to Return to Browsing All Rows of the Database
22.9.2 Coding the Address Book App
Method RefreshContacts
Method Contacts_Load
Method addressBindingNavigatorSaveItem_Click
Method findButton_Click
Method browseAllButton_Click
22.10 Tools and Web Resources
22.11 Wrap-Up
Summary
Section 22.1 Introduction
Section 22.2 Relational Databases
Section 22.3 A Books Database
Section 22.4 LINQ to Entities and the ADO.NET Entity Framework
Section 22.5 Querying a Database with LINQ
Section 22.5.1 Creating the ADO.NET Entity Data Model Class Library
Section 22.5.2 Creating a Windows Forms Project and Configuring It to Use the Entity Data Model
Section 22.5.3 Data Bindings Between Controls and the Entity Data Model
Section 22.6 Dynamically Binding Query Results
Section 22.7 Retrieving Data from Multiple Tables with LINQ
Section 22.8 Creating a Master/Detail View App
Section 22.9 Address Book Case Study
Terminology
Self-Review Exercises
Exercises
23 Asynchronous Programming with async and await
Objectives
Outline
23.1 Introduction
23.2 Basics of async and await
23.2.1 async Modifier
23.2.2 await Expression
23.2.3 async, await and Threads
23.3 Executing an Asynchronous Task from a GUI App
23.3.1 Performing a Task Asynchronously
A Compute-Intensive Algorithm: Calculating Fibonacci Numbers Recursively
Exponential Complexity
23.3.2 Method calculateButton_Click
23.3.3 Task Method Run: Executing Asynchronously in a Separate Thread
23.3.4 awaiting the Result
23.3.5 Calculating the Next Fibonacci Value Synchronously
23.4 Sequential Execution of Two Compute-Intensive Tasks
23.5 Asynchronous Execution of Two Compute-Intensive Tasks
23.5.1 awaiting Multiple Tasks with Task Method WhenAll
23.5.2 Method StartFibonacci
23.5.3 Modifying a GUI from a Separate Thread
23.5.4 awaiting One of Several Tasks with Task Method WhenAny
23.6 Invoking a Flickr Web Service Asynchronously with HttpClient
23.6.1 Using Class HttpClient to Invoke a Web Service
23.6.2 Invoking the Flickr Web Service’s Invoking the Flickr Web Service’s flickr.photos.search Method
23.6.3 Processing the XML Response
XML Elements and Attributes
Class XDocument and LINQ to XML
23.6.4 Binding the Photo Titles to the ListBox
23.6.5 Asynchronously Downloading an Image’s BytesAsynchronously Downloading an Image’s Bytes
23.7 Displaying an Asynchronous Task’s ProgressDisplaying an Asynchronous Task’s Progress
23.8 Wrap-Up
Summary
Section 23.1 Introduction
Section 23.2.1 async Modifier
Section 23.2.2 await Expression
Section 23.2.3 async, await and Threads
Section 23.3.1 Performing a Task Asynchronously
Section 23.3.2 Method calculateButton_Click
Section 23.3.3 Task Method Run: Executing Asynchronously in a Separate Thread
Section 23.3.4 awaiting the Result
Section 23.3.5 Calculating the Next Fibonacci Value Synchronously
Section 23.4 Sequential Execution of Two Compute-Intensive Tasks
Section 23.5 Asynchronous Execution of Two Compute-Intensive Tasks
Section 23.5.1 awaiting Multiple Tasks with Task Method WhenAll
Section 23.5.3 Modifying a GUI from a Separate Thread
Section 23.5.4 awaiting One of Several Tasks with Task Method WhenAny
Section 23.6 Invoking a Flickr Web Service Asynchronously with HttpClient
Section 23.6.1 Using Class HttpClient to Invoke a Web Service
Section 23.6.2 Invoking the Flickr Web Service’s Invoking the Flickr Web Service’s flickr.photos.search Method
Section 23.6.3 Processing the XML Response
Section 23.6.4 Binding the Photo Titles to the ListBox
Section 23.6.5 Asynchronously Downloading an Image’s Bytes Asynchronously Downloading an Image’s Bytes
Section 23.7 Displaying an Asynchronous Task’s Progress Displaying an Asynchronous Task’s Progress
Terminology
Self-Review Exercises
Exercises
Chapters on the Web
A Operator Precedence Chart
B Simple Types
C ASCII Character Set
Appendices on the Web
Index
Symbols
Numerics
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Appendices on the Web
Next
Next Chapter
Symbols
Index
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset