
- •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

72 |
Day 3 |
LISTING 3.9 continued
15:Console.WriteLine(“\nmy_variable is {0}”, my_variable );
16:Console.WriteLine(“\nPI is {0}”, PI );
17:}
18:}
OUTPUT |
my_variable is 4 |
|
|
|
PI is 3.1459 |
ANALYSIS Lines 12 and 13 declare an int and a double. Lines 15 and 16 print these values. This listing operates like those you’ve seen earlier, except it uses the .NET data
types.
In your C# programs, you should use the simple data types rather than the .NET types. All the functionality that the .NET types have is available to you in the simpler commands that C# provides. You should, however, understand that the simple C# data types translate to .NET equivalents. You’ll find that all other programming languages that work with the Microsoft .NET types also have data types that translate to these .NET types.
Note
The Common Type System (CTS) is a set of rules that data types within the CLR must adhere to. The simple data types within C# adhere to these rules, as do the .NET data types. If a language follows the CTS in creating its data types, the data created and stored should be compatible with other programming languages that also follow the CTS.
Literals Versus Variables
In the examples you’ve looked at so far, you have seen a lot of numbers and values used that were not variables. Often, you will want to type a number or value
into your source code. A literal value stands on its own within the source code. For example, in the following lines of code, the number 10 and the value “Bob is a fish” are literal values. As you can see, literal values can be put into variables.
int x = 10;
myStringValue = “Bob is a fish”;
Numeric Literals
In many of the examples, you have used numeric literals. By default, a numeric literal is either an int or a double. It is an int if it is a whole number, and it is a double if it is a floating-point number. For example, consider the following:

Storing Information with Variables |
73 |
nbr = 100;
In this example, 100 is a numeric literal. By default, it is considered to be of type int, regardless of what data type the nbr variable is. Now consider the following:
nbr = 99.9;
In this example, 99.9 is also a numeric literal; however, it is of type double by default. Again, this is regardless of the data type that nbr is. This is true even though 99.9 could be stored in a type float. In the following line of code, is 100. an int or a double?
x = 100.;
This is a tough one. If you guessed int, you are wrong. Because there is a decimal |
|
included with the 100, it is a double. |
|
Integer Literal Defaults |
3 |
When you use an integer value, it is put into an int, uint, long, or ulong depending on its size. If it will fit in an int or uint, it will be. If not, it will be put into a long or ulong. If you want to specify the data type of the literal, you can use a suffix on the literal. For example, to use the number 10 as a literal long value(signed or unsigned), you write it like the following:
10L;
You can make an unsigned value by using a u or a U. If you want an unsigned literal long value, you can combine the two suffixes: ul.
Note
The Microsoft C# compiler gives you a warning if you use a lowercase l to declare a long value literal. The compiler provides this warning to help you be aware that it is easy to mistake a lowercase l with the number 1.
Floating-Point Literal Defaults
As stated earlier, by default, a decimal value literal is a double. To declare a literal that is of type float, you include an f or F after the number. For example, to assign the number 4.4 to a float variable, my_float, you use the following:
my_float = 4.4f;
To declare a literal of type decimal, you use a suffix of m or M. For example, the following line declares my_decimal to be equal to the decimal number 1.32.
my_decimal = 1.32m;

74 |
Day 3 |
Boolean Literals (true and false)
Boolean literals have already been covered. The values true and false are literal. They also happen to be keywords.
String Literals
When you put characters together, they make words, phrases, and sentences. In programming parlance, a group of characters is called a string. A string can be identified because it is contained between a set of double quotes. You have actually been using strings in the examples so far. For example, the Console.WriteLine routine uses a string. A string literal is any set of characters between double quotes. The following are examples of strings:
“Hello, World!”
“My Name is Bradley”
“1234567890”
Because these numbers are between quotation marks, the last example is treated as a string literal rather than as a numeric literal.
Note
You can use any of the special characters from Table 3.3 inside a string.
Constants
In addition to using literals, there are times when you want to put a value in a variable and freeze it. For example, if you declare a variable called PI and you set it to 3.1459, you want it to stay 3.1459. There is no reason to change it. Additionally, you want to prevent people from changing it.
To declare a variable to hold a constant value, you use the const keyword. For example, to declare PI as stated, you use the following:
const float PI = 3.1459;
You can use PI in a program; however, you will never be able to change its value. The const keyword freezes its contents. You can use the const keyword on any variable of any data type.

Storing Information with Variables |
75 |
Tip
To help make it easy to identify constants, you can enter their names in all capital letters. This makes it easy to identify the fact that the variable is a constant.
Reference Types
To this point, you have seen a number of different data types. C# offers two primary ways of storing information: by value (byval) and by reference (byref). The basic data types that you have learned about store information by value.
When a variable stores information by value, the variable contains the actual information. |
|
For example, when you store 123 in an integer variable called x, the value of x is 123. |
3 |
The variable x actually contains the value 123. |
Storing information by reference is a little more complicated. If a variable stores by reference, rather than storing the information in itself, it stores the location of the information. In other words, it stores a reference to the information. For example, if x is a “by reference” variable, it contains information on where the value 123 is located. It does not store the value 123. Figure 3.1 illustrates the difference.
FIGURE 3.1 |
|
|
By reference versus by |
|
|
value. |
1 2 3 |
Memory |
|
||
X_byref |
X_byval |
|
The data types used by C# that store by reference are
•Classes
•Strings
•Interfaces
•Arrays
•Delegates
Each of these data types is covered in detail throughout the rest of this book.