Computing with C# and the .NET Framework Copyright 2003 by Art Gittleman Table of Contents 1. Computing with C# 1.1 Introduction to Computing Hardware Software High-Level Languages Compilers Interpreters Newtorks 1.2 The .NET Framework The Common Language Runtime The .NET Framework Class Library 1.3 Overview of C# History C#'s Features How C# Works 1.4 The Elements of a C# Program Lexical Structure Syntax 1.5 Compiling and Running C# Programs Using the Microsoft .NET Framework Software Development Kit Using Visual Studio .NET 1.6 Developing a C# Program Levels of Abstraction The Steps of Software Development Requirements Design Implementation Testing Making Improvements 2. C# Progamming Basics 2.1 Variables Identifiers Keywords The Character Set Type int Initialization 2.2 Assignment Constructing a Simple Program Constants 2.3 Input and Output Inputting from the Console Inputting an Integer Output to the Console Outputting a Table 2.4 Arithmetic Expressions Precedence of Arithmetic Operators Combining Assignment and Arithmetic Increment and Decrement Operators 2.5 Methods and Parameters Methods Passing by Value Programming with Methods 3. Software Engineering with Control Structures 3.1 Relational Operators and Expressions 3.2 If and If-Else Statements The If Statement Control Flow The If-Else Statement Blocks 3.3 The Type Double Scientific Notation Double Values Output Input Mixed-type Expressions 3.4 Program Design with the While Loop Repetition Planning Programs using Pseudocode Loop Termination Finding the Maximum 3.5 Debugging Getting Information Testing Carefully Using the Visual Studio .NET Debugger 4. More Control Structures and Types 4.1 The AND, OR, and NOT Operators Conditional AND Conditional OR Logical Complement Short-Circuiting Operator Precedence Combining AND with OR 4.2 Nested-Ifs and the Switch Statement Nested If Statements Pairing Else with If The Switch Statement 4.3 The For and Do Loops The For Statement The Do Statement 4.4 Additional Primitive Types and Enumerations The Char Type The Long Type Other Predefined Types Enumerations 4.5 Using the Math Library Powers and Roots Maximum, Minimum, and Absolute Value Floor and Ceiling Pi and e 4.6 Solving Problems with C#: Iterative Development Defining the Problem Toward a Solution: The Iterative Process -- Develop pseudocode, a C# program, and Test Toward a Solution: First Iteration -- Pseudocode, Program, Test Toward a Solution: Second Iteration -- Pseudocode, Program, Test Completing the C# Code: Third Iteration -- Pseudocode, Program, Test Testing the C# Code 5. Getting Started with Object-Oriented Programming 5.1 The Object Concept Objects Using Objects Defining an Object Type Instantiating Objects 5.2 Introduction to Object-Oriented Design A Definition Use Cases and Scenarios Scenarios for an ATM system Classification 5.3 Classes in C# The Structure of the BankAccount Class Instance Variables Instance Methods Constructors 5.4 Using C# Objects Reference Types Creating Objects Making an Object Behave Method Overloading 5.5 Class Variables and Methods 5.6 The QuickFood Example Class Diagrams Sequence Diagrams 6. Working with Objects 6.1 The String Class Visualizing a String Creating a String A String Property A String Indexer String Methods Overloaded Methods Class Methods 6.2 String Operations Assignment Equality Operator Overloading Comparing Strings 6.3 Object Composition 6.4 Interfaces Declaring an Interface Implementing an Interface 6.5 Random Numbers and Simulation The Random Class Simulation 7. Arrays 7.1 Using Arrays Changing an Array 7.2 Creating, Inputting, and Copying an Array Array Variables and Values Creating an Array Inputting an Array Copying an Array 7.3 Arrays of Objects and a Simulation Arrays of Objects A Simulation 7.4 Multidimensional Arrays An Array of Arrays 7.5 (Optional) Solving Problems with Java: Insertion Sort Defining the Problem Toward a Solution: Developing the Pseudocode Toward a Solution: Alternatives Completing the C# Code Testing the Code 8. Event-Driven Programming 8.1 Paint Events Graphics Mode Forms 8.2 Drawing Shapes Graphics Drawing Drawing Relative to the Screen 8.3 Drawing Text Fonts 8.4 Using Color Creating Colors 8.5 Mouse and Key Events Using the Mouse Using Keys 8.6 (Optional) Solving Problems with C#: The Tangram Puzzle Defining the Problem Completing the C# Code Testing the Code 9. User Interfaces 9.1 Controls and Event-Handling The Control Hierarchy Buttons Events Delegates 9.2 Using Controls TextBox Radio Buttons and Combo Boxes List, Picture, and Check Boxes StringBuilder 9.3 (Optional) Using Visual Studio .NET Creating a Project 9.4 (Optional) A GUI for Insertion Sorting Defining the Problem Designing a Solution: The Exploratory Process Designing a Solution: Making a Chart Designing a Solution: Sorting Designing a Solution: The User Interface Completing the C# Code: Making the User Interface Work Testing the Code 10. Inheritance 10.1 Derived Classes Classification Revisited Class Hierarchies Constructing a Derived Class Adding State and Behavior to a Derived Class Overriding Behavior Virtual Methods The Object Classs 10.2 Polymorhphism Polymorphic Operations Compile-time and Run-time Types The BankAccount Hierarchy 10.3 Abstract Classes Declaring an Abstract Class Polymorphism and Abstract Classes A Shape Class Line and Circle 10.4 Modifiers and Access Class Visibility Data Field and Method Visibility Access to Data Fields Access to Methods 10.5 (Optional) Object-Oriented Design with Use Cases and Scenarios Defining the Problem Object-Oriented Design--Developing Scenarios Object-Oriented Design--Assigning Responsibilities Object-Oriented Design--Defining the Classes Completing the C# Code Testing the Code 11. Exception Handling and Input/Output 11.1 Exception Handling Program Arguments Exception Classes The Index Out of Range Exception The Format Exception 11.2 Text Input and Output Reading from a File Reading Lines and Fields Writing to a File 11.3 Binary Input and Output The FileInfo Class Reading and Writing Bytes Reading and Writing Primitive Types Random Access Files 11.4 Menus and File Dialogs 12. Data Structures 12.1 Recursion Binary Search Merge Sort 12.2 Linked Lists 12.3 Stacks and Queues Stacks Queues 12.4 Collections: ArrayList The foreach Statement Timing ArrayList Operations 12.5 Collections: Hashtable and SortedList Hashtable 12.6 Comparisons and Ordering Inheriting from Object Overriding Object Methods The IComparable Interface 13. Threads and Animation 13.1 Introduction to Threads The Thread Class Interrupting Computation 13.2 Animation 13.3 Images Double Buffering 13.4 Concurrent Programming An Example without Synchronization Synchronization Attributes Communication Deadlock 14. Networking 14.1 Requests and Responses The Uniform Resource Locator (URL) The Hypertext Transfer Protocol (HTTP) Connecting from WebClient Connecting with WebRequest and WebResponse Using HttpWebRequest and HttpWebResponse 14.2 Clients and Servers Server Ports Handling HTTP Explicitly A Client-Server Example 14.3 Browsers and Web Servers A Very Simple Web Browser A Very Simple Web Server A Threaded Web Server 14.4 Remoting Distributed Computing The Interface The Implementation The Server The Client 15. Using a Database 15.1 Database Tables and SQL Queries Relational Database Tables Structured Query Language 15.2 Connecting to a Database Database and Application Servers Creating the Database Connecting from C# Buidling the Database 15.3 Retrieving Information Viewing Query Results SELECT Statement Options 15.4 Database Information and Aggregate Functions Database Information Creating a Data Set Aggregate Functions 15.5 Stored Procedures and Transactions Using Stored Procedures Transaction Processing 15.6 A GUI for Database Queries 16. ASP.NET 16.1 HTML Some HTML Tags 16.2 Web Server Controls and Code Behind Hosting a Web Page Adding Code to a Web Page Code Behind Posting Data 16.3 Three-tiered Architectures Using a Database in a Three-tiered Architecture 16.4 Session Tracking 17. XML and Web Services 17.1 XML and Information The Limitations of HTML XML Syntax Schemas Valid Documents 17.2 DOM (Document Object Model) Processing Building a DOM Tree from an XML File A Simplified Document and Schema Checking for Well-Formed and Valid Documents Building a DOM Tree from Data 17.3 XSLT (Extensible Stylesheet Language for Transformations) Stylesheets Using Internet Explorer Using the .NET Framework Stylesheet Templates A Stylesheet for the Author Document Using a Stylesheet 17.4 Web Services SOAP and WSDL Using a Web Service Creating a Web Service Appendices A. Binary and Hexadecimal Numbers B. Bitwise and Shift Operators C. Operator Precedence Rules D. The ASCII Character Set E. Simple Types Answers To Selected Exercises