Last Modified 5-1-2010




Introduction to C#

.NET Framework

VS2008 IDE

How C# is Compiled

Start Visual Studio

Windows Form App

C# OOP Coding

C# Code Example

C# Coding Rules


Error Window

C# Comments

C# Help Window

Language Essentials

C# Data Types

Declare Variables

Declare Constants

Arith. Expressions

Assign Statements

Operator Precedence

Type Casting

Math Class

C# Strings

String Esc Sequences

Convert Data Types

ToString Formatting

Variable Scope Use

C# Enumeration Use

C# Nullable Types

Program #2

C# Code Control

Relational Operators

Logical Operators

If-Else Statement

Switch Statement

C# ToString( ) Formatting Methods

C# allows several format specifiers to be used with the ToString( ) method:

Code Format Description
C or c Currency Formats the number as currency with the specified number of decimal places (default:2)
P or p Percent Formats the number as a percent with the specified number of decimal places
N or n Number Formats the number with thousands separators and the specified number of decimal places
F or f Float Formats the number as a decimal with the specified number of decimal places.
D or d Digits Formats an integer with the specified number of digits
E or e Exponential Formats the number in scientific notation with the specified number of decimal places
G or g General Formats the number as a decimal or in scientific notation depeding on which is more compact.

Figure CS-44: C# ToString Standard Numeric Formatting Codes

You can include a number after the D or d formatting code to specify the minimum number of digits in the result. If the integer has fewer digits than are specified, zeros are added to the beginning of the integer.

You can include a number after some of the numeric formatting codes to specify the number of decimal places in the result. If the numeric value contains more decimal places than are specified, the result will be rounded using standard rounding. If you don't specify the number of decimal places, the default is 2:

How to use the ToString method to format a number:
Statement Example
string MonthlyAmount = Amount.ToString("c");
string InterestRate = Interest.ToString("p1");
string QuantityString = Quantity.ToString("n0");
string PaymentString = Payment.ToString("f3");

How to use the Format method of the String class to format a number:
Statement Example
string MonthlyAmount=String.Format("{0:c}", 1547.2m);
string InterestRate=String.Format("{0:p1}", .023m);
string QuantityString=String.Format("{0:n0"}, 15000);
string PaymentString=String.Format("{0:f3"}, 432.8175);

Figure CS-45: C# Number Formatting Methods and Examples

How to work with Scope in C#


  • The scope of a variable determines what code has access to it. If you try to refer to a variable outside of its scope, it will cause a build error.

  • The scope of a variable is determined by where you declare it. If you declare a variable within a method, it has method scope. If you declare a variable within a class but not within a method, it has class scope.

  • A variable with method scope can only be referred to by statements within that method. A variable with class scope can be referred to by all of the methods in a class.

  • The lifetime of a variable is the period of time that it's available for use. A variable with method scope is only available while the method is executing. A variable with class scope is available while the class is instantiated.

  • You can declare class variables right after the code that is generated for a form.

The following diagram illustrates the concepts of class scope and method scope:

Scope Diagram

Figure CS-46: C# Example of Class and Method Scope Variables and Data

How to declare and use enumeration in C#


  • An enumeration defines a set of related constants. Each constant is known as a member of the enumeration.
  • By default, an enumeration uses the int type and sets the first constant to 0, the second to 1, and so on.
  • To use one of the other integer data types, you can code a colon after the enumeration name followed by the data type.
  • To specify other values for the constants, you can code an equals sign after the constant name followed by the integer value.

Enumeration Diagram

Figure CS-47: C# Enumeration Declaration and Example Usage

How to Work with Nullable Types in C#


  • A nullable type is a value type that can store a null value. Null values are typically used to indicate that the value of the variable is unknown.
  • To declare a nullable type, code a question mark (?) immediately after the keyword for the value type.
  • If you use a variable with a nullable type in an arithmetic expression and the value of the variable is null, the result of the arithmetic expression is always null.
  • You can only declare value types as nullable types. However, because reference types (such as strings) can store null values by default, there's no need to declare reference types as nullable, and your code won't compile if you try to do that.
How to Declare a Value Type that can Contain Null Values
	int? Quantity;
	Quantity = null;
	Quantity = 0;
	Quantity = 20;
How to Check if a Nullable Value Type Contains a Value
	bool HasValue = Quantity.HasValue;
How to get the Value of a Nullable Type
	int Qty = Quantity.Value;
How to use Nullable Types in Arithmetic Expressions
	double? Sales1 = 3267.58;
	double? Sales2 = null;
	double? SalesTotal = Sales1 + Sales2;	// result = null

Figure CS-48: C# Null Value Type Declarations and Examples

C# Program #2 Invoice Total 2

In order to solidify some of the concepts worked on in the last section. We will expand on the Invoice Total Program, and call it Invoice Total 2. If you have closed out Visual Studio, open the project as indicated in the diagram below:

VS2008 Splash Screen

Figure CS-49: Opening a Recent Project in Visual Studio 2008

The form needs 3 more TextBoxes, 3 more labels and 1 more button. Add the controls to the form and change the properties as indicated in the diagram below:

Invoice Total Form

Figure CS-50: Design Form for Invoice Total #2

The table below is a summary of the controls that need to be added in tabular form:

Control Type Property to be Changed New Value
Label Text Number of Invoices
TextBox (Name) txtNumberOfInvoices
Label Text Total of Invoices
TextBox (Name) txtTotalOfInvoices
Label Text Invoice Average
TextBox (Name) txtInvoiceAverage
Button (Name) btnClearTotals

Figure CS-51: New Controls and Properties for Invoice #2 Form

Once the controls have been placed on the form, and the property values modified as indicated in Figures CS-50 & CS-51. Modify the code to accomodate the variables with Class Scope and the additional controls as indicated in the diagram below:

Code Listing

Figure CS-52: Invoice Total #2 Code Listing

When the code has been entered, and all errors have been eliminated start the program running by pressing <F5> or the Start Debugging Icon: Start Debugging Symbol. The form should display similar to the illustration below:

Invoice Total At Runtime

Figure CS-53: Extended Invoice Form At Runtime

The values of $200, $300 & $400 were entered at runtime into the form illustrated above. Notice that since the NumberOfInvoices and TotalOfInvoices have Class Scope the values continue to exist even after the Process Event of btnCalculate has terminated.