- •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
82 C# PRIMER PLUS
A Few Fundamental Observations
The previous sections were able to extract many fundamental C# constructs and mechanisms from Listing 3.1, despite its simple appearance. This section looks at a few general C# concepts by summarizing and building on the knowledge you have gained from Listing 3.1.
C#’s Source Code Format
Blank lines, space characters, tab characters, and carriage returns are collectively known as whitespace. To a large degree, the C# compiler ignores whitespace. Consequently, you can use blank lines, space characters, tab characters, and carriage returns interchangeably. Let’s look at an example. From the C# compilers point of view, the three lines of source code in Figure 3.10 are all identical to line 5 of Listing 3.1.
05: public static void Main()
FIGURE 3.10
Whitespace is ignored by the compiler.
token
public static void Main ()
whitespace (carriage return) whitespace (space character)
Space characters and tab characters can be used interchangeably
token
public |
static |
void |
Main |
( |
) |
whitespace (carriage return) whitespace (tab characters)
Space characters, tab characters, blank lines and carriage returns can be used interchangeably
space characters
space characters |
public |
blank line
static void
valid but ugly code
Main (
)
carriage return
tab character
Notice how distorted the third line in Figure 3.10 has become; it is still valid code but incomprehensible and ugly.
Chapter 3 • A GUIDED TOUR THROUGH C#: PART I 83
The indivisible elements in a line of source code are called tokens. One token must be separated from the next by whitespace, commas, or semicolons. However, it is unacceptable to separate the token itself into smaller pieces by using whitespace or separators.
A token is a particular instance of a word with a special meaning attached to it. The term token is also frequently used in the scientific fields of logic and linguistics.
Don’t break up tokens with whitespace; this causes invalid code as shown below, which contains three invalid versions of line 5: public static void Main():
pub lic stati c void Main()
public static void Ma
in()
public static vo id Main()
On the other hand, it is possible to aggregate statements and source code on the same line, as shown in the following line.
class Hello { public static void Main() { string answer;
The following is better-styled original code:
02: class Hello 03: {
04:// The program begins with a call to Main()
05:public static void Main()
06:{
07:string answer;
Although C# provides much freedom when formatting your source code, you can increase its clarity considerably by following a reasonable style. It is possible to write valid but ugly source code, as you have seen in a few of the previous examples. Ugly here refers to messy, unclear, confusing source code that is difficult for another person to comprehend.
Listing 3.1 follows a certain style that is adhered to by a large proportion of the programming community. Let’s have a look at a few general guidelines. Line numbers from Listing 3.1 are provided as examples.
•Have one statement per line (lines 7, 9, 10, 11, and 14).
However, note the if statement; it’s an exception. It should be spread over several lines (in the case of Listing 3.1 it is spread over two lines).
•After an opening brace ({) move down one line and indent (lines 4, and 7).
•Indent matching pairs of braces identically (lines 3, 16 and lines 6, 15).
•While observing the two previous rules, indent lines between matching pairs of braces identically. (Lines 4, 5 and lines 7–14). Keep in mind that statements after if conditions should be indented (line 13).
84C# PRIMER PLUS
•Put in blank lines to separate distinctive logic parts of the source code (line 8).
•Do not use whitespace around the parentheses associated with a function name (line 5).
Competent programmers apply other conventions, to improve the style of their source code. We will discuss these in the following chapters whenever relevant.
A Brief Tour Around the .NET Framework
We have already made extensive use of the .NET Framework class library in Listing 3.1. It’s especially visible in lines 9, 10, 11, 13, and 14 with System.Console.WriteLine and System.Console.ReadLine. There is extensive support for equipping your program with a wide variety of functionality from this class library. But how do we know which particular classes and methods we have access to and can reuse and how do we know their characteristics and the functionality they provide? We can find answers to these questions in the comprehensive documentation provided for the class library. The simple aim of this section is to show you how to locate this documentation and confirm the existence of
System.Console.WriteLine and System.Console.ReadLine inside the myriad of other classes and methods.
At the time of writing, the following set of commands can be used to locate the .NET Framework documentation. This might change in later versions of the Software Development Kit (SDK).
Open up the directory with the path D:\Program Files\Microsoft.Net\FrameworkSDK.
Notice that D:\ could be another letter, depending on where your documentation has been installed.
You should then see a window displaying the contents of the FrameworkSDK folder, similar to that shown in Figure 3.11.
FIGURE 3.11
Contents of the
FrameworkSDK folder.
Double-click the StartHere icon to bring up the start page of the .NET Framework Reference. Display the start page of the .NET Framework Documentation by clicking the hyperlink called
Chapter 3 • A GUIDED TOUR THROUGH C#: PART I 85
.NET Framework SDK documentation situated in the Documentation section. Expand the appearing .NET Framework SDK node on the left hand side of the window. Locate the .NET Framework Reference node and expand it. Among many other appearing nodes one is called
.NET Framework Class Library which you can expand to display a window similar to that shown in Figure 3.12.
FIGURE 3.12
The .NET Framework
Class Library
Documentation.
You are now free to browse the documentation for the class library.
To locate the Console class, expand the System node and scroll down to find and expand the Console Class node. You can now find the WriteLine and ReadLine methods by clicking the Console Members node. Click the ReadLine hyperlink in the right part of the window to see the window shown in Figure 3.13.
FIGURE 3.13
Displaying the
Console.ReadLine specifications.