- •Contents
- •What Is C#?
- •C# Versus Other Programming Languages
- •Preparing to Program
- •The Program Development Cycle
- •Your First C# Program
- •Types of C# Programs
- •Summary
- •Workshop
- •C# Applications
- •Basic Parts of a C# Application
- •Structure of a C# Application
- •Analysis of Listing 2.1
- •Object-Oriented Programming (OOP)
- •Displaying Basic Information
- •Summary
- •Workshop
- •Variables
- •Using Variables
- •Understanding Your Computer’s Memory
- •C# Data Types
- •Numeric Variable Types
- •Literals Versus Variables
- •Constants
- •Reference Types
- •Summary
- •Workshop
- •Types of Operators
- •Punctuators
- •The Basic Assignment Operator
- •Mathematical/Arithmetic Operators
- •Relational Operators
- •Logical Bitwise Operators
- •Type Operators
- •The sizeof Operator
- •The Conditional Operator
- •Understanding Operator Precedence
- •Converting Data Types
- •Understanding Operator Promotion
- •For Those Brave Enough
- •Summary
- •Workshop
- •Controlling Program Flow
- •Using Selection Statements
- •Using Iteration Statements
- •Using goto
- •Nesting Flow
- •Summary
- •Workshop
- •Introduction
- •Abstraction and Encapsulation
- •An Interactive Hello World! Program
- •Basic Elements of Hello.cs
- •A Few Fundamental Observations
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Essential Elements of SimpleCalculator.cs
- •A Closer Look at SimpleCalculator.cs
- •Simplifying Your Code with Methods
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Lexical Structure
- •Some Thoughts on Elevator Simulations
- •Concepts, Goals and Solutions in an Elevator Simulation Program: Collecting Valuable Statistics for Evaluating an Elevator System
- •A Deeper Analysis of SimpleElevatorSimulation.cs
- •Class Relationships and UML
- •Summary
- •Review Questions
- •Programming Exercises
- •The Hello Windows Forms Application
- •Creating and Using an Event Handler
- •Defining the Border Style of the Form
- •Adding a Menu
- •Adding a Menu Shortcut
- •Handling Events from Menus
- •Dialogs
- •Creating Dialogs
- •Using Controls
- •Data Binding Strategies
- •Data Binding Sources
- •Simple Binding
- •Simple Binding to a DataSet
- •Complex Binding of Controls to Data
- •Binding Controls to Databases Using ADO.NET
- •Creating a Database Viewer with Visual Studio and ADO.NET
- •Resources in .NET
- •Localization Nuts and Bolts
- •.NET Resource Management Classes
- •Creating Text Resources
- •Using Visual Studio.NET for Internationalization
- •Image Resources
- •Using Image Lists
- •Programmatic Access to Resources
- •Reading and Writing RESX XML Files
- •The Basic Principles of GDI+
- •The Graphics Object
- •Graphics Coordinates
- •Drawing Lines and Simple Shapes
- •Using Gradient Pens and Brushes
- •Textured Pens and Brushes
- •Tidying up Your Lines with Endcaps
- •Curves and Paths
- •The GraphicsPath Object
- •Clipping with Paths and Regions
- •Transformations
- •Alpha Blending
- •Alpha Blending of Images
- •Other Color Space Manipulations
- •Using the Properties and Property Attributes
- •Demonstration Application: FormPaint.exe
- •Why Use Web Services?
- •Implementing Your First Web Service
- •Testing the Web Service
- •Implementing the Web Service Client
- •Understanding How Web Services Work
- •Summary
- •Workshop
- •How Do Web References Work?
- •What Is UDDI?
- •Summary
- •Workshop
- •Passing Parameters and Web Services
- •Accessing Data with Web Services
- •Summary
- •Workshop
- •Managing State in Web Services
- •Dealing with Slow Services
- •Workshop
- •Creating New Threads
- •Synchronization
- •Summary
- •The String Class
- •The StringBuilder Class
- •String Formatting
- •Regular Expressions
- •Summary
- •Discovering Program Information
- •Dynamically Activating Code
- •Reflection.Emit
- •Summary
- •Simple Debugging
- •Conditional Debugging
- •Runtime Tracing
- •Making Assertions
- •Summary
Runtime Debugging
CHAPTER 31
LISTING 31.9 TraceSwitch Entry in Config File: TraceSwitchDemo.config
<configuration>
<system.diagnostics>
<switches>
<add name=”TraceOutput” value=”3” /> </switches>
</system.diagnostics>
</configuration>
LISTING 31.10 Compilation Instructions for Listing 31.8
csc /d:TRACE TraceSwitchDemo.cs
Making Assertions
Another common debugging task is to check the state of a program at various intervals for logical consistency. This is performed with the Debug.Assert() method. By sprinkling Assert() methods at strategic points in a routine, such as preconditions, intermediate state, and post-conditions, you can verify that routine’s logical consistency. Whenever the assertion proves false, a given message is displayed in the form of a message box. Listing 31.11 has a simple program demonstrating the mechanics of the Assert() method.
LISTING 31.11 Assertion Demonstration: AssertDemo.cs
using System;
using System.Diagnostics;
///<summary>
///Assertion Demonstration.
///</summary>
class AssertDemo
{
static void Main(string[] args)
{
decimal profit = -0.01m;
// do some calculations
Debug.Assert(profit >= 0.0m,
“Illogical Negative Profit Calculation”);
}
}
643
31
UNTIMER
EBUGGINGD
644
Extreme C#
PART IV
The example in Listing 31.11 simulates some fictitious profit calculation that should never return a negative result. The Debug.Assert() method takes two parameters. The first is the logical condition to check, which should evaluate to a Boolean true or false. In this case, it’s making sure the profit is always zero or greater. The second parameter is the message to be displayed. The example forces the assertion to evaluate to false, displaying the message shown in Figure 31.1.
Assertions are designed to work only in debugging mode. Therefore, you will want to add a /define switch to the command-line when debugging. This program can be compiled with the command line in Listing 31.12.
LISTING 31.12 Compilation Instructions for Listing 31.11
csc /d:DEBUG AssertDemo.cs
FIGURE 31.1
Assertion message box.
Summary
Once appropriate statements and methods are in place, runtime debugging can make program verification more efficient by watching console printouts of viewing log files for pertinent results. Runtime debugging can be turned on and off with conditional attributes, specialized output methods that accept Boolean parameters, command line options, and preprocessing directives.
The Debug class is effective in development environments where the debugging code will be removed for deployment. Alternatively, the Trace class would be the best decision for situations where code should be deployed with a debugging capability.
Runtime debugging in trace-enabled code can be controlled with Boolean switches or multilevel trace switches. Each option provides a means of controlling the level of debugging with less disruption to a customer.
The Debug.Assert() method assists in verifying the logical consistency of an application during debugging. When a specified constraint fails, the Assert() method notifies the user with a message box displaying information about the reason for the failure.
Runtime Debugging
CHAPTER 31
Runtime detection of program errors is an important capability. Similarly, it’s important to monitor the performance of a program. The next chapter, “Performance Monitoring,” shows how to capture runtime performance of an application.
645
31
UNTIMER
EBUGGINGD