+ All Categories
Home > Documents > Introduction to C#

Introduction to C#

Date post: 16-Nov-2014
Category:
Upload: api-3729173
View: 193 times
Download: 1 times
Share this document with a friend
Popular Tags:
483
Introduction to C# Introduction Overview of C# C#, pronounced c sharp, is a computer language used to give instructions that tell the computer what to do, how to do it, and when to do it. This is a universal language that, although currently mostly used on the Microsoft Windows operating system, will be also used on other operating systems like Linux, BeOS, Macintosh, etc. C# is one of the languages used in the Microsoft .NET Framework. The Microsoft .NET Framework is a library of objects that create or draw things on the computer. The C# language is used to create applications that display on a black window referred to as the DOS prompt or DOS window. Introduction to Objects An object, any object, is made of different parts. For example, a car is made of tires, doors, wires, belts, etc: In computer languages, such an object is called a class. In the C# language, everything is built as a class. Therefore, a program starts with the class keyword followed by a name. Here is an example: class Car The actual building of a class starts with an opening curly bracket "{" and ends with a closing curly bracket "}". Therefore, a class can be created as follows: 1
Transcript
Page 1: Introduction to C#

 

Introduction to C# 

Introduction 

Overview of C#

C#, pronounced c sharp, is a computer language used to give instructions that tell the computer what to do, how to do it, and when to do it. This is a universal language that, although currently mostly used on the Microsoft Windows operating system, will be also used on other operating systems like Linux, BeOS, Macintosh, etc. C# is one of the languages used in the Microsoft .NET Framework. The Microsoft .NET Framework is a library of objects that create or draw things on the computer.

The C# language is used to create applications that display on a black window referred to as the DOS prompt or DOS window.

Introduction to Objects

An object, any object, is made of different parts. For example, a car is made of tires, doors, wires, belts, etc:

In computer languages, such an object is called a class.

In the C# language, everything is built as a class. Therefore, a program starts with the class keyword followed by a name. Here is an example:

class Car

The actual building of a class starts with an opening curly bracket "{" and ends with a closing curly bracket "}". Therefore, a class can be created as follows:

 

1

Page 2: Introduction to C#

class Car { }

Because a class can be made of numerous parts, such as tires, doors, seats, etc, listed as sub-objects of a class, one line could become (too) crowded. Therefore, each curly bracket should be displayed on its own line:

class Car{}

The assignments of a program written in C# are carried in entities called methods. A method is simply a section of code that takes care of a particular detail for the functionality of the program. To create a method, you must specify its name, which starts with a letter or an underscore and can be followed by letters, digits, or underscores. The name of a method is followed by an opening and a closing parentheses. Here is an example:

class Car{Drive()}

A method's job is to carry a specific assignment within a program. As such, it could provide a value once the assignment has been carried. In Lesson 8, we will review the types of values that a method can provide. If on the other hand a method doesn't give back a value, then it is considered void. The type of value that a method can provide (or return) is written on the left side of the method name. An example would be:

class Car{void Drive()}

The assignment that a method carries is included between an opening curly bracket "{" and a closing curly bracket "}". Therefore, a method can be written as follows:

class Car{void Drive(){}}

Indentation is a technique of specifying where a line of code starts and where it ends. Although indentation is not part of the C# language, it makes the program easier to read. Therefore, the above lines of code can be written as follows:

class Car{

void Drive(){}

}

Note that this time, it is easier to know where the class starts and ends, and where the method starts and ends. In some occasions (we will see when and why), we will need to write a method

2

Page 3: Introduction to C#

as "static".

The most regularly used method of a C# program is called Main. Therefore, the minimum you can write a program with, is as follows:

class Car{

static void Main(){}

}

A computer program is started in a regular computer file. A file for a C# program has the extension .cs. To make your programming fast, the C# language ships with many primary files that allow you to lay a foundation for your program.

 

Command Line Fundamentals 

Introduction

When the computer starts, there is no primary visual or graphical effect. The programs must first be loaded into memory. In fact, in the beginning, the Microsoft operating system started as text-based with DOS 2.2, 3.3, 5.0, and 6.2 (6.2 was never a big hit like 5.0 because Windows 3.1, Windows 3.2, and Windows 3.3 was released at the same time, bringing the now attractive "Windows" display). To create a program, a programmer entered the source code using a text editor (there was a text editor called EDIT; it was very cute, very basic, and very wonderful; in fact, up to Windows 3.3, it was still the text editor of choice of most programmers and it was used as the editor for QBasic, the parent of Visual Basic). After creating the source file, the programmer had to use a black (or "amber") screen that displayed a blinking caret whose role was to let the user know that it was waiting for a command.

A command was a word or a group of words that the programmer typed and pressed Enter (or Return). This caused the computer to produce an action. The command was typed on a line. For this reason, the area that displayed the caret was called the Command Line. Most low-level (low-level doesn't mean weak, to the contrary) programs are still using this concept. This means that, behind the scenes, computer languages such as C/C++, Pascal, Java, C#, etc, are still created and executed as if things were done at the Command Line.

 

Command Line Understanding

To be an effective programmer, it is important to know how the Command Line works because, as mentioned already, C# programs are compiled at the Command Prompt. If you know how the Command Line works, you can have a better understanding of things that are going on behind the scenes.

As its name indicates, the Command Line is a blank line with a blinking caret that expects you to type a command:

3

Page 4: Introduction to C#

There are various types of commands used on the command prompt. Some of them are as old as the operating system. Such is the case for commands used to create files or folders, copy files or folders, delete files or folders, format a drive, etc. Some other commands are created by a person like you who needs it for a particular application. For example, the C# language provides a command called csc to compile a program at the command line. In the same way, when you create a program, particularly when you create a source file, you can use some options that would be used at the command prompt.

The Compiler

You may have realized that all of the words we have used so far, such as class, void, or static, are regular English words that you can read and whose meaning you can find in a dictionary. Computer languages are created with meaningful words like that to make them easier to read and understand. Unfortunately, the computer doesn't understand those words and cannot execute the instructions those words represent. Like most other languages, C# is just a language that is used to give instructions to the computer. To make the computer understand these instructions, you need another, somehow more powerful, program. This program would receive the language's instructions in plain English and "translate" them in another language that the computer can understand. This program is called a compiler.

A compiler is a computer program made of internal other sub-programs. One of the sub-programs, in fact probably the first, of a compiler is called a parser. A parser "scans" a file that contains (part of) the program. It checks for syntax, keywords, unknown words, and some other routines. If the parser finds a problem, which could be anything, either it stops or it continues making a list of the mistakes it found. Then it displays this list to you to fix. Sometimes it would point to the exact line where the/a problem was found. Sometimes it would point to the line where the problem showed its impact although the problem may be found somewhere else. With experience, you will know how to fix the programs or troubleshoot these problems. On this site, we will address as many issues as possible.

If the parser doesn't find any problem, or after you have fixed the problems, it (the parser) passes its result(s) to the compiler. The compiler calls another program called a linker. If the program contains just one file, the linker considers it. If the program contains more than one file (because a program can contain as many files as necessary), the linker considers them. The linker gathers some of the files that the C# compiler shipped with (those files that your program needs in order to work, since your program doesn't need all possible files that C# ships with), puts them together ("links" them) with your file(s) to get your instructions in a manner that can produce a suitable result. If there is no significant problem, the compiler creates the program. This doesn't mean that everything is alright, it only means that the compiler thinks that everything is alright: it is still possible that the result may not be what you would expect. To make your life easier, all of the sub-programs (parser, linker, debugger, etc) that ship with C# are grouped in one large program: the compiler. Therefore, from now on, we will use the word "compiler" to refer to the program you use to "translate" your English-based instructions into a

4

Page 5: Introduction to C#

computer-based language.

The compiler that ships with the C# version we will use, that is, the compiler of the .NET Framework is a program called csc. Like most other programs, it has the extension .exe. This csc name is not standard. This means that another C# compiler may have another name; csc.exe is just the name of the compiler we will use.

 

Creating a Program

On this site, we will use Microsoft Visual Studio .NET to create our programs. To use it, you have two main options. You can start from an empty project and add the necessary files, or you can create a console application using its Option.

 

Practical Learning: Creating a Program1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with Exercise1 and specify the desired path in the Location 

6. Click OK

5

Page 6: Introduction to C#

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise 

10. Click Open

11. In the empty file, type the following: 

class OrderProcessing{

static void Main(){}

}

12. To save the file, on the main menu, click File -> Save

6

Page 7: Introduction to C#

13. To execute the application, on the main menu, click Debug -> Start Without Debugging 

14. Press Enter to close the DOS window and return to Visual C#

 

Namespaces 

Introduction

When many people work in creating the same program, it could be difficult to keep track of the names of various classes. If more than one programmer creates a class with the same name in the same program, there would be conflict and the program would not work. The solution to avoid this situation is to delimit sections of code with names.

A namespace is a section of code that is identified by a specific name. The name could be anything such as somebody's name, the name of the company's department, or a city. To create a namespace, you start with the namespace keyword followed by the name of the section.

Like a class, the section that is part of a namespace starts with an opening curly bracket "{" and ends with a closing curly bracket "}". Here is an example:

namespace Jason{}

Between the curly brackets, you can type anything that is part of the namespace. For example, you can create a class inside of a namespace. Here is an example:

7

Page 8: Introduction to C#

namespace Jason{

class Airport{}

}

 

Practical Learning: Creating a Namespace

1. To create a namespace, change the file as follows: 

namespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){}

}}

2. Save the file

 

Accessing Members of a Namespace

After creating the necessary items in a namespace, you can use the period operator to access an item that is part of the namespace. To do this, in the desired location, type the name of the namespace, followed by a period, followed by the desired member of the namespace. Here is an example:

namespace Accounting{

class Departure{}

}

class MainClass{

static void Main(){

Accounting.Departure}

}

 

Namespace Nesting

Imagine that you create a namespace called Accounting and many people from the Accounting department are asked to create their methods and objects only in the Accounting namespace and you may have asked the

8

Page 9: Introduction to C#

Accounting programmers not to create their methods and objects outside of the Accounting namespace. One thing you can do is to create other namespaces inside of the existing namespace.

Creating a namespace inside of an existing namespace is referred to as nesting the namespace. The namespace inside of another namespace is nested.

To create a namespace inside of another, simply type it as you would create another namespace. Here is an example:

namespace Accounting{

namespace Jason{}

}

In the example above, the Jason namespace is nested inside of the Accounting namespace.

After creating the needed namespaces, nested or not, you can create the necessary methods and objects inside of the desired namespace. To access anything that is included in a nested namespace, you use the period operator before calling a member of a namespace or before calling the next nested namespace. Here is an example:

namespace Accounting{

class Payroll{}

}namespace Personnel{

namespace EmployeesRecords{

class TimeSheet{}

}namespace Benefits{

class MedicalInsurance{}

}}

class MainClass{

static void Main(){

Accounting.PayrollPersonnel.Benefits.MedicalInsurancePersonnel.EmployeesRecords.TimeSheet

}}

A line of code such as Accounting.Payroll is called a statement. Every statement must be terminated with a colon. Therefore, this statement may be written as:

9

Page 10: Introduction to C#

Accounting.Payroll;

 

The System Namespace 

Introduction

To make programming in C# a little easier, many classes have already been created and stored in various namespaces. Each namespace in C# is used to provide a specify set of classes. The most regularly used namespace in the C# language is called System. Inside of the System namespace is a class called Console. The Console class is used to display things on the console screen also called the DOS window.

The Console class contains assignments (called methods) to display information on the screen or to retrieve information from the user who types it in the DOS window. The method that is used to display text on the screen is called Write. To use the Write() method, inside of the parentheses, type the sentence between double-quotes. Here is an example:

class Program{

static void Main(){

System.Console.Write("The Wonderful World of C# Programming");}

}

Besides the Write() method, the Console class also provides a method called WriteLine(). The difference is that, after displaying something on the screen, the Write() method keeps the caret on the same line but WriteLine() transfers the caret the next line. We will see various examples of this behavior throughout our lessons.

 

Practical Learning: Introducing Console Methods

1. To use the System namespace, change the contents of the file as follows: 

namespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){

System.Console.WriteLine("Georgetown Cleaning Services");

}}

}

2. Execute the application again to see the result 

10

Page 11: Introduction to C#

3. Press Enter to close the window and return to Visual C#

 

The Using Keyword

We saw that, to call an object or a method that is part of a namespace, you must "qualify" that method or object using the period operator. Instead of using this approach, if you already know the name of a namespace that exists or has been created in another file, you can use a special keyword to indicate that you are using a namespace that is defined somewhere. This is done with the using keyword. To do this, on top of the file (preferably), type using followed by the name of the namespace.

With the using keyword, you can include as many external namespaces as necessary.

 

Practical Learning: Using the Keyword

1. Change the file as follows: 

using System;

namespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){

Console.WriteLine("Georgetown Cleaning Services");

}

11

Page 12: Introduction to C#

}}

2. Execute the application to test it

3. Return to Visual C#

4. To create an application using a wizard, on the main menu, click File -> New -> Project

5. In the Project Types list of the New Project dialog box, make sure Visual C# Projects is selected.In the Templates list, click Console Application

6. In the Name text box, replace the name with Exercise2 and specify the desired path in the Location 

7. Click OK

8. To change the name of the file, in Solution Explorer, right-click Class1 and click Rename

9. Type Exercise.cs and press Enter

10. To rename the class, in the Code Editor, double-click Class1 and type OrderProcessing

11. Execute the application

 

Accessories for Coding 

Comments

A comment is a line or paragraph of text that the compiler would not consider when examining the code of a

12

Page 13: Introduction to C#

program. There are two types of comments recognized by C#.

To display a comment on a line of text, start the line with two forward slashes //. Anything on the right side of // would be ignored. Here is an example:

// This line will be ignored. I can write in it anything I want

The above type of comment is used on only one line. You can also start a comment with /*. This type of comment ends with */. Anything between this combination of /* and */ would not be read by the compiler. Therefore, you can use this technique to span a comment on more than one line.

 

Practical Learning: Creating Comments

1. To create comments, change the file as follows: 

// Project Name: Exercise// Purpose: Used to process cleaning orders for customers

using System;

// Orders and business-related operations will be performed herenamespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){

Console.WriteLine("Georgetown Cleaning Services");

}}

}

2. Execute the application to test it

3. Close the window and return to Visual C#

 

Command Line Options

To see the name of the executable created for the above project, you can open Windows Explorer or My Computer, display the contents of the Exercise1 folder, followed by bin, and followed by debug. You would find a program called exercise1.exe

13

Page 14: Introduction to C#

 

By default, Microsoft Visual C# uses the name of the project to create the name of the executable of a project. In some cases, you may want to use a different name. To specify this information, you can open the properties of the project. To do this, you can click Project -> ProjectName Properties... from the main menu. Alternatively, in the Solution Explorer, you can right-click the name of the project and click Properties. Once in the properties, change the name of the Assembly Name.

 

Practical Learning: Specifying the Name of the Executable1. Click anywhere in the source file to make sure the project is selected. On the main menu, click Project ->

Exercise1 Properties...

2. To specify a custom name, delete the contents of Assembly Name and typeGeorgetown Cleaning Services 

14

Page 15: Introduction to C#

3. Click OK

4. Execute the application

5. Open Windows Explorer or My Computer and display the contents of the Exercise1\bin\debug folder to notice an executable named Georgetown Cleaning Services

6. Return to Visual C#

Unsafe Code

When C# was invented, one of its biggest goals was to avoid some of the difficulties of C/C++. Among them was the use of pointers for people who couldn't stand them or those who don't like or can't stand their intricacies. C/C++ uses pointers to refer to the area in memory where a value is located. C# highly avoids pointers and takes over memory management as opposed to letting the programmer take care of that aspect of an application. You can still use pointers in C# in extreme cases you judge them necessary.

Because the C# compiler is in charge of managing the memory used by the values of an application, pointers are said to be unsafe. If you want to use a pointer in your application, you must precede the name of every method that uses unsafe code with the unsafe keyword. To compile the application, you must indicate that you are using unsafe code. To do that, use the /unsafe modifier.

 

 

15

Page 16: Introduction to C#

 

Variables 

Overview 

Definition

A computer receives information from different applications in various forms. Sometimes a person types it using the keyboard. Sometimes the user  clicks the mouse. Sometimes information comes from another, more complicated source. The idea is that the computer spends a great deal of its time with various pieces of information. Information provided to the computer through a program is called datum and the plural is data. Sometimes the word data is used both for singular and plural items.

Data used by the computer comes and goes regularly as this information changes. For this reason, such information is called a variable.

When the user enters data in a program, the computer receives it and must store it somewhere to eventually make it available to the program as needed. For a program used to process employment applications, the types of information a user would enter into the program are the name, the residence, the desired salary, years of experience, education level, etc. Because there can be so much information for the same program, you must specify to the computer what information you are referring to and when. To do this, each category of piece of information must have a name.

Names in C#

To name the variables of your program, you must follow strict rules. In fact, everything else in your program must have a name. C# uses a series of words, called keywords, for its internal use. This means that you must avoid naming your objects using one of these keywords. They are:

abstract const extern int out short typeof

as continue false interface override sizeof uint

base decimal finally internal params stackalloc ulong

bool default fixed is private static unchecked

break delegate float lock protected string unsafe

byte do for long public struct ushort

case double foreach namespace readonly switch using

catch else goto new ref this virtual

char enum if null return throw void

checked event implicit object sbyte true volatile

 

16

Page 17: Introduction to C#

class explicit in operator sealed try while

Once you avoid these words, there are rules you must follow when naming your objects. On this site, here are the rules we will follow:

The name must start with a letter or an underscore

After the first letter or underscore, the name can have letters, digits, and/or underscores

The name must not have any special characters other than the underscore

The name cannot have a space

Besides these rules, you can also create your own but that abide by the above.

C# is case-sensitive. This means that the names Case, case, and CASE are completely different. For example, the main function is always written Main.

 

The Numeric Systems 

Introduction

When a computer boots, it “loads” the operating system. If you want to use a program, you must find it either on the Start menu or from its directory and take the necessary action to open it. Such a program uses numbers, characters, meaningful words, pictures, graphics, etc, that are part of the program. As these things are numerous, so is the size of the program, and so is the length of time needed to come up. Your job as a programmer is to create such programs and make them available to the computer, then to people who want to interact with the machine.

To write your programs, you will be using alphabetic letters that are a, b, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z. You will also use numeric symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Additionally, you will use characters that are not easily readable but are part of the common language; they are ` ~ ! @ # $ % ^ & * ( ) _ + - = : “ < > ; ‘ , . /. Some of these symbols are used in the C# language while some others are not. When creating your programs, you will be combining letters and/or symbols to create English words or language instructions.

Some of the instructions you will give to the computer could consist of counting the number of oranges, converting water to soup, or making sure that a date occurs after January 15. After typing an instruction, the compiler would translate it to machine language. The computer represents any of your instructions as a group of numbers. Even if you ask the computer to use an orange, it would translate it into a set of numbers. As you give more instructions or create more words, the computer stores them in its memory using a certain amount of space for each instruction or each item you use.

There are three numeric systems that will be involved in your programs, with or without your intervention.

 

The Binary System

17

Page 18: Introduction to C#

When dealing with assignments, the computer considers a piece of information to be true or to be false. To evaluate such a piece, it uses two symbols: 0 and 1. When a piece of information is true, the computer gives it a value of 1; otherwise, its value is 0. Therefore, the system that the computer recognizes and uses is made of two symbols: 0 and 1. As the information in your computer is greater than a simple piece, the computer combines 0s and 1s to produce all sorts of numbers. Examples of such numbers are 1, 100, 1011, or 1101111011. Therefore, because this technique uses only two symbols, it is called the binary system.

When reading a binary number such as 1101, you should not pronounce "One Thousand One Hundred And 1", because such a reading is not accurate. Instead, you should pronounce 1 as One and 0 as zero or o. 1101 should be pronounced One One Zero One, or One One o One.

The sequence of the symbols of the binary system depends on the number that needs to be represented.

The Decimal System

The numeric system that we are familiar with uses ten symbols that are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Each of these symbols is called a digit. Using a combination of these digits, you can display numeric values of any kind, such as 240, 3826 or 234523. This system of representing numeric values is called the decimal system because it is based on 10 digits.

When a number starts with 0, a calculator or a computer ignores the 0. Consequently, 0248 is the same as 248; 030426 is the same as 30426. From now on, we will represent a numeric value in the decimal system without starting with 0: this will reduce, if not eliminate, any confusion.

Decimal Values: 3849, 279, 917293, 39473Non- Decimal Values: 0237, 0276382, k2783, R3273

The decimal system is said to use a base 10. This allows you to recognize and be able to read any number. The system works in increments of 0, 10, 100, 1000, 10000, and up. In the decimal system, 0 is 0*100 (= 0*1, which is 0); 1 is 1*100 (=1*1, which is 1); 2 is 2*100 (=2*1, which is 2), and 9 is 9*100 (= 9*1, which is 9). Between 10 and 99, a number is represented by left-digit * 101

+ right-digit * 100. For example, 32 = 3*101 + 2*100 = 3*10 + 2*1 = 30 + 2 = 32. In the same way, 85 = 8*101 + 5*100 = 8*10 + 5*1 = 80 + 5 = 85. Using the same logic, you can get any number in the decimal system. Examples are:

2751 = 2*103 + 7*102 + 5*101 + 1*100 = 2*1000 + 7*100 + 5*10 + 1 = 2000 + 700 + 50 + 1 = 2751

67048 = 6*104 + 7*103 + 0*102 + 4*101 + 8*100 = 6*10000 + 7*1000+0*100+4*10+8*1 = 67048

Another way you can represent this is by using the following table:

etc Add 0 to the preceding value

1000000 100000 10000 1000 100 10 0

When these numbers get large, they become difficult to read; an example is 279174394327. To make this easier to read, you can separate each thousand fraction with a comma. Our number would become 279,174,394,327. You can do this only on paper, never in a program: the compiler would not understand the comma(s).

18

Page 19: Introduction to C#

The Hexadecimal  System

While the decimal system uses 10 digits (they are all numeric), the hexadecimal system uses sixteen (16) symbols to represent a number. Since the family of Latin languages consists of only 10 digits, we cannot make up new ones. To compensate for this, the hexadecimal system uses alphabetic characters. After counting from 0 to 9, the system uses letters until it gets 16 different values. The letters used are a, b, c, d, e, and f, or their uppercase equivalents A, B, C, D, E, and F. The hexadecimal system counts as follows: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, and f; or 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. To produce a hexadecimal number, you use a combination of these sixteen symbols.

Examples of hexadecimal numbers are 293, 0, df, a37, c23b34, or ffed54. At first glance, the decimal representation of 8024 and the hexadecimal representation of 8024 are the same. Also, when you see fed, is it a name of a federal agency or a hexadecimal number? Does CAB represent a taxi, a social organization, or a hexadecimal number?

From now on, to express the difference between a decimal number and a hexadecimal one, each hexadecimal number will start with 0x or 0X. The number will be followed by a valid hexadecimal combination. The letter can be in uppercase or lowercase.

Legal Hexadecimals: 0x273, 0xfeaa, 0Xfe3, 0x35FD, 0x32F4eNon-Hex Numbers: 0686, ffekj, 87fe6y, 312

There is also the octal system but we will not use it anywhere in our applications.

Signed and unsigned

The numbers we have used so far were counting from 0, then 1, then 2, and up to any number desired, in incrementing values. Such a number that increments from 0, 1, 2, and up is qualified as positive. By convention, you do not need to let the computer or someone else know that such a number is positive: by just displaying or saying it, the number is considered positive. This is the basis of our counting items.

In real life, there are numbers counted in decrement values. Such numbers start at –1 and move down to -2, -3, -4 etc. These numbers are qualified as negative.

When you write a number “normally”, such as 42, 502, or 1250, the number is positive. If you want to express the number as negative, you use the – on the left side of the number. The – symbol is called a sign. Therefore, if the number does not have the – symbol, C++ (or the compiler) considers such a number as unsigned. In C++, if you declare a variable that would represent a numeric value and you do not initialize (assign a value to) such a variable, the compiler will consider that the variable can hold either a signed or an unsigned value. If you want to let the compiler know that the variable should hold only a positive value, you will declare such a variable as unsigned.

 

19

Page 20: Introduction to C#

Data Types

In order to use a variable in your program, the compiler must be aware of it. Once the compiler knows about a variable, it would reserve an amount of memory space for that variable

Using its name, you can refer to a particular variable when necessary. Because there are various types of variables a program can use, such as the employee's name, the residence, the desired salary, years of experience, education level, etc for our employment application analogy, the compiler needs a second piece of information for each variable you intend to use. This piece of information specifies the amount of space that a variable needs. You can see that, to store a character, such as an employee's gender (M or F) or an answer as Y or N to a question, the compiler would certainly not need the same amount of space to store the name of the last school attended by an employee.

A data type is an amount of space needed to store the information related to a particular variable.

The name of a variable allows you and the compiler to refer to a particular category of information in your program. The data type allows the compiler to reserve an adequate amount of memory space for a variable. Because you are the one who writes a program, you also tell the compiler the amount of memory space each particular variable will need. Based on this, the C# language provides categories of data types used  to specify this amount of space needed for a variable.,/p>

As stated already, before using a variable, you must communicate your intentions to the compiler. Making the compiler aware is referred to as declaring the variable. To declare a variable, provide the data type followed by the name of the variable. Therefore, the syntax used to declare a variable is:

DataType VariableName;

In the next lesson, we will review the assignment operator. For now, we will know that we can use it to provide a new value to a variable. The assignment operator is represented by =

 

Practical Learning: Using Variables1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with GCS1 and specify the desired path in the Location

20

Page 21: Introduction to C#

6. Click OK

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise

10. Click Open

11. In the empty file, type the following: 

using System;

namespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

}}

}

12. To execute the application, on the main menu, click Debug -> Start Without Debugging

13. Press Enter to close the DOS window and return to Visual C#

 

Representing Numbers 

A Bit

The computer (or an Intel computer, or a computer that runs on an Intel microprocessor) uses the binary system to represent its information. It represents data using only a 0 or 1 value. To make an illustration, let's consider that a computer uses a (small) cup (such as a cup of coffee) to represent such a value. This cup can have only one of two states. When the cup is empty, we will give it a value of 0. When the cup is full, we give it a value of 1. The box can have only one of these two values:

0 1

Since this cup can have only one of two states, consider that you can use it to represent anything that could assume one out of two states. Examples include: True-False; Parent-Child; On-Off; Discount-NoDiscount; Male-Female; Yes-No, etc.

This technique of representing values is the same as the binary system we mentioned earlier. In the computer, it uses values 0 and/or 1, which themselves are called digits. The entity used to represent such a value is called a binary digit; in its abbreviated form, it is called a bit (for binary digit). The bit (binary digit) is the most fundamental representation of the computer's counting

21

Page 22: Introduction to C#

system.

Although the C# compiler recognizes a bit, you cannot store a variable in a bit. However, eventually, you will be able to manipulate the information stored in a bit.

 

The Four-Bit Combination

The single bit is used only to represent a tinny piece of information. To get effective numbers, the computer combines the bits. The first combination of bits consists of grouping four consecutive bits.

To count the bits, we number them starting at 0, followed by 1, 2, and 3. The count starts with the most right bit.

The first bit, on the right side of the group, is called the Low Order bit or LO bit. This is also called the least significant bit. The last bit, on the left side of the group, is called the High Order bit or HI bit; it is also called the most significant bit. The bit on the right side is counted as bit 0. The bit on the left side is counted as bit 3. The other bits are called by their positions: bit 1 and bit 2.

Once again, each bit can have one of two states. Continuing with our illustration, when a cup is empty, it receives a value of 0. Otherwise, it has a value of 1. On a group of four consecutive bits, we can have the following combinations:

This produces the following binary combinations: 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 = 16 combinations. When using the decimal system, these combinations can be represented as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15.

This combination is also a system that the computer uses to count bits internally. Sometimes, in your program or in the help files, you will encounter a number that is less than four bits, such as 10 or 01 or 101. The technique used to complete and fill out the group of 4 bits consists of

22

Page 23: Introduction to C#

displaying 0 for each non-represented bit. The binary number 10 will be the same as 0010. The number 01 is the same as 0001. The number 101 is the same as 0101. This technique is valuable and allows you to always identify a binary number as a divider of 4.

When all bits of a group of 4 are 0, the combination has the lowest value, which is 0000. Any of the other combinations has at least one 0 bit, except for the last one. When all bits are 1, this provides the highest value possible for a group of 4 bits. The lowest value, also considered the minimum value, can be represented in the decimal system as 0. The highest value, also considered the maximum, can be expressed in decimal value as 24 (2 represents the fact that there are two possible states: 0 and 1; 4 represents the fact that there are four possible combinations), which is 16. This produces 16 because 24 = 16.

As you can see, the binary system can appear difficult to read when a value combines various bit representations. To make it easier, the computer recognizes the hexadecimal representation of bits. Following the box combinations above, we can represent each 4-bit of the sixteen combinations using the decimal, hexadecimal, and binary systems as follows:

Decimal  Binary Hexadecimal 

0 0000 0

1 0001 1

2 0010 2

3 0011 3

4 0100 4

5 0101 5

6 0110 6

7 0111 7

8 1000 8

9 1001 9

10 1010 A

11 1011 B

12 1100 C

13 1101 D

14 1110 E

15 1111 F

 Table of Numeric Conversions

 

When looking at a binary value represented by 4 bits, you can get its decimal or hexadecimal values by referring to the table above. A group of four consecutive bits has a minimum and maximum values on each system as follows:

  Decimal  Hexadecimal  Binary

Minimum  0 0x0 0000

Maximum  15 0xf 1111

Although the C# compiler recognizes a group of four consecutive bits, you cannot store any variable in it. You can, however, manipulate the bits of the group.

23

Page 24: Introduction to C#

A Byte 

Introduction

A byte is a group or eight consecutive bits. The bits are counted from right to left starting at 0. The most right bit is bit 0; it is called the least significant bit. It is also referred to as the Low Order bit, the LO bit, or LOBIT. The most left bit is bit 7; it is called the most significant bit. It is also referred to as the High Order bit, the HI bit, or HIBIT. The other bits are referred to following their positions.

 

Using the binary system, you can represent the byte using a combination of 0s and 1s. When all bits have a value of 0, the byte is represented as 00000000. On the other hand, when all bits have a value of 1, the byte is represented as 11111111. When the number grows very large, it becomes difficult to read. Therefore, you can represent bits in groups of four. Instead of writing 00000000, you can write 0000 0000. This makes the number easier to read.

If you have the patience to create combinations of bits using the cups as we did for the group of 4, you would find out that there are 256 possible combinations. Another way to find it out is by using the base 2 technique:

27 + 26 + 25 + 24 + 23 + 22 + 21 + 20

= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1= 255

Therefore, the maximum decimal value you can store in a byte is 255.

Remember that the byte with all bits having a value of 0 has its value set to 0. Since this byte also holds a valid value, the number of combinations = 255 + 1 = 256.

24

Page 25: Introduction to C#

When a byte is completely represented with 0s, it provides the minimum value it can hold; this is 0000 0000, which is also 0. When all bits have a value of 1, which is 1111 1111, a byte holds its maximum value that we calculated as 255 in the decimal system. As done with the group of 4 bits, we get the following table:

  Decimal Hexadecimal Binary

Minimum 0 0x0 0000

Maximum 255 0xff 1111 1111

The minimum storage area offered by the (Intel) computer is the byte. As you know already, a byte is a group of 8 consecutive bits. The amount of memory space offered by a byte can be used to store just a single symbol, such as those you see on your keyboard. These symbols, also called characters, have been organized by the American Standard Code for Information Exchange (ASCII) in a set list. But, ASCII uses only 128 decimal numbers (based on a 7-bit format) to represent symbols counted from 0 to 127. To compensate for the remaining 1 bit, IBM used it to organize special characters, foreign language characters, mathematical symbols, small graphics, etc. Each one of these characters has a decimal, a hexadecimal, and a binary equivalents.

Each one of the characters you see on your keyboard is represented as a numeric value, but whether it appears as a number, a letter, or a symbol, each one of these is considered a character. To display any character on your screen, you can pass it to Write() or WriteLine() and include the character between single-quotes, as follows:

using System;

class Exercise{

static void Main(){

Console.WriteLine('n');}

}

Characters

In the English alphabet, a character is one of the following symbols: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z. Besides these readable characters, the following symbols are called digits and they are used to represent numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In addition, some symbols on the (US QWERTY) keyboard are also called characters or symbols. They are ` ~ ! @ # $ % ^ & * ( ) - _ = + [ { ] } \ | ; : ' < ? . / , > "

Besides the English language, other languages use other or additional characters that represent verbal or written expressions.

C# recognizes that everything that can be displayed as a symbol is called a character. To declare a variable whose value would be a character, use the char keyword. Here is an example:

using System;

class ObjectName{

static void Main()

25

Page 26: Introduction to C#

{char Gender = 'M';

Console.Write("Student Gender: ");Console.WriteLine(Gender);

}}

This would produce:

Student Gender: M

The Byte Data Type

A byte is an unsigned number whose value can range from 0 to 255 and therefore can be store in one byte. You can use it when you know a variable would hold a relatively small value such as people's age, the number of children of one mother, etc. To declare a variable that would hold a small natural number, use the byte keyword. Here is an example:

byte Age;

You can initialize a byte variable when declaring it or afterwards. Here is an example that uses the byte data type:

using System;

class ObjectName{

static void Main(){

Byte Age = 14;

Console.Write("Student Age: ");Console.WriteLine(Age);Age = 12;Console.Write("Student Age: ");Console.WriteLine(Age);

}}

Make sure you do not use a value that is higher than 255 for a byte variable, you would receive an error. When in doubt, or when you think that there is a possibility a variable would hold a bigger value, don't use the byte data type as it doesn't like exceeding the 255 value limit.

 

Practical Learning: Using Bytes

1. To use byte variables, change the file as follows: 

using System;

namespace GeorgetownCleaners{

class OrderProcessing{

26

Page 27: Introduction to C#

static void Main(){

byte NumberOfShirts;byte NumberOfPants;

NumberOfShirts = 4;NumberOfPants = 1;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

Console.WriteLine("========================");Console.WriteLine("Item Type Qty");Console.WriteLine("------------------------");Console.Write("Shirts ");Console.WriteLine(NumberOfShirts);Console.Write("Pants ");Console.WriteLine(NumberOfPants);Console.WriteLine("========================");Console.WriteLine();

}}

}

2. Execute the program. This would produce: 

-/- Georgetown Cleaning Services -/-========================Item Type Qty------------------------Shirts 4Pants 1========================

3. Return to Visual C#

Signed Byte

A byte number is referred to as signed if it can hold a negative or a positive value that ranges from -128 to 127, which can therefore fit in a byte. To declare a variable for that kind of value, use the sbyte keyword. Here is an example:

using System;

class NumericRepresentation{

static void Main(){

sbyte RoomTemperature = -88;

Console.Write("When we entered, the room temperature was ");Console.WriteLine(RoomTemperature);Console.WriteLine();

}}

27

Page 28: Introduction to C#

This would produce:

When we entered, the room temperature was -88

A Word 

Introduction

A word is a group of 16 consecutive bits. The bits are counted from right to left starting at 0:

Considered as a group of 16 bits, the most right bit of a word, bit 0, is called the least significant bit or Low Order bit or LO bit or LOBIT. The most left bit, bit 15, is called the most significant bit or High Order bit or HI bit or HIBIT. The other bits are referred to using their positions: bit 1, bit 2, bit 3, etc.

Considering that a word is made of two bytes, the group of the right 8 bits is called the least significant byte or Low Order byte or LO byte or LOBYTE. The other group is called the most significant byte or High Order byte or HI byte or HIBYTE.

The representation of a word in binary format is 0000000000000000. To make it easier to read, you can group bits by 4, like this: 0000 0000 0000 0000. Therefore, the minimum binary value represented by a word is 0000 0000 0000 0000. The minimum decimal value of a word is 0. The minimum hexadecimal value you can store in a word is 0x0000000000000000. This is also represented as 0x00000000, or 0x0000, or 0x0. All these numbers produce the same value, which is 0x0. 

The maximum binary value represented by a word is 1111 1111 1111 1111. To find out the maximum decimal value of a word, you can use the base 2 formula, filling out each bit with 1:

1*215+1*214+1*213 + 1*212 + 1*211 + 1*210 + 1*29 + 1*28 + 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20

= 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 +

28

Page 29: Introduction to C#

2 + 1

= 65535

To find out the maximum hexadecimal number you can store in a word, replace every group of 4 bits with an f or F:

1111 1111 1111 1111f f f f

= 0xffff= 0xFFFF= 0Xffff= 0XFFFF

 

Short Integers

A word, which is a group of 16 contiguous bits or 2 bytes, can be used to hold a natural number. As we have studied, the maximum numeric value that can fit in a word is 65535. Since the byte is used for very small numbers, whenever you plan to use a number in your program, the minimum representation you should use is a word.

The smallest integer you can store in a word is declared with the short keyword followed by a name. Because a short integer is signed by default, it can store a value that ranges from –32768 to 32767. Here is an example program that uses two short integers:

using System;

class NumericRepresentation{

static void Main(){

short NumberOfPages;short Temperature;

NumberOfPages = 842;Temperature = -1544;

Console.Write("Number of Pages of the book: ");Console.WriteLine(NumberOfPages);Console.Write("Temperature to reach during the experiment: ");Console.Write(Temperature);Console.WriteLine(" degrees\n");

}}

This would produce:

Number of Pages of the book: 842Temperature to reach during the experiment: -1544 degrees

Because a short integer handles numbers that are larger than the signed byte, any variable you can declare for a signed byte can also be declared for a short variable.

29

Page 30: Introduction to C#

Unsigned Short Integers

If a variable must hold positive and relatively small numbers, it is referred as an unsigned short integer. Such a variable can be declared using the ushort keyword. An unsigned short integer can hold numbers that range from 0 to 65535 and therefore can fit in 16 bits. Here is an example:

using System;

class NumericRepresentation{

static void Main(){

// These variables must hold only positive integersushort NumberOfTracks;ushort MusicCategory;

NumberOfTracks = 16;MusicCategory = 2;

Console.Write("This music album contains ");Console.Write(NumberOfTracks);Console.WriteLine(" tracks");Console.Write("Music Category: ");Console.Write(MusicCategory);Console.WriteLine();

}}

This would produce:

This music album contains 16 tracksMusic Category: 2

Practical Learning: Using Unsigned Short Integers

1. To use unsigned short integers, change the file as follows: 

using System;

class GeorgetownCleaningServices{

static void Main(){

ushort NumberOfShirts;ushort NumberOfPants;ushort NumberOfDresses;

NumberOfShirts = 4;NumberOfPants = 0;NumberOfDresses = 3;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

30

Page 31: Introduction to C#

Console.WriteLine("========================");Console.WriteLine("Item Type Qty");Console.WriteLine("------------------------");Console.Write("Shirts ");Console.WriteLine(NumberOfShirts);Console.Write("Pants ");Console.WriteLine(NumberOfPants);Console.Write("Dresses ");Console.WriteLine(NumberOfDresses);Console.WriteLine("========================");Console.WriteLine();

}}

2. Execute the program. This would produce: 

-/- Georgetown Cleaning Services -/-========================Item Type Qty------------------------Shirts 4Pants 0Dresses 3========================

3. Return to Visual C#

A Double-Word 

Introduction

A double-word is a group of two consecutive Words. This means that a double-word combines 4 bytes or 32 bits. The bits, counted from right to left, start at 0 and end at 31.

    

The most right bit, bit 0, is called the Low Order bit or LO bit or LOBIT. The most left bit, bit 31, is called the High Order bit or HI bit or HIBIT. The other bits are called using their positions.

The group of the first 8 bits (from bit 0 to bit 7), which is the right byte, is called the Low Order Byte, or LO Byte. It is sometimes referred to as LOBYTE. The group of the last 8 bits (from bit 24 to bit 31), which is the left byte, is called the High Order Byte, or HI Byte or HIBYTE. The other bytes are called by their positions.

The group of the right 16 bits, or the right Word, is called the Low Order Word, or LO Word, or LOWORD. The group of the left 16 bits, or the left Word, is called the High Order Word, or HI Word, or HIWORD.

31

Page 32: Introduction to C#

    

The minimum binary number you can represent with a double-word is 0. The minimum decimal value of a double-word is 0. To find out the maximum decimal value of a word, you can use the base 2 formula giving a 1 value to each bit:

  230  229  228 227  226 225 224

1,073,741,824 536,870,912 268,435,456 134,217,728 67,108,864 33,554,432 16,777,216

  223 222 221 220 219 218 217 216

8,388,608 4,194,304 2,097,152 1,048,576 524,288 262,144 131,072 65,536

  215 214 213 212 211 210 29 28

32,768 16,384 8,192 4,096 2,048 1,024 512 256

  27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

1*231+1*230+1*229 + 1*228 + 1*227 + 1*226 + 1*225 + 1*224 + 1*223 + 1*222 + 1*221 + 1*220 + 1*219 + 1*218 + 1*217 + 1*216 + 1*215 + 1*214 + 1*213 + 1*212 + 1*211 + 1*210 + 1*29 + 1*28 + 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20

= 2,147,483,648 + 1,073,741,824 + 536,870,912 + 268,435,456 + 134,217,728 + 67,108,864 + 33,554,432 + 16,777,216 + 8,388,608 + 4,194,304 + 2,097,152 + 1,048,576 + 524,288 + 262,144 + 131,072 + 65,536 + 32,768 + 16,384 + 8,192 + 4,096 + 2,048 + 1,024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= 4,286,578,708

The minimum hexadecimal value you can store in a double-word is 0x00000000000000000000000000000000 which is the same as 0x0. To find out the maximum hexadecimal number you can represent with a word, replace every group of 4-bits with an f or F:

32

Page 33: Introduction to C#

1111 1111 1111 1111 1111 1111 1111 1111

f f f f f f f f

= 0xffffffff = 0Xffffffff = 0XFFFFFFFF = 0xFFFFFFFF

 

Signed Integers

A double-word is large enough to contain double the amount of data that can be stored in a word. This is equivalent to 32 bits or 4 bytes or 4,294,967,295. Therefore, a double-word is used for large numbers that would not fit in a word.

To use a variable that would hold quite large numbers, declare it using the int keyword. A variable declared as int can store values between –2,147,483,648 and 2,147,484,647 negative or positive, that can fit in 32 bits.

Here is an example:

using System;

class NumericRepresentation{

static void Main(){

int CoordX;int CoordY;

CoordX = 12;CoordY = -8;

Console.Write("Cartesian Coordinate System: ");Console.Write("P(");Console.Write(CoordX);Console.Write(", ");Console.Write(CoordY);Console.WriteLine(")\n");

}}

When executed, the program would produce:

Cartesian Coordinate System: P(12, -8)

Unsigned Integers

If the variable must hold only positive natural numbers, you can declare it using the uint keyword. The uint keyword is used to identify a 32-bit positive integer whose value would range from 0 to 4,294,967,295. Here is an example:

using System;

class NumericRepresentation{

static void Main()

33

Page 34: Introduction to C#

{uint DayOfBirth;uint MonthOfBirth;uint YearOfBirth;

DayOfBirth = 8;MonthOfBirth = 11;YearOfBirth = 1996;

Console.WriteLine("Red Oak High School");Console.Write("Student Date of Birth: ");Console.Write(MonthOfBirth);Console.Write("/");Console.Write(DayOfBirth);Console.Write("/");Console.Write(YearOfBirth);Console.WriteLine();

}}

This would produce:

Red Oak High SchoolStudent Date of Birth: 11/8/1996

Practical Learning: Using Unsigned Integers

1. To unsigned unsigned variables, change the file as follows: 

using System;

namespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){

ushort NumberOfShirts;ushort NumberOfPants;ushort NumberOfDresses;

uint OrderDay;uint OrderMonth;uint OrderYear;

NumberOfShirts = 4;NumberOfPants = 0;NumberOfDresses = 3;OrderDay = 15;OrderMonth = 7;OrderYear = 2002;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

Console.WriteLine("========================");

34

Page 35: Introduction to C#

Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------");Console.WriteLine("Item Type Qty");Console.WriteLine("------------------------");Console.Write("Shirts ");Console.WriteLine(NumberOfShirts);Console.Write("Pants ");Console.WriteLine(NumberOfPants);Console.Write("Dresses ");Console.WriteLine(NumberOfDresses);Console.WriteLine("========================");Console.WriteLine();

}}

}

2. Execute the program. This would produce: 

-/- Georgetown Cleaning Services -/-========================Order Date: 7/15/2002------------------------Item Type Qty------------------------Shirts 4Pants 0Dresses 3========================

3. Return to Visual C#

A Quad-Word 

Introduction

Sometimes you may want to store values that a double-word cannot handle. To store a very large number in a variable, you can consider a combination of 64 bits. The group can also be referred to as a quad-word. A quad-word is so large it can store numbers in the range of –9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

Long Integers

To use a variable that can hold very large numbers that would require up to 64 bits, declare it using the long keyword. This is for a number that can range between –9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

35

Page 36: Introduction to C#

In C++, the long data type is 32 bits while in C#, the long data type is 64 bits.

Here is an example that uses the long data type:

using System;

class NumericRepresentation{

static void Main(){

long CountryArea;

CountryArea = 5638648;

Console.Write("Country Area: ");Console.Write(CountryArea);Console.Write("km2\n");

}}

This would produce:

Country Area: 5638648km2

Although the long data type is used for large number, it mainly indicates the amount of space available but you don't have to use the whole space. For example, you can use the long keyword to declare a variable that would hold the same range of numbers as the short, the int, or the uint data types.

If you declare a variable as long but use it for small numbers that don't require 64 bits, the compiler would allocate the appropriate amount of space to accommodate the values of the variable. Consequently, the amount of space made available may not be as large as 64 bits. If you insist and want the compiler to reserve 64 bits, when assigning a value to the variable, add an l or an L suffix to it. Here is an example that uses space of a long data type to store a number that would fit in 32 bits:

using System;

class NumericRepresentation{

static void Main(){

long CountryArea;

CountryArea = 5638648L;

Console.Write("Country Area: ");Console.Write(CountryArea);Console.Write("km2\n");

}}

Therefore, keep in mind that an int, a uint, a short, or a ushort can fit in a long variable.

Unsigned Long Integers

You can use a combination of 64 bits to store positive or negative integers. In some cases, you will need a variable to hold only positive, though large, numbers. To declare such a variable, you can use the

36

Page 37: Introduction to C#

ulong data type. A variable declared as ulong can handle extremely positive numbers that range from 0 to 18,446,744,073,709,551,615 to fit in 64 bits.

 

Real Numbers 

Introduction

A real number is a number that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel:

On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.

Single-Precision Numbers

37

Page 38: Introduction to C#

The integers we have used so far have the main limitation of not allowing decimal values. C# provides floating-point values that would solve this problem. The most fundamental floating variable is declared with the float keyword. A variable declared with float can store real numbers that range from ±1.5 × 10−45 to ±3.4 × 1038 with a precision of 7 digits in 32 bits. Here is an example:

using System;

class NumericRepresentation{

static void Main(){

float Distance;}

}

Double-Precision Numbers

When a variable is larger than the float can handle and requires more precision, you should declare it using the double keyword. A variable declared as double uses 64 bits to store very large numbers ranging from ±5.0 × 10−324 to ±1.7 × 10308 with a precision of 15 or 16 digits.

Here is an example:

using System;

class NumericRepresentation{

static void Main(){

double StudentAge;

StudentAge = 14.50;

Console.Write("Student Age: ");Console.Write(StudentAge);Console.WriteLine();

}}

This would produce:

Student Age: 14.5

 

Practical Learning: Using a Double-Precision Variable

1. To use a double-precision value, change the file as follows: 

using System;

namespace GeorgetownCleaners{

class OrderProcessing

38

Page 39: Introduction to C#

{static void Main(){

ushort NumberOfShirts;ushort NumberOfPants;ushort NumberOfDresses;

uint OrderDay;uint OrderMonth;uint OrderYear;

double MondayDiscount;

NumberOfShirts = 4;NumberOfPants = 0;NumberOfDresses = 3;OrderDay = 15;OrderMonth = 7;OrderYear = 2002;MondayDiscount = 0.25; // 25%

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

Console.WriteLine("========================");Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------");Console.WriteLine("Item Type Qty");Console.WriteLine("------------------------");Console.Write("Shirts ");Console.WriteLine(NumberOfShirts);Console.Write("Pants ");Console.WriteLine(NumberOfPants);Console.Write("Dresses ");Console.WriteLine(NumberOfDresses);Console.WriteLine("------------------------");Console.Write("Monday Discount: ");Console.Write(MondayDiscount);Console.WriteLine('%');Console.WriteLine("========================");Console.WriteLine();

}}

}

2. Save the file

 

Decimal

The decimal data type can be used to declare a variable that would hold significantly large values that

39

Page 40: Introduction to C#

can be stored in a combination of 128 bits. You declare such a variable using the decimal keyword. The values stored in a decimal variable can range from ±1.0 × 10−28 to ±7.9 × 1028 with a precision of 28 to 29 digits. Because of this high level of precision, the decimal data type is suitable for currency values.

After declaring a decimal variable, you can initialize it with a natural number. Here is an example:

using System;

class NumericRepresentation{

static void Main(){

decimal HourlySalary;

HourlySalary = 24;

Console.Write("Hourly Salary = ");Console.WriteLine(HourlySalary);Console.WriteLine();

}}

This would produce:

Hourly Salary = 24

 

Initializing a Real Number

Because the double data type provides a better result with a better precision than the float, whenever you declare a variable as float and assign it a value as we did earlier, the compiler allocates 64 bits to store the values of the variable. If you insist on the variable being treated as float, when assigning it a value, add an f or an F suffix to the value. Here is an example:

using System;

class NumericRepresentation{

static void Main(){

float Distance;

Distance = 248.38F;

Console.Write("Distance = ");Console.Write(Distance);Console.WriteLine("km\n");

}}

This would produce:

Distance = 248.38km

Otherwise, if you declare a double variable, when initializing it, to make sure the value is treated as double, type the d or the D suffix to the right of the value.

40

Page 41: Introduction to C#

If you declare a variable using the decimal keyword, to indicate that the variable holds a decimal value, when initializing it, add an m or an M suffix to its value.

Practical Learning: Using Decimal Values

1. To use decimal variables, change the file as follows: 

using System;

namespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){

ushort NumberOfShirts;ushort NumberOfPants;ushort NumberOfDresses;

decimal PriceOneShirt;decimal PriceAPairOfPants;decimal PriceOneDress;

uint OrderDay;uint OrderMonth;uint OrderYear;

double MondayDiscount;

NumberOfShirts = 1;NumberOfPants = 1;NumberOfDresses = 1;OrderDay = 15;OrderMonth = 7;OrderYear = 2002;PriceOneShirt = 0.95M;PriceAPairOfPants = 2.95M;PriceOneDress = 4.55M;MondayDiscount = 0.25; // 25%

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

Console.WriteLine("========================");Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------");Console.WriteLine("Item Type Qty Sub-Total");Console.WriteLine("------------------------");Console.Write("Shirts ");

41

Page 42: Introduction to C#

Console.Write(NumberOfShirts);Console.Write(" ");Console.WriteLine(PriceOneShirt);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.WriteLine(PriceAPairOfPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.WriteLine(PriceOneDress);Console.WriteLine("------------------------");Console.Write("Monday Discount: ");Console.Write(MondayDiscount);Console.WriteLine('%');Console.WriteLine("========================");Console.WriteLine();

}}

}

2. Execute the program. This would produce: 

-/- Georgetown Cleaning Services -/-========================Order Date: 7/15/2002------------------------Item Type Qty Sub-Total------------------------Shirts 1 0.95Pants 1 2.95Dresses 1 4.55------------------------Monday Discount: 0.25%========================

3. Return to Visual C#

.NET Support of Data Types 

Introduction

All of the data types we have used so far are in fact complete classes. This means that they are equipped to handle their own assignments called functions as we will learn in subsequent lessons. When a data type (in this case a class) has to ability to perform assignments through its functions, it is able to better communicate with other variables, functions, or classes (the fact that the other languages, namely C/C++, Pascal, etc, use data types that are not considered classes doesn't mean they are less efficient or that they have an anomaly: it is simply the construct of the language!). These classes are defined in the System namespace. The classes of these data types are defined as:

  Data Type Class   Data Type Class

  bool Boolean   char Char

42

Page 43: Introduction to C#

  byte Byte   sbyte SByte

  short Int16   ushort UInt16

  int Int32   uint UInt32

  long Int64   ulong UInt64

  float Single   double Double

  decimal Decimal      

This means that, if you don't want to use the data types we have reviewed so far, you can use the class that is defined in the System namespace. To use one of these types, type the System namespace followed by a period. Then type the equivalent class you want to use. Here is an example:

class Operations{

public double Addition(){

System.Double a;System.Double b;System.Double c;

a = 128.76;b = 5044.52;c = a + b;

return c;}

}

Because the regular names of data types we introduced in the previous lesson are more known and familiar, we will mostly use them.

Because the data type are defined as classes, they are equipped with methods. One of the methods that each one them has is called ToString. As it s name implies, it is used to convert a value to a string.

 

Strings

A string is an empty space, a character, a word, or a group of words that you want the compiler to consider "as is", that is, not to pay too much attention to what the string is made of, unless you explicitly ask it to. This means that, in the strict sense, you can put in a string anything you want.

Primarily, the value of a string starts with a double quote and ends with a double-quote. An example of a string is "Welcome to the World of C# Programming!". You can include such a string in the Console.Write() method to display it on the console. Here is an example:

using System;

class BookClub{

static void Main(){

Console.WriteLine("Welcome to the World of C# Programming!");}

}

43

Page 44: Introduction to C#

This would produce:

Welcome to the World of C# Programming!

Sometimes, you will need to use a string whose value is not known in advance. Therefore, you can first declare a string variable. To do this, use the string keyword (in fact, it is a class) followed by a name for the variable. The name will follow the rules we defined above. An example of a string declaration is:

string Msg;

After declaring a string, you can give it a primary value by assigning it an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes. Here are examples of string variables declared and initialized:

string Empty = "";string Gender = "F";string FName = "Nelson Mandela";string Msg = "Welcome to the World of C# Programming! ";

After initializing a string variable, you can use its name in any valid operation or expression. For example, you can display its value on the console using the Console.Write() or the Console.WriteLine() methods. Here is an example:

using System;

class BookClub{

static void Main(){

string Msg = "Welcome to the World of C# Programming! ";Console.WriteLine(Msg);

}}

 

Practical Learning: Using Strings

1. To use strings, change the file as follows: 

using System;

namespace GeorgetownCleaners{

class OrderProcessing{

static void Main(){

string CustomerName;string CustomerHomePhone;

ushort NumberOfShirts;ushort NumberOfPants;ushort NumberOfDresses;

decimal PriceOneShirt;

44

Page 45: Introduction to C#

decimal PriceAPairOfPants;decimal PriceOneDress;

uint OrderDay;uint OrderMonth;uint OrderYear;

double MondayDiscount;

CustomerName = "Gregory Almas";CustomerHomePhone = "(301) 723-4425";

NumberOfShirts = 1;NumberOfPants = 1;NumberOfDresses = 1;OrderDay = 15;OrderMonth = 7;OrderYear = 2002;PriceOneShirt = 0.95M;PriceAPairOfPants = 2.95M;PriceOneDress = 4.55M;MondayDiscount = 0.25; // 25%

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

Console.WriteLine("========================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");Console.WriteLine(CustomerHomePhone);Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------");Console.WriteLine("Item Type Qty Sub-Total");Console.WriteLine("------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.WriteLine(PriceOneShirt);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.WriteLine(PriceAPairOfPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.WriteLine(PriceOneDress);Console.WriteLine("------------------------");Console.Write("Monday Discount: ");Console.Write(MondayDiscount);Console.WriteLine('%');Console.WriteLine("========================");

45

Page 46: Introduction to C#

Console.WriteLine();}

}}

2. Execute the program. This would produce: 

 

3. Return to Visual C#

 

Dates and Times

A date is a unit that measures the number of years, months, or days elapsed in a specific period. A time is a unit that counts the number of seconds that have elapsed since midnight of the day considered. Although dates and times are large subjects that would require a detailed study, at this time, we will consider them in simple terms.

To declare a variable that would hold date or time values, use the DateTime data type (like every data type we have used so far, except the string that is a class, DateTime is a structure).

using System;

class NumericRepresentation{

static void Main(){

DateTime DateHired;}

}

The .NET Framework sets its starting periodic date to January 1, 0001 at midnight (12:00:00 or 0:00 AM). If not assigned a specific value (in future lessons, we will learn that this is equivalent to declaring a

46

Page 47: Introduction to C#

variable using the default constructor), the variable is initialized to 1/1/0001 at midnight.

Objects

The Object data type (it is a class) is used to declare a variable whose type is not primarily defined and can be any of the other data types we have introduced. Here is an example:

using System;

class Exercise{

static void Main(){

object Whatever;}

}

 

47

Page 48: Introduction to C#

 

Operators and Operands 

Fundamental C# Operators 

Introduction

An operation is an action performed on one or more values either to modify the value held by one or both of the variables, or to produce a new value by combining existing values. Therefore, an operation is performed using at least one symbol and at least one value. The symbol used in an operation is called an operator. A value involved in an operation is called an operand.

A unary operator is an operator that performs its operation on only one operand. An operator is referred to as binary if it operates on two operands.

Semi-Colon;

The semi-colon is used to indicate the end of an expression or a declaration. Here is an example:

using System;

As we will learn when studying the for conditional statement, there are other uses of the semi-colon.

 

Practical Learning: Introducing the Semi-Colon1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with GCS2 and specify the desired path in the Location

6. Click OK

 

48

Page 49: Introduction to C#

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise

10. Click Open

11. In the empty file, type the following: 

using System;

12. To execute the application, on the main menu, click Debug -> Start Without Debugging

13. Press Enter to close the DOS window and return to Visual C#

Curly Brackets { }

Curly brackets are probably the most used and the most tolerant operators of C#. Fundamentally, curly brackets are used to create a section of code. As such they are required to delimit the bodies of namespaces, classes, structures, exceptions, etc. They are also optionally used in conditional statements. Curly brackets are also used to create variable scope.

Here is an example:

using System;

class Exercise{}

Practical Learning: Using Curly Brackets

1. To use curly brackets, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{}

2. Save the file

 

Parentheses ( )

Like most computer languages, C# uses parentheses to isolate a group of items that must be considered as belonging to one entity. For example, parentheses are used to differentiate a method such as Main from a regular variable. Here is an example:

using System;

class Exercise{

49

Page 50: Introduction to C#

static void Main(){}

}

Parentheses can also be used to isolate an operation or an expression with regard to another operation or expression.

Practical Learning: Using Parentheses

1. To use parentheses, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

double MondayDiscount;}

}}

2. Save the file

The Comma ,

The comma is used to separate variables used in a group. For example, a comma can be used to delimit the names of variables that are declared with the same data type. Here is an example:

using System;

class Exercise{

static void Main(){

string FirstName, LastName, FullName;}

}

The comma can also be used to separate the member of an enumerator or the arguments of a method. We will review all of them when the time comes.

Practical Learning: Using Commas

1. To use commas, change the file as follows: 

using System;

namespace GeorgetownCleaningServices

50

Page 51: Introduction to C#

{class OrderProcessing{

static void Main(){

string CustomerName, HomePhone;uint NumberOfShirts, NumberOfPants, NumberOfDresses;decimal PriceOneShirt, PriceAPairOfPants,

PriceOneDress;uint OrderMonth, OrderDay, OrderYear;double MondayDiscount;

}}

}

2. Save the file

The Assignment =

When you declare a variable, a memory space is reserved for it. That memory space may be empty until you fill it with a value. To "put" a value in the memory space allocated to a variable, you can use the assignment operator represented as =. Based on this, the assignment operation gives a value to a variable. Its syntax is:

VariableName = Value

The VariableName factor must be a valid variable name. It cannot be a value such as a numeric value or a (double-quoted) string. Here is an example that assigns a numeric value to a variable:

namespace Operations{

class Exercise{

static void Main(){

decimal Salary;

// Using the assignment operatorSalary = 12.55M;

}}

}

Once a variable has been declared and assigned a value, you can call Write() or WriteLine() to display its value.  Here is an example:

namespace Operations{

class Exercise{

static void Main(){

decimal Salary;

// Using the assignment operator

51

Page 52: Introduction to C#

Salary = 12.55M;System.Console.Write("Employee's Hourly Salary: ");System.Console.WriteLine(Salary);

}}

}

This would produce:

Employee's Hourly Salary: $12.55

The above code declares a variable before assigning it a value. You will usually perform this assignment when you want to change the value held by a variable. Providing a starting value to a variable when the variable is declared is referred to as initializing the variable. Here is an example:

namespace Operations{

class Exercise{

static void Main(){

// Using the assignment operatordecimal Salary = 12.55M;

System.Console.Write("Employee's Hourly Salary: ");System.Console.WriteLine(Salary);

}}

}

We saw that you can declare various variables at once by using the same data type but separating their names with commas. When doing this, you can also initialize each variable by assigning it the desired value before the comma or the semi-colon. Here is an example:

namespace Operations{

class Exercise{

static void Main(){

// Initializing various variables when declaring them with the same data type

double Value1 = 224.58, Value2 = 1548.26;

System.Console.Write("Value 1 = ");System.Console.WriteLine(Value1);System.Console.Write("Value 2 = ");System.Console.WriteLine(Value2);System.Console.WriteLine();

}}

}

This would produce:

Value 1 = 224.58Value 2 = 1548.26

52

Page 53: Introduction to C#

 

Practical Learning: Assigning Values to Variables

1. To use the assignment operator, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

string CustomerName, HomePhone;uint NumberOfShirts = 5,

NumberOfPants = 2, NumberOfDresses = 0;

decimal PriceOneShirt = 0.95M, PriceAPairOfPants = 2.95M, PriceOneDress = 4.55M;

uint OrderMonth = 3, OrderDay = 15, OrderYear = 2002;

double MondayDiscount = 0.25; // 25%;

Console.WriteLine();}

}}

2. Save the file

Single-Quote '

The single quote is used to include one character to initialize, or assign a symbol to, a variable declared as char. A single is usually combined with another and a character is included between them. Here is an example:

using System;

class Exercise{

static void Main(){

char Gender;string FirstName, LastName, FullName;

Gender = 'M';}

}

You can include only one character between single-quotes except if the combination of symbols can be evaluated to one character. This is the case for escape sequences. Here is an example:

53

Page 54: Introduction to C#

using System;

class Exercise{

static void Main(){

Console.WriteLine('\n');}

}

Double-Quotes "

The double-quote " is used to delimit a string. Like the single-quote, the double-quote is usually combined with another. Between the combination of double-quotes, you can include an empty space, a character, a word, or a group of words, making it a string. Here is an example:

using System;

class Exercise{

static void Main(){

Console.WriteLine("The Wonderful World of C#!!!");}

}

A double-quoted string can also be declared and then assigned to a variable.

Practical Learning: Using Quotes

1. To use single and double-quotes, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

string CustomerName = "James Burreck", HomePhone = "(202) 301-7030";

uint NumberOfShirts = 1, NumberOfPants = 1, NumberOfDresses = 1;

decimal PriceOneShirt = 0.95M, PriceAPairOfPants = 2.95M, PriceOneDress = 4.55M;

uint OrderMonth = 3, OrderDay = 15, OrderYear = 2002;double MondayDiscount = 0.25; // 25%;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");

Console.WriteLine("========================");

54

Page 55: Introduction to C#

Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");Console.WriteLine(HomePhone);Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------");Console.WriteLine("Item Type Qty Sub-Total");Console.WriteLine("------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.WriteLine(PriceOneShirt);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.WriteLine(PriceAPairOfPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.WriteLine(PriceOneDress);Console.WriteLine("------------------------");Console.Write("Monday Discount: ");Console.Write(MondayDiscount);Console.WriteLine('%');Console.WriteLine("========================");Console.WriteLine();

}}

}

2. To execute the program, on the main menu, click Debug -> Start Without Debugging. This would produce:

55

Page 56: Introduction to C#

 

3. Return to Visual C#

Square Brackets [ ]

Square brackets are mostly used to control the dimension or index of an array. We will learn how to use them when we study arrays. Here is an example from a future lesson:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double[] number = new double[5];

number[0] = 12.44; number[1] = 525.38;

number[2] = 6.28; number[3] = 2448.32; number[4] = 632.04;

Console.WriteLine();}

}}

The Positive Operator +

56

Page 57: Introduction to C#

Algebra uses a type of ruler to classify numbers. This ruler has a middle position of zero. The numbers on the left side of the 0 are referred to as negative while the numbers on the right side of the rulers are considered positive:

-∞   -6 -5 -4 -3 -2 -1   1 2 3 4 5 6   +∞

   0

-∞   -6 -5 -4 -3 -2 -1   1 2 3 4 5 6   +∞

A value on the right side of 0 is considered positive. To express that a number is positive, you can write a + sign on its left. Examples are +4, +228, +90335. In this case the + symbol is called a unary operator because it acts on only one operand. The positive unary operator, when used, must be positioned on the left side of its operand, never on the right side.

As a mathematical convention, when a value is positive, you don't need to express it with the + operator. Just writing the number without any symbol signifies that the number is positive. Therefore, the numbers +4, +228, and +90335 can be, and are better, expressed as 4, 228, 90335. Because the value doesn't display a sign, it is referred as unsigned.

To express a variable as positive or unsigned, you can just type it. here is an example:

using System;

class Exercise{

static void Main(){

// Displaying an unsigned numberConsole.Write("Number = ");Console.WriteLine(+802);

}}

This would produce:

Number = 802

The Negative Operator -

As you can see on the above ruler, in order to express any number on the left side of 0, it must be appended with a sign, namely the - symbol. Examples are -12, -448, -32706. A value accompanied by - is referred to as negative. The - sign must be typed on the left side of the number it is used to negate. Remember that if a number does not have a sign, it is considered positive. Therefore, whenever a number is negative, it MUST have a - sign. In the same way, if you want to change a value from positive to negative, you can just add a - sign to its left.

Here is an example that uses two variables. One has a positive value while the other has a negative value:

using System;

class Exercise{

static void Main(){

// Displaying an unsigned number

57

Page 58: Introduction to C#

Console.Write("First Number ");Console.WriteLine(+802);// Displaying a negative numberConsole.Write("Second Number ");Console.WriteLine(-802);

}}

This would produce:

First Number 802Second Number -802

 

Unary Operators: The sizeof Operator

We have already seen that when declaring a variable, the compiler reserves a portion of space in the computer memory to hold that variable. We also illustrated how much space some data types need to store their variable. C++ provides the unary sizeof operator that allows you to find out how much space a data type. You can use the sizeof operator as unsafe in your C# application.

To find out how much memory space a data type uses, type it between the parentheses of the sizeof operator. Remember that sizeof is unsafe. Therefore, a method that uses it must be preceded with the unsafe keyword. Here is an example:

using System;

class Exercise{

unsafe static void Main(){

// The sizeof operator used to get the memory size used by // a variable declared with a certain a data typeConsole.WriteLine("The sizeof Operator");Console.WriteLine("==========================");Console.WriteLine("Data Type Memory Size");Console.WriteLine("--------------------------");Console.Write("char ");Console.Write(sizeof(char));Console.WriteLine(" Bytes");Console.Write("bool ");Console.Write(sizeof(bool));Console.WriteLine(" Bytes");Console.Write("int ");Console.Write(sizeof(int));Console.WriteLine(" Bytes");Console.Write("uint ");Console.Write(sizeof(uint));Console.WriteLine(" Bytes");Console.Write("short ");Console.Write(sizeof(short));Console.WriteLine(" Bytes");Console.Write("ushort ");Console.Write(sizeof(ushort));Console.WriteLine(" Bytes");

58

Page 59: Introduction to C#

Console.Write("byte ");Console.Write(sizeof(byte));Console.WriteLine(" Bytes");Console.Write("sbyte ");Console.Write(sizeof(sbyte));Console.WriteLine(" Bytes");Console.Write("float ");Console.Write(sizeof(float));Console.WriteLine(" Bytes");Console.Write("double ");Console.Write(sizeof(double));Console.WriteLine(" Bytes");Console.Write("decimal ");Console.Write(sizeof(decimal));Console.WriteLine(" Bytes");Console.Write("long ");Console.Write(sizeof(long));Console.WriteLine(" Bytes");Console.Write("ulong ");Console.Write(sizeof(ulong));Console.WriteLine(" Bytes");Console.WriteLine("===========================");Console.WriteLine();

}}

This would produce: 

The sizeof Operator==========================Data Type Memory Size--------------------------char 2 Bytesbool 1 Bytesint 4 Bytesuint 4 Bytesshort 2 Bytesushort 2 Bytesbyte 1 Bytessbyte 1 Bytesfloat 4 Bytesdouble 8 Bytesdecimal 16 Byteslong 8 Bytesulong 8 Bytes===========================

 

Algebraic OperatorsIn algebra, operations are performed on numeric values. Algebraic operators are represented with the following symbols:

59

Page 60: Introduction to C#

 

The Addition

The addition is an operation used to add things of the same nature one to another, as many as necessary. Sometimes, the items are added one group to another. The concept is still the same, except that this last example is faster. The addition is performed in mathematics using the + sign. The same sign is used in C#.

To get the addition of two values, you add the first one to the other. After the addition of two values has been performed, you get a new value. This means that if you add Value1 to Value2, you would write Value1 + Value2. The result is another value we could call Value3. You can also add more than two values, like a + b + c.

With numbers, the order you use to add two or more values doesn't matter. This means that Value1 + Value2 is the same as Value2 + Value1. In the same way a + b + c is the same as a + c + b the same as b + a + c and the same as c + b + a

Here is an example that adds two numbers:

namespace Operations{

class Exercise{

static void Main(){

System.Console.Write("244 + 835 = ");System.Console.WriteLine(244 + 835);

}}

}

Here is the result:

244 + 835 = 1079

You can also add some values already declared and initialized in your program. You can also get the values from the user.

In C#, you can also add string variables to add a new string. The operation is performed as if you were using numbers. For example, "Pie" + "Chart" would produce "PieChart". You can also add add as many strings as possible by including the + operator between them. Here is an example:

namespace Operations{

class Exercise{

static void Main(){

string FirstName = "Alexander";string LastName = "Kallack";string FullName = FirstName + " " + LastName;System.Console.Write("Full Name: ");System.Console.WriteLine(FullName);

60

Page 61: Introduction to C#

}}

}

This would produce:

Full Name: Alexander Kallack

Practical Learning: Using the Addition Operator

1. To use the addition, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

string CustomerName = "James Burreck", HomePhone = "(202) 301-7030";

uint NumberOfShirts = 1, NumberOfPants = 1, NumberOfDresses = 1;

uint TotalNumberOfItems;decimal PriceOneShirt = 0.95M,

PriceAPairOfPants = 2.95M, PriceOneDress = 4.55M;

uint OrderMonth = 3, OrderDay = 15, OrderYear = 2002;

TotalNumberOfItems = NumberOfShirts + NumberOfPants + NumberOfDresses;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("========================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");Console.WriteLine(HomePhone);Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------");Console.WriteLine("Item Type Qty Sub-Total");Console.WriteLine("------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.WriteLine(PriceOneShirt);Console.Write("Pants ");Console.Write(NumberOfPants);

61

Page 62: Introduction to C#

Console.Write(" ");Console.WriteLine(PriceAPairOfPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.WriteLine(PriceOneDress);Console.WriteLine("------------------------");Console.Write("Number of Items: ");Console.WriteLine(TotalNumberOfItems);Console.WriteLine("========================");Console.WriteLine();

}}

}

2. Execute the program. This would produce: 

-/- Georgetown Cleaning Services -/-========================Customer: James BurreckHome Phone: (202) 301-7030Order Date: 3/15/2002------------------------Item Type Qty Sub-Total------------------------Shirts 1 0.95Pants 1 2.95Dresses 1 4.55------------------------Number of Items: 3========================

3. Return to Visual C#

The Multiplication

The multiplication allows adding one value to itself a certain number of times, set by a second value. As an example, instead of adding a value to itself in this manner: A + A + A + A, since the variable a is repeated over and over again, you could simply find out how many times A is added to itself, then multiply a by that number which, is this case, is 4. This would mean adding a to itself 4 times, and you would get the same result.

Just like the addition, the multiplication is associative: a * b * c = c * b * a. When it comes to programming syntax, the rules we learned with the addition operation also apply to the multiplication.

Here is an example:

namespace Operations{

class Exercise{

static void Main(){

// Initializing various variables when declaring them with the same data type

62

Page 63: Introduction to C#

double Value1 = 224.58, Value2 = 1548.26;double Result = Value1 * Value2;

System.Console.Write(Value1);System.Console.Write(" * ");System.Console.Write(Value2);System.Console.Write(" = ");System.Console.WriteLine(Result);System.Console.WriteLine();

}}

}

This would produce:

224.58 * 1548.26 = 347708.2308

Practical Learning: Using the Multiplication Operator

1. To multiply, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

const decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;string CustomerName = "James Burreck",

HomePhone = "(202) 301-7030";uint NumberOfShirts = 5,

NumberOfPants = 2, NumberOfDresses = 3;

uint TotalNumberOfItems;decimal SubTotalShirts, SubTotalPants, SubTotalDresses;decimal TotalOrder;uint OrderMonth = 3, OrderDay = 15, OrderYear = 2002;

TotalNumberOfItems = NumberOfShirts + NumberOfPants + NumberOfDresses;SubTotalShirts = PriceOneShirt * NumberOfShirts;SubTotalPants = PriceAPairOfPants * NumberOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;TotalOrder = SubTotalShirts + SubTotalPants +

SubTotalDresses;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");Console.WriteLine(HomePhone);Console.Write("Order Date: ");

63

Page 64: Introduction to C#

Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.Write(PriceOneShirt);Console.Write(" ");Console.WriteLine(SubTotalShirts);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.Write(PriceAPairOfPants);Console.Write(" ");Console.WriteLine(SubTotalPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.Write(PriceOneDress);Console.Write(" ");Console.WriteLine(SubTotalDresses);Console.WriteLine("------------------------------------");Console.Write("Number of Items: ");Console.WriteLine(TotalNumberOfItems);Console.Write("Total Order: ");Console.WriteLine(TotalOrder);Console.WriteLine("====================================");Console.WriteLine();

}}

}

2. Execute the program. This would produce: 

-/- Georgetown Cleaning Services -/-====================================Customer: James BurreckHome Phone: (202) 301-7030Order Date: 3/15/2002------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 5 0.95 4.75Pants 2 2.95 5.90Dresses 3 4.55 13.65------------------------------------Number of Items: 10Total Order: 24.30====================================

3. Return to Visual C#

64

Page 65: Introduction to C#

The Subtraction

The subtraction operation is used to take out or subtract a value from another value. It is essentially the opposite of the addition. The subtraction is performed with the - sign.

Here is an example:

using System;

namespace Operations{

class Exercise{

static void Main(){

// Values used in this programdouble Value1 = 224.58, Value2 = 1548.26;double Result = Value1 - Value2;

Console.Write(Value1);Console.Write(" - ");Console.Write(Value2);Console.Write(" = ");Console.WriteLine(Result);

}}

}

This would produce:

224.58 - 1548.26 = -1323.68

Unlike the addition, the subtraction is not associative. In other words, a - b - c is not the same as c - b - a. Consider the following program that illustrates this:

using System;

namespace Operations{

class Exercise{

static void Main(){

// This tests whether the addition is associativeConsole.WriteLine(" =+= Addition =+=");Console.Write("128 + 42 + 5 = ");Console.WriteLine(128 + 42 + 5);Console.Write(" 5 + 42 + 128 = ");Console.WriteLine(5 + 42 + 128);

Console.WriteLine();

// This tests whether the subtraction is associativeConsole.WriteLine(" =-= Subtraction =-=");Console.Write("128 - 42 - 5 = ");Console.WriteLine(128 - 42 - 5);

65

Page 66: Introduction to C#

Console.Write(" 5 - 42 - 128 = ");Console.WriteLine(5 - 42 - 128);

Console.WriteLine();}

}}

This would produce:

=+= Addition =+=128 + 42 + 5 = 175 5 + 42 + 128 = 175

=-= Subtraction =-=128 - 42 - 5 = 81 5 - 42 - 128 = -165

Notice that both operations of the addition convey the same result. In the subtraction section, the numbers follow the same order but produce different results.

Practical Learning: Using the Subtraction Operator

1. To Subtract, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

const decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal SalesTaxRate = 0.0575M; // 5.75%

string CustomerName = "James Burreck", HomePhone = "(202) 301-7030";

uint NumberOfShirts = 5, NumberOfPants = 2, NumberOfDresses = 3;

uint TotalNumberOfItems;decimal SubTotalShirts, SubTotalPants, SubTotalDresses;decimal TaxAmount, TotalOrder, NetPrice;uint OrderMonth = 3, OrderDay = 15, OrderYear = 2002;

TotalNumberOfItems = NumberOfShirts + NumberOfPants + NumberOfDresses;

SubTotalShirts = PriceOneShirt * NumberOfShirts;SubTotalPants = PriceAPairOfPants * NumberOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;TotalOrder = SubTotalShirts + SubTotalPants +

SubTotalDresses;

66

Page 67: Introduction to C#

TaxAmount = TotalOrder * SalesTaxRate;NetPrice = TotalOrder - TaxAmount;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");Console.WriteLine(HomePhone);Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.Write(PriceOneShirt);Console.Write(" ");Console.WriteLine(SubTotalShirts);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.Write(PriceAPairOfPants);Console.Write(" ");Console.WriteLine(SubTotalPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.Write(PriceOneDress);Console.Write(" ");Console.WriteLine(SubTotalDresses);Console.WriteLine("------------------------------------");Console.Write("Number of Items: ");Console.WriteLine(TotalNumberOfItems);Console.Write("Total Order: ");Console.WriteLine(TotalOrder);Console.Write("Tax Rate: ");Console.Write(SalesTaxRate * 100);Console.WriteLine('%');Console.Write("Tax Amount: ");Console.WriteLine(TaxAmount);Console.Write("Net Price: ");Console.WriteLine(NetPrice);Console.WriteLine("====================================");Console.WriteLine();

}}

}

2. Execute the program. This would produce: 

67

Page 68: Introduction to C#

-/- Georgetown Cleaning Services -/-====================================Customer: James BurreckHome Phone: (202) 301-7030Order Date: 3/15/2002------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 5 0.95 4.75Pants 2 2.95 5.90Dresses 3 4.55 13.65------------------------------------Number of Items: 10Total Order: 24.30Discount Rate: 12.500%Discount Amount: 3.03750Net Price: 21.26250====================================

3. Return to Visual C#

The Division

Dividing an item means cutting it in pieces or fractions of a set value. For example, when you cut an apple in the middle, you are dividing it in 2 pieces. If you cut each one of the resulting pieces, you will get 4 pieces or fractions. This is considered that you have divided the apple in 4 parts. Therefore, the division is used to get the fraction of one number in terms of another. The division is performed with the forward slash /.

Here is an example:

namespace Operations{

class Exercise{

static void Main(){

// Initializing various variables when declaring them with the same data type

double Value1 = 224.58, Value2 = 1548.26;double Result = Value1 / Value2;

System.Console.Write(Value1);System.Console.Write(" / ");System.Console.Write(Value2);System.Console.Write(" = ");System.Console.WriteLine(Result);System.Console.WriteLine();

}}

}

This would produce:

224.58 / 1548.26 = 0.145053156446592

68

Page 69: Introduction to C#

When performing the division, be aware of its many rules. Never divide by zero (0). Make sure that you know the relationship(s) between the numbers involved in the operation.

 

Practical Learning: Using the Division Operator

1. To use the division, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

const decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal DiscountRate = 0.20M; // 20%const decimal TaxRate = 5.75M; // 5.75%

string CustomerName = "James Burreck", HomePhone = "(202) 301-7030";

uint NumberOfShirts = 5, NumberOfPants = 2, NumberOfDresses = 3;

uint TotalNumberOfItems;decimal SubTotalShirts, SubTotalPants, SubTotalDresses;decimal DiscountAmount, TotalOrder, NetPrice, TaxAmount,

SalesTotal;decimal AmountTended, Difference;uint OrderMonth = 3, OrderDay = 15, OrderYear = 2002;

TotalNumberOfItems = NumberOfShirts + NumberOfPants + NumberOfDresses;

SubTotalShirts = PriceOneShirt * NumberOfShirts;SubTotalPants = PriceAPairOfPants * NumberOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;TotalOrder = SubTotalShirts + SubTotalPants +

SubTotalDresses;DiscountAmount = TotalOrder * DiscountRate;NetPrice = TotalOrder - DiscountAmount;TaxAmount = TotalOrder * TaxRate / 100;SalesTotal = NetPrice + TaxAmount;AmountTended = 50M;Difference = AmountTended - SalesTotal;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");

69

Page 70: Introduction to C#

Console.WriteLine(HomePhone);Console.Write("Order Date: ");Console.Write(OrderMonth);Console.Write('/');Console.Write(OrderDay);Console.Write('/');Console.WriteLine(OrderYear);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.Write(PriceOneShirt);Console.Write(" ");Console.WriteLine(SubTotalShirts);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.Write(PriceAPairOfPants);Console.Write(" ");Console.WriteLine(SubTotalPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.Write(PriceOneDress);Console.Write(" ");Console.WriteLine(SubTotalDresses);Console.WriteLine("------------------------------------");Console.Write("Number of Items: ");Console.WriteLine(TotalNumberOfItems);Console.Write("Total Order: ");Console.WriteLine(TotalOrder);Console.Write("Discount Rate: ");Console.Write(DiscountRate * 100);Console.WriteLine('%');Console.Write("Discount Amount: ");Console.WriteLine(DiscountAmount);Console.Write("After Discount: ");Console.WriteLine(NetPrice);Console.Write("Tax Rate: ");Console.Write(TaxRate);Console.WriteLine('%');Console.Write("Tax Amount: ");Console.WriteLine(TaxAmount);Console.Write("Net Price: ");Console.WriteLine(SalesTotal);Console.WriteLine("====================================");Console.Write("Amount Tended: ");Console.WriteLine(AmountTended);Console.Write("Difference: ");Console.WriteLine(Difference);Console.WriteLine("====================================");

Console.WriteLine();}

70

Page 71: Introduction to C#

}}

2. Execute the program. This would produce: 

3. Return to Visual C#

The Remainder

The division program above will give you a result of a number with decimal values if you type an odd number (like 147), which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. Imagine you have 26 kids at a football (soccer) stadium and they are about to start. You know that you need 11 kids for each team to start. If the game starts with the right amount of players, how many will seat and wait?

The remainder operation is performed with the percent sign (%) which is gotten from pressing Shift + 5.

Here is an example:

// Program used to perform the remainder operation.using System;

class Exercise{ static void Main() {

int Players = 26;

// When the game starts, how many players will wait?.Console.Write("Out of ");

71

Page 72: Introduction to C#

Console.Write(Players);Console.Write(" players, ");Console.Write(26 % 11);Console.WriteLine(" players will have to wait when the game starts.\n");

}}

This would produce:

Out of 26 players, 4 players will have to wait when the game starts.

C# Language Operators 

Increment ++

Introduction

We are used to counting numbers such as 1, 2, 3, 4, etc. In reality, when counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. The simplest technique of incrementing a value consists of adding 1 to it. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. This is illustrated in the following example:

// This program studies value incrementingusing System;

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of incrementing a value");Console.Write("Value = ");Console.WriteLine(Value); 

Value = Value + 1;

Console.Write("Value = ");Console.WriteLine(Value); 

}}

This would produce:

Techniques of incrementing a valueValue = 12Value = 13

C# provides a special operator that takes care of this operation. The operator is called the increment operator and is represented by ++. Instead of writing Value = Value + 1, you can write Value++ and you would get the same result. The above program can be re-written as follows:

72

Page 73: Introduction to C#

// This program studies value incrementingusing System;

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of incrementing a value");Console.Write("Value = ");Console.WriteLine(Value); 

Value++;

Console.Write("Value = ");Console.WriteLine(Value); 

}}

The ++ is a unary operator because it operates on only one variable. It is used to modify the value of the variable by adding 1 to it. Every time the Value++ is executed, the compiler takes the previous value of the variable, adds 1 to it, and the variable holds the incremented value:

// This program studies value incrementingusing System;

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of incrementing a value");

Value++;Console.Write("Value = ");Console.WriteLine(Value); 

Value++;Console.Write("Value = ");Console.WriteLine(Value);

Value++;Console.Write("Value = ");Console.WriteLine(Value);

}}

This would produce:

Techniques of incrementing a valueValue = 13Value = 14Value = 15 

73

Page 74: Introduction to C#

Pre and Post-Increment

When using the ++ operator, the position of the operator with regard to the variable it is modifying can be significant. To increment the value of the variable before re-using it, you should position the operator on the left of the variable:

// This program studies value incrementingusing System;

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of incrementing a value");

Console.Write("Value = ");Console.WriteLine(Value); 

Console.Write("Value = ");Console.WriteLine(++Value);

Console.Write("Value = ");Console.WriteLine(Value);

}}

This would produce:

Techniques of incrementing a valueValue = 12Value = 13Value = 13

When writing ++Value, the value of the variable is incremented before being called. On the other hand, if you want to first use a variable, then increment it, in other words, if you want to increment the variable after calling it, position the increment operator on the right side of the variable:

// This program studies value incrementingusing System;

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of incrementing a value");

Console.Write("Value = ");Console.WriteLine(Value); 

74

Page 75: Introduction to C#

Console.Write("Value = ");Console.WriteLine(Value++);

Console.Write("Value = ");Console.WriteLine(Value);

}}

This would produce:

Techniques of incrementing a valueValue = 12Value = 12Value = 13

Decrement --

Introduction

When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a value. This operation works as if a value is decremented by 1, as in Value = Value – 1:

// This program studies value decrementingusing System;

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of decrementing a value");Console.Write("Value = ");Console.WriteLine(Value); 

Value = Value - 1;

Console.Write("Value = ");Console.WriteLine(Value); 

}}

This would produce:

Techniques of decrementing a valueValue = 12Value = 11

As done to increment, C# provides a quicker way of subtracting 1 from a value. This is done using the decrement operator, that is --. To use the decrement operator, type –- on the left or the right side of the variable when this operation is desired. Using the decrement operator, the above program could be written:

// This program studies value decrementingusing System;

75

Page 76: Introduction to C#

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of decrementing a value");Console.Write("Value = ");Console.WriteLine(Value); 

Value--;

Console.Write("Value = ");Console.WriteLine(Value); 

}}

Pre-Decrement

Once again, the position of the operator can be important. If you want to decrement the variable before calling it, position the decrement operator on the left side of the operand. This is illustrated in the following program:

// This program studies value decrementingusing System;

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of decrementing a value");Console.Write("Value = ");Console.WriteLine(Value); 

Console.Write("Value = ");Console.WriteLine(--Value);

Console.Write("Value = ");Console.WriteLine(Value);

}}

This would produce:

Techniques of decrementing a valueValue = 12Value = 11Value = 11

If you plan to decrement a variable only after it has been accessed, position the operator on the right side of the variable. Here is an example:

// This program studies value decrementingusing System;

76

Page 77: Introduction to C#

class Exercise{

static void Main(){

int Value = 12;

Console.WriteLine("Techniques of decrementing a value");Console.Write("Value = ");Console.WriteLine(Value); 

Console.Write("Value = ");Console.WriteLine(Value--);

Console.Write("Value = ");Console.WriteLine(Value);

}}

This would produce:

Techniques of decrementing a valueValue = 12Value = 12Value = 11

Techniques of Incrementing and Decrementing a Variable

It is not unusual to add or subtract a constant value to or from a variable. All you have to do is to declare another variable that would hold the new value. Here is an example:

// This program studies value incrementing and decrementingusing System;

class Exercise{

static void Main(){

double Value = 12.75;double NewValue;

Console.WriteLine("Techniques of incrementing and decrementing a value");

Console.Write("Value = ");Console.WriteLine(Value);

NewValue = Value + 2.42;

Console.Write("Value = ");Console.WriteLine(NewValue); 

}}

This would produce:

Techniques of incrementing and decrementing a valueValue = 12.75

77

Page 78: Introduction to C#

Value = 15.17

The above technique requires that you use an extra variable in your application. The advantage is that each value can hold its own value although the value of the second variable depends on whatever would happen to the original or source variable.

Sometimes in your program you will not need to keep the original value of the source variable. You may want to permanently modify the value that a variable is holding. In this case you can perform the addition operation directly on the variable by adding the desired value to the variable. This operation modifies whatever value a variable is holding and does not need an additional variable.

To add a value to a variable and change the value that the variable is holding, you can combine the assignment “=” and the addition “+” operators to produce a new operator as +=

Here is an example:

// This program studies value incrementing and decrementingusing System;

class Exercise{

static void Main(){

double Value = 12.75;

Console.WriteLine("Techniques of incrementing and decrementing a value");

Console.Write("Value = ");Console.WriteLine(Value);

Value += 2.42;

Console.Write("Value = ");Console.WriteLine(Value); 

}}

This program produces the same result as the previous. To decrement the value of a variable, instead of the addition, use the subtraction and apply the same technique. In the above program, the variable can have its value decremented by combining the assignment and the subtraction operations on the variable. This is done with the -= operator. Here is an example:

// This program studies value incrementing and decrementingusing System;

class Exercise{

static void Main(){

double Value = 12.75;

Console.WriteLine("Techniques of incrementing and decrementing a value");

Console.Write("Value = ");Console.WriteLine(Value);

78

Page 79: Introduction to C#

Value -= 2.42;

Console.Write("Value = ");Console.WriteLine(Value); 

}}

This would produce:

Techniques of incrementing and decrementing a valueValue = 12.75Value = 10.33

Operator Precedence and Direction

When combining operations in C#, there are two aspects involved: an operator's precedence and its direction. If you ask the compiler to add two numbers, for example 240 + 65, it will execute the operation by adding 240 to 65. In other words, it would read 240, then +, then 65, and evaluate the result. This is considered as operating from left to right: 240 -> + -> 65. This process is referred to as the direction of the operation.

As you will regularly combine operators on your various calculations, each operation is known for how much it "weighs" as compared to other operators. This is known as its precedence. This means that when a certain operator is combined with another, such as a + b * c, or x / y - z, some operators would execute before others, almost regardless of how you write the operation. That's why an operator could be categorized by its level of precedence.

One way you can avoid being concerned by the level of precedence of operator is by using parentheses to tell the compiler how to proceed with the operations, that is, what operation should (must) be performed first. Consider the following algebraic operation:

154 - 12 + 8

The question here is to know whether you want to subtract the addition of 12 and 8 from 154 or you want to add the difference between 154 and 12 to 8. Using parentheses, you can communicate your intentions to the compiler. This is illustrated in the following program:

using System;

class Exercise{

static void Main(){

int Operation1 = (154 - 12) + 8;int Operation2 = 154 - (12 + 8);

Console.Write("Examining the effect of the parentheses in an operation");

Console.Write("\n(154 - 12) + 8 = ");Console.Write(Operation1);Console.Write("\n154 - (12 + 8) = ");Console.Write(Operation2);Console.WriteLine();

}}

79

Page 80: Introduction to C#

This would produce:

Examining the effect of the parentheses in an operation(154 - 12) + 8 = 150154 - (12 + 8) = 134

As you can see, using the parentheses controls how the whole operation would proceed. This difference can be even more accentuated if your operation includes 3 or more operators and 4 or more operands.

 

 

Data Reading and Formatting 

Data Reading 

80

Page 81: Introduction to C#

 

Introduction

In previous lessons, we saw that the Console class allows using the Write() and the WriteLine() functions to display things on the screen. While the Console.Write() method is used to display something on the screen, the Console class provides the Read() method to get a value from the user. To use it, the name of a variable can be assigned to it. The syntax used is:

VariableName = Console.Read();

This simply means that, when the user types something and presses Enter, what the user had typed would be given (the word is assigned) to the variable specified on the left side of the assignment operator.

Read() doesn't always have to assign its value to a variable. For example, it can be used on its own line, which simply means that the user is expected to type something but the value typed by the user would not be used for any significant purpose. For example some versions of C# (even including Microsoft's C# and Borland C#Builder) would display the DOS window briefly and disappear. You can use the Read() function to wait for the user to press any key in order to close the DOS window.

Besides Read(), the Console class also provides the ReadLine() method. Like the WriteLine() member function, after performing its assignment, the ReadLine() method sends the caret to the next line. Otherwise, it plays the same role as the Read() function.

Practical Learning: Introducing Data Reading1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with GCS3 and specify the desired path in the Location

6. Click OK

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise

10. Click Open

11. In the empty file, type the following: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main()

81

Page 82: Introduction to C#

{Console.WriteLine();

}}

}

12. To execute the application, on the main menu, click Debug -> Start Without Debugging

 

String Value Request 

In most assignments of your programs, you will not know the value of a string when writing your application. For example, you may want the user to provide such a string. To request a string (or any of the variables we will see in this lesson), you can call the Console.Read() or the Console.ReadLine() function and assign it to the name of the variable whose value you want to retrieve. Here is an example:

string FirstName;Console.Write("Enter First Name: ");FirstName = Console.ReadLine();

 

Practical Learning: Reading String Values

1. To request strings from the user, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

string CustomerName, HomePhone;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request customer information from the userConsole.Write("Enter Customer Name: ");CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");HomePhone = Console.ReadLine();

Console.WriteLine();// Display the receiptConsole.WriteLine("====================================");Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");Console.WriteLine(HomePhone);Console.WriteLine("====================================\n");

}

82

Page 83: Introduction to C#

}}

2. To execute the program, on the main menu, click Debug -> Start Without Debugging. This would produce: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: James WatsonEnter Customer Phone: (410) 493-2005

====================================-/- Georgetown Cleaning Services -/-====================================Customer: James WatsonHome Phone: (410) 493-2005====================================

3. Return to Visual C# notepad

Number Request

In C#, everything the user types is a string and the compiler would hardly analyze it without your explicit asking it to do so. Therefore, if you want to get a number from the user, first request a string. Here is an example:

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

int Number;string strNumber;

strNumber = Console.ReadLine();}

}}

After getting the string, you must convert it to a number. To perform this conversion, each data type of the .NET Framework provides a mechanism called Parse. To use Parse(), type the data type, followed by a period, followed by Parse, and followed by parentheses. In the parentheses of Parse, type the string that you requested from the user. Here is an example:

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main()

83

Page 84: Introduction to C#

{int Number;string strNumber;

strNumber = Console.ReadLine();Number = int.Parse(strNumber);

}}

}

An advanced but faster way to do this is to type Console.ReadLine() in the parentheses of Parse. This has the same effect. Here is an example:

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

int Number;

Number = int.Parse(Console.ReadLine());}

}}

 

Practical Learning: Reading Numeric Values

1. To retrieve various numbers from the user, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 0.0575M; // 5.75%

// Customer personal infoirmationstring CustomerName, HomePhone;// Unsigned numbers to represent cleaning itemsuint NumberOfShirts, NumberOfPants, NumberOfDresses;// Each of these sub totals will be used for cleaning itemsdecimal SubTotalShirts, SubTotalPants, SubTotalDresses;

84

Page 85: Introduction to C#

// Values used to process an orderdecimal TotalOrder, TaxAmount, SalesTotal;decimal AmountTended, Difference;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request customer information from the userConsole.Write("Enter Customer Name: ");CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");HomePhone = Console.ReadLine();

// Request the quantity of each category of itemsConsole.Write("Number of Shirts: ");string strShirts = Console.ReadLine();NumberOfShirts = uint.Parse(strShirts);

Console.Write("Number of Pants: ");string strPants = Console.ReadLine();NumberOfPants = uint.Parse(strPants);

Console.Write("Number of Dresses: ");string strDresses = Console.ReadLine();NumberOfDresses = uint.Parse(strDresses);

// Perform the necessary calculationsSubTotalShirts = NumberOfShirts * PriceOneShirt;SubTotalPants = NumberOfPants * PriceAPairOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;// Calculate the "temporary" total of the orderTotalOrder = SubTotalShirts + SubTotalPants +

SubTotalDresses;

// Calculate the tax amount using a constant rateTaxAmount = TotalOrder * TaxRate;// Add the tax amount to the total orderSalesTotal = TotalOrder + TaxAmount;

// Communicate the total to the user...Console.Write("\nThe Total order is: ");Console.WriteLine(SalesTotal);// and request money for the orderConsole.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;Console.WriteLine();

// Display the receiptConsole.WriteLine("====================================");Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");

85

Page 86: Introduction to C#

Console.WriteLine(HomePhone);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.Write(PriceOneShirt);Console.Write(" ");Console.WriteLine(SubTotalShirts);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.Write(PriceAPairOfPants);Console.Write(" ");Console.WriteLine(SubTotalPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.Write(PriceOneDress);Console.Write(" ");Console.WriteLine(SubTotalDresses);Console.WriteLine("------------------------------------");Console.Write("Total Order: ");Console.WriteLine(TotalOrder);Console.Write("Tax Rate: ");Console.Write(TaxRate * 100);Console.WriteLine('%');Console.Write("Tax Amount: ");Console.WriteLine(TaxAmount);Console.Write("Net Price: ");Console.WriteLine(SalesTotal);Console.WriteLine("------------------------------------");Console.Write("Amount Tended: ");Console.WriteLine(AmountTended);Console.Write("Difference: ");Console.WriteLine(Difference);Console.WriteLine("====================================");

}}

}

2. Save the file

3. At the Command Prompt, change to the GCS3 folder

4. To compile the program, type csc /out:"Georgetown Cleaning Services".exe Exercise.cs and press Enter

5. To execute the program, type "Georgetown Cleaning Services" and press Enter. Here is an example of running the program: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: Genevieve AltonEnter Customer Phone: (202) 974-8244Number of Shirts: 8Number of Pants: 2

86

Page 87: Introduction to C#

Number of Dresses: 3

The Total order is: 28.711125Amount Tended? 30

====================================-/- Georgetown Cleaning Services -/-====================================Customer: Genevieve AltonHome Phone: (202) 974-8244------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 8 0.95 7.60Pants 2 2.95 5.90Dresses 3 4.55 13.65------------------------------------Total Order: 27.15Tax Rate: 5.7500%Tax Amount: 1.561125Net Price: 28.711125------------------------------------Amount Tended: 30Difference: 1.288875====================================

6. Return to Notepad

 

Requesting Dates and Times

As done with the regular numbers, you can request a date value from the user. This is also done by requesting a string from the user. Here is an example:

using System;

namespace ValueRequests{

class Exercise{

static void Main(){

string strDateHired;

strDateHired = Console.ReadLine();}

}}

After the user has entered the string you can then convert it to a DateTime value. Just like any value you request from the user, a date or time value that the user types must be valid, otherwise, the program would produce an error. Because dates and times follow some rules for their formats, you should strive to let the user know how you expect the value to be entered.

By default, if you request only a date from the user and the user enters a valid date, the compiler would add the midnight value to the date. If you request only the time from the user

87

Page 88: Introduction to C#

and the user enters a valid time, the compiler would add the current date to the value. Later on, we will learn how to isolate either only the date or only the time.

Practical Learning: Requesting Date and Time Values

1. To deal with new dates and times, change the program as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 0.0575M; // 5.75%

// Basic information about an orderstring CustomerName, HomePhone;DateTime OrderDate;// Unsigned numbers to represent cleaning itemsuint NumberOfShirts, NumberOfPants, NumberOfDresses;// Each of these sub totals will be used for cleaning itemsdecimal SubTotalShirts, SubTotalPants, SubTotalDresses;// Values used to process an orderdecimal TotalOrder, TaxAmount, SalesTotal;decimal AmountTended, Difference;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");HomePhone = Console.ReadLine();Console.WriteLine("Enter the order date and time (mm/dd/yyyy hh:mm

AM/PM)");OrderDate = DateTime.Parse(Console.ReadLine());

// Request the quantity of each category of itemsConsole.Write("Number of Shirts: ");string strShirts = Console.ReadLine();NumberOfShirts = uint.Parse(strShirts);

Console.Write("Number of Pants: ");string strPants = Console.ReadLine();NumberOfPants = uint.Parse(strPants);

Console.Write("Number of Dresses: ");string strDresses = Console.ReadLine();

88

Page 89: Introduction to C#

NumberOfDresses = uint.Parse(strDresses);

// Perform the necessary calculationsSubTotalShirts = NumberOfShirts * PriceOneShirt;SubTotalPants = NumberOfPants * PriceAPairOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;// Calculate the "temporary" total of the orderTotalOrder = SubTotalShirts + SubTotalPants +

SubTotalDresses;

// Calculate the tax amount using a constant rateTaxAmount = TotalOrder * TaxRate;// Add the tax amount to the total orderSalesTotal = TotalOrder + TaxAmount;

// Communicate the total to the user...Console.Write("\nThe Total order is: ");Console.WriteLine(SalesTotal);// and request money for the orderConsole.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;Console.WriteLine();

// Display the receiptConsole.WriteLine("====================================");Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.Write("Customer: ");Console.WriteLine(CustomerName);Console.Write("Home Phone: ");Console.WriteLine(HomePhone);Console.Write("Date & Time: ");Console.WriteLine(OrderDate);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.Write("Shirts ");Console.Write(NumberOfShirts);Console.Write(" ");Console.Write(PriceOneShirt);Console.Write(" ");Console.WriteLine(SubTotalShirts);Console.Write("Pants ");Console.Write(NumberOfPants);Console.Write(" ");Console.Write(PriceAPairOfPants);Console.Write(" ");Console.WriteLine(SubTotalPants);Console.Write("Dresses ");Console.Write(NumberOfDresses);Console.Write(" ");Console.Write(PriceOneDress);

89

Page 90: Introduction to C#

Console.Write(" ");Console.WriteLine(SubTotalDresses);Console.WriteLine("------------------------------------");Console.Write("Total Order: ");Console.WriteLine(TotalOrder);Console.Write("Tax Rate: ");Console.Write(TaxRate * 100);Console.WriteLine('%');Console.Write("Tax Amount: ");Console.WriteLine(TaxAmount);Console.Write("Net Price: ");Console.WriteLine(SalesTotal);Console.WriteLine("------------------------------------");Console.Write("Amount Tended: ");Console.WriteLine(AmountTended);Console.Write("Difference: ");Console.WriteLine(Difference);Console.WriteLine("====================================");

}}

}

2. Save the file

3. At the Command Prompt, change to the GCS3 folder

4. To compile the program, type csc /out:"Georgetown Cleaning Services".exe Exercise.cs and press Enter

5. To execute the program, type "Georgetown Cleaning Services" and press Enter. Here is an example of running the program: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: Alexander PappasEnter Customer Phone: (301) 397-9764Enter the order date and time (mm/dd/yyyy hh:mm AM/PM)06/22/98 08:26 AMNumber of Shirts: 2Number of Pants: 6Number of Dresses: 0

The Total order is: 20.727000Amount Tended? 50

====================================-/- Georgetown Cleaning Services -/-====================================Customer: Alexander PappasHome Phone: (301) 397-9764Date & Time: 6/22/1998 8:26:00 AM------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 2 0.95 1.90Pants 6 2.95 17.70Dresses 0 4.55 0------------------------------------

90

Page 91: Introduction to C#

Total Order: 19.60Tax Rate: 5.7500%Tax Amount: 1.127000Net Price: 20.727000------------------------------------Amount Tended: 50Difference: 29.273000====================================

6. Return to Notepad

Formatting Data Display 

Introduction

Instead of using two Write() or a combination of Write() and WriteLine() to display data, you can convert a value to a string and display it directly. To do this, you can provide two strings to the Write() or WriteLine() and separate them with a comma:

1. The first part of the string provided to Write() or WriteLine() is the complete string that would display to the user. This first string itself can be made of different sections:

a. One section is a string in any way you want it to display

b. Another section is a number included between an opening curly bracket "{" and a closing curly bracket "}". This combination of "{" and "}" is referred to as a placeholder

You can put the placeholder anywhere inside of the string. The first placeholder must have number 0. The second must have number 1, etc. With this technique, you can create the string anyway you like and use the placeholders anywhere inside of the string

2. The second part of the string provided to Write() or WriteLine() is the value that you want to display. It can be one value if you used only one placeholder with 0 in the first string. If you used different placeholders, you can then provide a different value for each one of them in this second part, separating the values with a comma

Here are examples:

using System;

// An Exercise classclass Exercise{ static void Main() {

String FullName = "Anselme Bogos"; int Age = 15; double HSalary = 22.74;

Console.WriteLine("Full Name: {0}", FullName); Console.WriteLine("Age: {0}", Age); Console.WriteLine("Distance: {0}", HSalary);

91

Page 92: Introduction to C#

Console.WriteLine(); }}

This would produce:

Full Name: Anselme BogosAge: 15Distance: 22.74

As mentioned already, the numeric value typed in the curly brackets of the first part is an ordered number. If you want to display more than one value, provide each incremental value in its curly brackets. The syntax used is:

Write("To Display {0} {1} {2} {n}", First, Second, Third, nth);

You can use the sections between a closing curly bracket and an opening curly bracket to create a meaningful sentence.

 

Practical Learning: Displaying Data With Placeholders

1. To use curly brackets to display data, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 0.0575M; // 5.75%

// Basic information about an orderstring CustomerName, HomePhone;DateTime OrderDate;// Unsigned numbers to represent cleaning itemsuint NumberOfShirts, NumberOfPants, NumberOfDresses;// Each of these sub totals will be used for cleaning itemsdecimal SubTotalShirts, SubTotalPants, SubTotalDresses;// Values used to process an orderdecimal TotalOrder, TaxAmount, SalesTotal;decimal AmountTended, Difference;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");HomePhone = Console.ReadLine();

92

Page 93: Introduction to C#

Console.WriteLine("Enter the order date and time (mm/dd/yyyy hh:mm AM/PM)");

OrderDate = DateTime.Parse(Console.ReadLine());

// Request the quantity of each category of itemsConsole.Write("Number of Shirts: ");NumberOfShirts = uint.Parse(Console.ReadLine());

Console.Write("Number of Pants: ");NumberOfPants = uint.Parse(Console.ReadLine());

Console.Write("Number of Dresses: ");NumberOfDresses = uint.Parse(Console.ReadLine());

// Perform the necessary calculationsSubTotalShirts = NumberOfShirts * PriceOneShirt;SubTotalPants = NumberOfPants * PriceAPairOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;// Calculate the "temporary" total of the orderTotalOrder = SubTotalShirts + SubTotalPants +

SubTotalDresses;

// Calculate the tax amount using a constant rateTaxAmount = TotalOrder * TaxRate;// Add the tax amount to the total orderSalesTotal = TotalOrder + TaxAmount;

// Communicate the total to the user...Console.Write("\nThe Total order is: ");Console.WriteLine(SalesTotal);// and request money for the orderConsole.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;Console.WriteLine();

// Display the receiptConsole.WriteLine("====================================");Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.WriteLine("Customer: {0}", CustomerName);Console.WriteLine("Home Phone: {0}", HomePhone);Console.WriteLine("Date & Time: {0}", OrderDate);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.WriteLine("Shirts {0} {1} {2}",

NumberOfShirts, PriceOneShirt, SubTotalShirts);Console.WriteLine("Pants {0} {1} {2}",

NumberOfPants, PriceAPairOfPants, SubTotalPants);Console.WriteLine("Dresses {0} {1} {2}",

NumberOfDresses, PriceOneDress, SubTotalDresses);

93

Page 94: Introduction to C#

Console.WriteLine("------------------------------------");Console.WriteLine("Total Order: {0}", TotalOrder);Console.WriteLine("Tax Rate: {0}%", TaxRate * 100);Console.WriteLine("Tax Amount: {0}", TaxAmount);Console.WriteLine("Net Price: {0}", SalesTotal);Console.WriteLine("------------------------------------");Console.WriteLine("Amount Tended: {0}", AmountTended);Console.WriteLine("Difference: {0}", Difference);Console.WriteLine("====================================");

}}

}

2. Save, compile, and execute the program

Conversion To String

We mentioned earlier that everything the user types using the keyboard is primarily a string and it's your job to convert it to the appropriate type. In reverse, if you have a value that is not a string, you can easily convert it to a string. To support this, each .NET Framework data type provides a mechanism called ToString. Normally, in C#, as we mentioned with boxing, and as we have done so far, this conversion is automatically or transparently done by the compiler. In some cases, you will need to perform the conversion yourself.

To conversion a value of a primitive data type to a string, type the name of the variable, followed by a period, followed, followed by ToString(). Here is an example:

using System;

// An Exercise classclass Exercise{

static void Main(){

String FullName = "Anselme Bogos";int Age = 15;double HSalary = 22.74;

Console.WriteLine("Full Name: {0}", FullName);Console.WriteLine("Age: {0}", Age.ToString());Console.WriteLine("Distance: {0}", HSalary.ToString());

Console.WriteLine();

}}

In some cases, you will type something in the parentheses of ToString().

Practical Learning: Converting to String

1. To convert some values to string, change the program as follows: 

using System;

94

Page 95: Introduction to C#

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 0.0575M; // 5.75%

. . . No Change

Console.WriteLine("------------------------------------");Console.WriteLine("Shirts {0} {1} {2}",

NumberOfShirts.ToString(), PriceOneShirt,SubTotalShirts.ToString());

Console.WriteLine("Pants {0} {1} {2}",NumberOfPants, PriceAPairOfPants,

SubTotalPants);Console.WriteLine("Dresses {0} {1} {2}",

NumberOfDresses, PriceOneDress, SubTotalDresses);

Console.WriteLine("------------------------------------");Console.WriteLine("Total Order: {0}", TotalOrder);Console.WriteLine("Tax Rate: {0}%", TaxRate * 100);Console.WriteLine("Tax Amount: {0}",

TaxAmount.ToString());Console.WriteLine("Net Price: {0}", SalesTotal);Console.WriteLine("------------------------------------");Console.WriteLine("Amount Tended: {0}", AmountTended);Console.WriteLine("Difference: {0}", Difference);Console.WriteLine("====================================");

}}

}

2. Save, compile, and execute the program

Number Formatting

To properly display data in a friendly and most familiar way, you can format it. Formatting tells the compiler what kind of data you are using and how you want the compiler to display it to the user. As it happens, you can display a natural number in a common value or, depending on the circumstance, you may prefer to show it as a hexadecimal value. When it comes to double-precision numbers, you may want to display a distance with three values on the right side of the decimal separator and in some cases, you may want to display a salary with only 2 decimal places.

The System namespace provides a specific letter that you can use in the Write() or WriteLine()'s placeholder for each category of data to display. To format a value, in the placeholder of the variable or value, after the number, type a colon and one of the appropriate letter from the following table. If you are using ToString(), then, in the parentheses of

95

Page 96: Introduction to C#

ToString(), you can include a specific letter or combination inside of double-quotes. The letters and their meanings are:

  Character Used For

  c C Currency values

  d D Decimal numbers

  e E Scientific numeric display such as 1.45e5

  f F Fixed decimal numbers

  g G General and most common type of numbers

  n N Natural numbers

  r R Roundtrip formatting

  x X Hexadecimal formatting

  p P Percentages

Here are examples:

using System;

// An Exercise classclass Exercise{ static void Main() { double Distance = 248.38782; int Age = 15; int NewColor = 3478; double HSalary = 22.74, HoursWorked = 35.5018473; double WeeklySalary = HSalary * HoursWorked;

Console.WriteLine("Distance: {0}", Distance.ToString("E")); Console.WriteLine("Age: {0}", Age.ToString()); Console.WriteLine("Color: {0}", NewColor.ToString("X")); Console.WriteLine("Weekly Salary: {0} for {1} hours", WeeklySalary.ToString("c"), HoursWorked.ToString("F")); Console.WriteLine(); }}

This would produce:

Distance: 2.483878E+002Age: 15Color: D96Weekly Salary: $807.31 for 35.50 hours

As you may have noticed, if you leave the parentheses of ToString() empty, the compiler would use a default formatting to display the value.

As opposed to calling ToString(), you can use the above letters in the curly brackets of the first part of Write() or WriteLine(). In this case, after the number in the curly brackets, type the colon operator followed by the letter.

 

96

Page 97: Introduction to C#

Practical Learning: Formatting Data Display

1. To format data display, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 5.75M; // 5.75%

// Basic information about an orderstring CustomerName, HomePhone;DateTime OrderDate;// Unsigned numbers to represent cleaning itemsuint NumberOfShirts, NumberOfPants, NumberOfDresses;// Each of these sub totals will be used for cleaning itemsdecimal SubTotalShirts, SubTotalPants, SubTotalDresses;// Values used to process an orderdecimal TotalOrder, TaxAmount, SalesTotal;decimal AmountTended, Difference;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");HomePhone = Console.ReadLine();Console.WriteLine("Enter the order date and time (mm/dd/yyyy

hh:mm AM/PM)");OrderDate = DateTime.Parse(Console.ReadLine());

// Request the quantity of each category of itemsConsole.Write("Number of Shirts: ");NumberOfShirts = uint.Parse(Console.ReadLine());

Console.Write("Number of Pants: ");NumberOfPants = uint.Parse(Console.ReadLine());

Console.Write("Number of Dresses: ");NumberOfDresses = uint.Parse(Console.ReadLine());

// Perform the necessary calculationsSubTotalShirts = NumberOfShirts * PriceOneShirt;SubTotalPants = NumberOfPants * PriceAPairOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;// Calculate the "temporary" total of the order

97

Page 98: Introduction to C#

TotalOrder = SubTotalShirts + SubTotalPants + SubTotalDresses;

// Calculate the tax amount using a constant rateTaxAmount = TotalOrder * TaxRate;// Add the tax amount to the total orderSalesTotal = TotalOrder + TaxAmount;

// Communicate the total to the user...Console.Write("\nThe Total order is: ");Console.WriteLine(SalesTotal);// and request money for the orderConsole.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;Console.WriteLine();

// Display the receiptConsole.WriteLine("====================================");Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.WriteLine("Customer: {0}", CustomerName);Console.WriteLine("Home Phone: {0}", HomePhone);Console.WriteLine("Date & Time: {0}", OrderDate);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.WriteLine("Shirts {0} {1:C} {2}",

NumberOfShirts.ToString(), PriceOneShirt,SubTotalShirts.ToString("C"));

Console.WriteLine("Pants {0} {1:C} {2:C}",NumberOfPants, PriceAPairOfPants,SubTotalPants);

Console.WriteLine("Dresses {0} {1:C} {2:C}", NumberOfDresses, PriceOneDress,

SubTotalDresses);Console.WriteLine("------------------------------------");Console.WriteLine("Total Order: {0:C}", TotalOrder);Console.WriteLine("Tax Rate: {0:P}", TaxRate);Console.WriteLine("Tax Amount: {0}",

TaxAmount.ToString("C"));Console.WriteLine("Net Price: {0:F}", SalesTotal);Console.WriteLine("------------------------------------");Console.WriteLine("Amount Tended: {0:C}", AmountTended);Console.WriteLine("Difference: {0:C}", Difference);Console.WriteLine("====================================");

}}

}

2. Save it and switch to the Command Prompt. Then compile the file and execute the application. Here is an example: 

98

Page 99: Introduction to C#

-/- Georgetown Cleaning Services -/-Enter Customer Name: Gretchen McCormackEnter Customer Phone: (410) 739-2884Enter the order date and time (mm/dd/yyyy hh:mm AM/PM)04/09/2001 10:25 AMNumber of Shirts: 5Number of Pants: 12Number of Dresses: 8

The Total order is: 80.951625Amount Tended? 100

====================================-/- Georgetown Cleaning Services -/-====================================Customer: Gretchen McCormackHome Phone: (410) 739-2884Date & Time: 4/9/2001 10:25:00 AM------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 5 $0.95 $4.75Pants 12 $2.95 $35.40Dresses 8 $4.55 $36.40------------------------------------Total Order: $76.55Tax Rate: 5.75 %Tax Amount: $4.40Net Price: 80.95------------------------------------Amount Tended: $100.00Difference: $19.05====================================

3. Return to Notepad

Line Formatting

In the above programs, to display a line of text, we easily used Write() or WriteLine(). To position text of different lengths one above the other, we had to "corrupt" a string by including extra-empty spaces. Such a technique is uncertain and less professional. Fortunately, you can highly format how a string or a line of text should display. The .NET Framework provides mechanisms to control the amount of space used to display a string of text and how to align that string on its line.

To specify the amount of space used to display a string, you can use its placeholder in Write() or WriteLine(). To do this, in the placeholder, type the 0 or the incrementing number of the placer and its formatting character if necessary and if any. Then, type a comma followed by the number of characters equivalent to the desired width. Here are examples:

using System;

// An Exercise classclass Exercise{

99

Page 100: Introduction to C#

static void Main(){

String FullName = "Anselme Bogos";int Age = 15;double HSalary = 22.74;

Console.WriteLine("Full Name: {0,20}", FullName);Console.WriteLine("Age:{0,14}", Age.ToString());Console.WriteLine("Distance: {0:C,8}", HSalary.ToString());

Console.WriteLine();

}}

This would produce:

Full Name: Anselme BogosAge: 15Distance: 22.74

The sign you provide for the width is very important. If it is positive, the line of text is aligned to the right. This should be your preferred alignment for numeric values. If the number is negative, then the text is aligned to the left.

Data and Time Formatting

As mentioned earlier, when the user enters a date value for a DateTime variable, the compiler adds a time part to the value. Fortunately, if you want to consider only the date or only the time part, you can specify this to the compiler. To support this, the DateTime data type provides a series of letters you can use to format how its value should be displayed to the user. The character is entered in the placeholder of the DateTime variable after the 0 or the incremental numeric value.

Practical Learning: Controlling Date/Time Formatting

1. To control formatting of date and time, change the file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

static void Main(){

// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 0.0575M; // 5.75%

// Basic information about an orderstring CustomerName, HomePhone;

100

Page 101: Introduction to C#

DateTime OrderDate, OrderTime;// Unsigned numbers to represent cleaning itemsuint NumberOfShirts, NumberOfPants, NumberOfDresses;// Each of these sub totals will be used for cleaning itemsdecimal SubTotalShirts, SubTotalPants, SubTotalDresses;// Values used to process an orderdecimal TotalOrder, TaxAmount, SalesTotal;decimal AmountTended, Difference;

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");HomePhone = Console.ReadLine();Console.Write("Enter the order date(mm/dd/yyyy): ");OrderDate = DateTime.Parse(Console.ReadLine());Console.Write("Enter the order time(hh:mm AM/PM): ");OrderTime = DateTime.Parse(Console.ReadLine());

// Request the quantity of each category of itemsConsole.Write("Number of Shirts: ");NumberOfShirts = uint.Parse(Console.ReadLine());

Console.Write("Number of Pants: ");NumberOfPants = uint.Parse(Console.ReadLine());

Console.Write("Number of Dresses: ");NumberOfDresses = uint.Parse(Console.ReadLine());

// Perform the necessary calculationsSubTotalShirts = NumberOfShirts * PriceOneShirt;SubTotalPants = NumberOfPants * PriceAPairOfPants;SubTotalDresses = NumberOfDresses * PriceOneDress;// Calculate the "temporary" total of the orderTotalOrder = SubTotalShirts + SubTotalPants +

SubTotalDresses;

// Calculate the tax amount using a constant rateTaxAmount = TotalOrder * TaxRate;// Add the tax amount to the total orderSalesTotal = TotalOrder + TaxAmount;

// Communicate the total to the user...Console.WriteLine("\nThe Total order is: {0:C}",

SalesTotal);// and request money for the orderConsole.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;Console.WriteLine();

// Display the receipt

101

Page 102: Introduction to C#

Console.WriteLine("====================================");Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.WriteLine("Customer: {0}", CustomerName);Console.WriteLine("Home Phone: {0}", HomePhone);Console.WriteLine("Order Date: {0:D}", OrderDate);Console.WriteLine("Order Time: {0:t}", OrderTime);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.WriteLine("Shirts {0} {1} {2}",

NumberOfShirts.ToString(), PriceOneShirt.ToString("C"), SubTotalShirts.ToString("C"));

Console.WriteLine("Pants {0} {1} {2}",NumberOfPants.ToString(),

PriceAPairOfPants.ToString("C"),SubTotalPants.ToString("C"));

Console.WriteLine("Dresses {0} {1} {2}", NumberOfDresses.ToString(), PriceOneDress.ToString("C"), SubTotalDresses.ToString("C"));

Console.WriteLine("------------------------------------");Console.WriteLine("Total Order: {0}",

TotalOrder.ToString("C"));Console.WriteLine("Tax Rate: {0}",

TaxRate.ToString("P"));Console.WriteLine("Tax Amount: {0}",

TaxAmount.ToString("C"));Console.WriteLine("Net Price: {0}",

SalesTotal.ToString("C"));Console.WriteLine("------------------------------------");Console.WriteLine("Amount Tended: {0}",

AmountTended.ToString("C"));Console.WriteLine("Difference: {0}",

Difference.ToString("C"));Console.WriteLine("====================================");

}}

}

2. Save, compile, and run the program. Here is an example: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: Antoinette CalhounEnter Customer Phone: (703) 797-1135Enter the order date(mm/dd/yyyy): 04/12/2002Enter the order time(hh:mm AM/PM): 2:12 PMNumber of Shirts: 5Number of Pants: 2Number of Dresses: 1

The Total order is: $16.07Amount Tended? 20

====================================

102

Page 103: Introduction to C#

-/- Georgetown Cleaning Services -/-====================================Customer: Antoinette CalhounHome Phone: (703) 797-1135Order Date: Friday, April 12, 2002Order Time: 2:12 PM------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 5 $0.95 $4.75Pants 2 $2.95 $5.90Dresses 1 $4.55 $4.55------------------------------------Total Order: $15.20Tax Rate: 5.75 %Tax Amount: $0.87Net Price: $16.07------------------------------------Amount Tended: $20.00Difference: $3.93====================================

3. Type Exit and close Notepad

 

103

Page 104: Introduction to C#

 

Introduction to Conditions 

Overview of Conditions 

Introduction

A program is a series of instructions that ask the computer (actually the compiler) to check some situations and to act accordingly. To check such situations, the computer spends a great deal of its time performing comparisons between values. A comparison is a Boolean operation that produces a true or a false result, depending on the values on which the comparison is performed.

A comparison is performed between two values of the same type; for example, you can compare two numbers, two characters, or the names of two cities. On the other hand, a comparison between two disparate value doesn't bear any meaning. For example, it is difficult to compare a telephone number and somebody's age, or a music category and the distance between two points. Like the binary arithmetic operations, the comparison operations are performed on two values. Unlike arithmetic operations where results are varied, a comparison produces only one of two results. The result can be a logical true or false. When a comparison is true, it has an integral value of 1 or positive; that is, a value greater than 0. If the comparison is not true, it is considered false and carries an integral value of 0.

The C# language is equipped with various operators used to perform any type of comparison between similar values. The values could be numeric, strings, or objects (operations on objects are customized in a process referred to as Operator Overloading).

 

Introduction

There are primary assumptions you should make when writing statements used in conditions:

Simplicity and Clarity: A statement should be clear enough and possibly simple but as complete as possible. When a statement becomes long, it can lead to being segmented in short parts, which deceives its clarity and may create other issues.

Factual: The statement must be presented as fact and not as opinion. This means that you don't have to like the statement but the majority, including you, must agree that it is true. In fact, the statement doesn't have to be correct but it must be agreed upon to be true. Based on this, a statement such as "An hour contains 45 minutes" doesn't have to fit your way of thinking but it must be considered as true or as false. A statement such as "This job applicant is attractive" is an opinion and therefore must not be considered in a conditional statement. 

Circumstantial Truthfulness: At the time the statement is made, it must be considered as true even if it can change at another time. For example, supposed that, in a certain year, a

104

Page 105: Introduction to C#

statement is formulated as "This year, the month of February has 28 days". Although allowed, you should refrain from regularly using circumstantial truthfulness, unless you have to.

Inverse: A statement must be able to find its reverse. This means that, when a statement is made and decided upon to be true or false, an inverse statement must be found to make it false or true. For example, if you have a statement such as "This job applicant is 18 years old", you must be able to state that "This job applicant is not 18 years old" or "This job applicant is younger than 18".

In your programs, make sure you clearly formulate your statements. This would make your programs easy to read and troubleshoot when problems occur.

 

Logical Operations 

The Equality Operator ==

To compare two variables for equality, C# uses the == operator. Its syntax is:

Value1 == Value2

The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. From our syntax, the compiler would compare the value of Value1 with that of Value2. If Value1 and Value2 hold the same value, the comparison produces a true result. If they are different, the comparison renders false.

Most of the comparisons performed in C# will be applied to conditional statements. The result of a comparison can also be assigned to a variable. To store the result of a comparison, you should include the comparison operation between parentheses. Here is an example:

using System;

class NewProject{

105

Page 106: Introduction to C#

static void Main(){

int Value1 = 15;int Value2 = 24;

Console.Write("Value 1 = ");Console.WriteLine(Value1);Console.Write("Value 2 = ");Console.WriteLine(Value2);Console.Write("Comparison of Value1 == 15 produces ");Console.WriteLine(Value1 == 15);

}}

This would produce:

Value 1 = 15Value 2 = 24Comparison of Value1 == 15 produces True

It is important to make a distinction between the assignment "=" and the logical equality operator "==". The first is used to give a new value to a variable, as in Number = 244. The operand on the left side of = must always be a variable and never a constant. The == operator is never used to assign a value; this would cause an error. The == operator is used only to compare to values. The operands on both sides of == can be variables, constants, or one can be a variable while the other is a constant. If you use one operator in place of the other, you would receive an error when you compile the program.

The Logical Not Operator !

When a variable is declared and receives a value (this could be done through initialization or a change of value) in a program, it becomes alive. It can then participate in any necessary operation. The compiler keeps track of every variable that exists in the program being processed. When a variable is not being used or is not available for processing (in visual programming, it would be considered as disabled) to make a variable (temporarily) unusable, you can nullify its value. C# considers that a variable whose value is null is stern. To render a variable unavailable during the evolution of a program, apply the logical not operator which is !. Its syntax is:

!Value

There are two main ways you can use the logical not operator. As we will learn when studying conditional statements, the most classic way of using the logical not operator is to check the state of a variable.

To nullify a variable, you can write the exclamation point to its left. When used like that, you can display its value using the cout extractor. You can even assign it to another variable. Here is an example: 

using System;

class NewProject{

static void Main()

106

Page 107: Introduction to C#

{bool HasAirCondition = true;bool DoesIt;

Console.Write("HasAirCondition = ");Console.WriteLine(HasAirCondition);DoesIt = !HasAirCondition;Console.Write("DoesIt = ");Console.WriteLine(DoesIt);

}}

This would produce:

HasAirCondition = TrueDoesIt = False

When a variable holds a value, it is "alive". To make it not available, you can "not" it. When a variable has been "notted", its logical value has changed. If the logical value was true, which is 1, it would be changed to false, which is 0. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it. 

The Inequality Operator !=

As opposed to Equality, C# provides another operator used to compare two values for inequality. This operation uses a combination of equality and logical not operators. It combines the logical not ! and a simplified == to produce !=. Its syntax is:

Value1 != Value2

The != is a binary operator (like all logical operator except the logical not, which is a unary operator) that is used to compare two values. The values can come from two variables as in Variable1 != Variable2. Upon comparing the values, if both variables hold different values, the comparison produces a true or positive value. Otherwise, the comparison renders false or a null value. 

107

Page 108: Introduction to C#

Here is an example:

using System;

class NewProject{

static void Main(){

int Value1 = 212;int Value2 = -46;bool Value3 = (Value1 != Value2);

Console.Write("Value1 = ");Console.WriteLine(Value1);Console.Write("Value2 = ");Console.WriteLine(Value2);Console.Write("Value3 = ");Console.Write(Value3);Console.WriteLine();

}}

The inequality is obviously the opposite of the equality.

The Comparison for a Lower Value <

To find out whether one value is lower than another, use the < operator. Its syntax is:

Value1 < Value2

The value held by Value1 is compared to that of Value2. As it would be done with other operations, the comparison can be made between two variables, as in Variable1 < Variable2. If the value held by Variable1 is lower than that of Variable2, the comparison produces a true or positive result. 

108

Page 109: Introduction to C#

Here is an example:

using System;

class NewProject{

static void Main(){

int Value1 = 15;bool Value2 = (Value1 < 24);

Console.Write("Value 1 = ");Console.WriteLine(Value1);Console.Write("Value 2 = ");Console.WriteLine(Value2);Console.WriteLine();

}}

This would produce:

Value 1 = 15Value 2 = True

Combining Equality and Lower Value <=

The previous two operations can be combined to compare two values. This allows you to know if two values are the same or if the first is less than the second. The operator used is <= and its syntax is:

Value1 <= Value2

The <= operation performs a comparison as any of the last two. If both Value1 and VBalue2 hold the same value, result is true or positive. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true.

Here is an example:

109

Page 110: Introduction to C#

using System;

class NewProject{

static void Main(){

int Value1 = 15;bool Value2 = (Value1 <= 24);

Console.Write("Value 1 = ");Console.WriteLine(Value1);Console.Write("Value 2 = ");Console.WriteLine(Value2);Console.WriteLine();

}}

This would produce:

Value 1 = 15Value 2 = True

The Comparison for a Greater Value >

When two values of the same type are distinct, one of them is usually higher than the other. C# provides a logical operator that allows you to find out if one of two values is greater than the other. The operator used for this operation uses the > symbol. Its syntax is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the > operator is greater than the value on the right side or a constant, the comparison produces a true or positive value . Otherwise, the comparison renders false or null.

  

110

Page 111: Introduction to C#

The Greater Than or Equal Operator >=

The greater than or the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. Its syntax is:

Value1 >= Value2

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a true or positive value. If the value of the left operand is greater than that of the right operand,, the comparison produces true or positive also. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false or null result.

Here is a summary table of the logical operators we have studied:

Operator Meaning Example Opposite

== Equality to a == b !=

!= Not equal to 12 != 7 ==

< Less than 25 < 84 >=

<= Less than or equal to Cab <= Tab >

> Greater than 248 > 55 <=

>= Greater than or equal to

Val1 >= Val2 <

 

111

Page 112: Introduction to C#

 

Conditional Statements 

Introduction to Conditional Statements  Introduction

Imagine you are writing a program for the Motor Vehicle Administration. When processing a driver's license, you want to be able to ask an applicant if he or she wants to be an organ donor. This section of the program can appear as follows:

using System;

class Exercise{

static void Main(){

string Answer;

Console.Write("Are you willing to be an organ donor? ");Answer = Console.ReadLine();

return 0;}

}

The possible answers to this question are y, yes, Y, Yes, YES, n, N, no, No, NO, I don’t know, It depends, Why are you asking?, What do you mean?, What kind of organ are you referring to? What kind of person would possibly want my organ? Are you planning to sell my body parts?, etc. When you ask this type of question, make sure you let the applicant know that this is a simple question expecting a simple answer. For example, the above question can be formulated as follows:

using System;

class Exercise{

static void Main(){

string Answer;

 

112

Page 113: Introduction to C#

Console.Write("Are you willing to be an organ donor (y=Yes/n=No)? ");Answer = Console.ReadLine();

return 0;}

}

This time, the applicant knows that the expected answers are simply y for Yes or n for No.

  Practical Learning: Introducing Conditional Statements1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with MVD1 and specify the desired path in the Location

6. Click OK

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise

10. Click Open

11. In the empty file, type the following: 

using System;

namespace MotorVehicleDivision{

class Exercise{

static void Main(){

string FirstName, LastName;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Application ---");

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Information ---");

Console.WriteLine("Full Name: {0} {1}\n", FirstName, LastName);

}

113

Page 114: Introduction to C#

}}

12. To execute the application, on the main menu, click Debug -> Start Without Debugging

13. Return to Visual C#

Boolean Variables

A variable is referred to as Boolean if it is meant to carry only one of two logical values stated as true or false. For this reason, such a variable is used only when testing conditions or when dealing with conditional statements.

To declare a Boolean variable, you can use the bool keyword. Here is an example of declaring a Boolean variable:

bool TheStudentIsHungry;

After declaring a Boolean variable, you can give it a value by assigning it a true or false value. Here is an example:

using System;

class ObjectName{

static void Main(){

bool TheStudentIsHungry;

TheStudentIsHungry = true;Console.Write("The Student Is Hungry expression is: ");Console.WriteLine(TheStudentIsHungry);TheStudentIsHungry = false;Console.Write("The Student Is Hungry expression is: ");Console.WriteLine(TheStudentIsHungry);

}}

This would produce:

The Student Is Hungry expression is: TrueThe Student Is Hungry expression is: False

You can also give its first value to a variable when declaring it. In this case, the above variable can be declared and initialized as follows:

bool TheStudentIsHungry = true;

  Practical Learning: Using a Boolean Variable

1. To use an example of a Boolean variable, change the file as follows: 

using System;

namespace MotorVehicleDivision{

class Exercise

114

Page 115: Introduction to C#

{static void Main(){

string FirstName, LastName;bool WillingToBeAnOrganDonor;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Application ---");

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();

Console.Write("Are you willing to be an Organ Donor(True=Yes/False=No)? ");

WillingToBeAnOrganDonor = bool.Parse(Console.ReadLine());

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Information ---");

Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);

Console.WriteLine("Organ Donor? {0}", WillingToBeAnOrganDonor);

return 0;}

}}

2. Execute the application. Here is an example: 

-=- Motor Vehicle Administration -=- --- Driver's License Application ---First Name: AhmedLast Name: KouakouAre you willing to be an Organ Donor(True=Yes/False=No)? True

-=- Motor Vehicle Administration -=- --- Driver's License Information ---Full Name: Ahmed KouakouOrgan Donor? True

3. Return to Visual C#

 

Formulations of Conditional Statements  if a Condition is True

One of the most regularly performed operations on a program consists of checking that a condition is true or false. When something is true, you may act one way. If it is false, you act another way. A

115

Page 116: Introduction to C#

condition that a program checks must be clearly formulated in a statement, following specific rules. The statement can come from you or from the computer itself. Examples of statements are:

"You are 12 years old"

"It is raining outside"

You live in Sydney"

One of the comparisons the computer performs is to find out if a statement is true (in reality, programmers (like you) write these statements and the computer only follows your logic). If a statement is true, the computer acts on a subsequent instruction.

The comparison using the if statement is used to check whether a condition is true or false. The syntax to use it is:

if(Condition) Statement;

If the Condition is true, then the compiler would execute the Statement. The compiler ignores anything else:

 

If the statement to execute is (very) short, you can write it on the same line with the condition that is being checked.

Consider a program that is asking a user to answer Yes or No to a question such as "Are you ready to provide your credit card number?". A source file of such a program could look like this:

using System;

class NewProject{ static void Main() {

char Answer;string Ans;

// Request the availability of a credit card from the userConsole.Write("Are you ready to provide your credit card number(1=Yes/0=No)?

");Ans = Console.ReadLine();Answer = char.Parse(Ans);

// Since the user is ready, let's process the credit card transaction

116

Page 117: Introduction to C#

if(Answer == '1') Console.WriteLine("\nNow we will get your credit card information."); }

}

You can write the if condition and the statement on different lines; this makes your program easier to read. The above code could be written as follows:

using System;

class NewProject{

static void Main(){

char Answer;string Ans;

// Request the availability of a credit card from the userConsole.Write("Are you ready to provide your credit card

number(1=Yes/0=No)? ");Ans = Console.ReadLine();Answer = char.Parse(Ans);

// Since the user is ready, let's process the credit card transaction

if(Answer == '1')Console.WriteLine("\nNow we will get your credit card

information.");

}}

You can also write the statement on its own line if the statement is too long to fit on the same line with the condition.

Although the (simple) if statement is used to check one condition, it can lead to executing multiple dependent statements. If that is the case, enclose the group of statements between an opening curly bracket “{“ and a closing curly bracket “}”. Here is an example:

using System;

class NewProject{

static void Main(){

char Answer;string Ans, CreditCardNumber;

// Request the availability of a credit card from the userConsole.Write("Are you ready to provide your credit card

number(1=Yes/0=No)? ");Ans = Console.ReadLine();Answer = char.Parse(Ans);

// Since the user is ready, let's process the credit card transaction

if(Answer == '1')

117

Page 118: Introduction to C#

{Console.WriteLine("Now we will get your credit card

information.");Console.Write("Please enter your credit card number without

spaces: ");CreditCardNumber = Console.ReadLine();

}}

}

If you omit the brackets, only the statement that immediately follows the condition would be executed.

  Practical Learning: Using if

1. To state an if condition, change the program as follows: 

using System;

namespace MotorVehicleDivision{

class Exercise{

static void Main(){

string FirstName, LastName;string OrganDonorAnswer;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Application ---");

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();Console.Write("Are you willing to be an Organ

Donor(1=Yes/0=No)? ");OrganDonorAnswer = Console.ReadLine();

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Information ---");

Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);

Console.Write("Organ Donor? ");if( OrganDonorAnswer == "1" ) Console.WriteLine("Yes");

}}

}

2. Execute the application. Here is an example: 

118

Page 119: Introduction to C#

-=- Motor Vehicle Administration -=- --- Driver's License Application ---First Name: CatherineLast Name: WallingtonAre you willing to be an Organ Donor(1=Yes/0=No)? 1

-=- Motor Vehicle Administration -=- --- Driver's License Information ---Full Name: Catherine WallingtonOrgan Donor? Yes

3. Return to Visual C#

Otherwise: if…else

The if condition is used to check one possibility and ignore anything else. Usually, other conditions should be considered. In this case, you can use more than one if statement. For example, on a program that asks a user to answer Yes or No, although the positive answer is the most expected, it is important to offer an alternate statement in case the user provides another answer. Here is an example:

using System;

class NewProject{

static void Main(){

char Answer;string Ans;

// Request the availability of a credit card from the userConsole.Write("Do you consider yourself a hot-tempered

individual(y=Yes/n=No)? ");Ans = Console.ReadLine();Answer = char.Parse(Ans);

if( Answer == 'y' ) // First Condition{

Console.WriteLine("\nThis job involves a high level of self-control.");

Console.WriteLine("We will get back to you.\n");}if( Answer == 'n' ) // Second Condition

Console.Write("\nYou are hired!\n");}

}

Here is an example of running the program:

Do you consider yourself a hot-tempered individual(y=Yes/n=No)? y

This job involves a high level of self-control.We will get back to you.

Press any key to continue

The problem with the above program is that the second if is not an alternative to the first, it is just another condition that the program has to check and execute after executing the first. On that program,

119

Page 120: Introduction to C#

if the user provides y as the answer to the question, the compiler would execute the content of its statement and the compiler would execute the second if condition.

You can also ask the compiler to check a condition; if that condition is true, the compiler will execute the intended statement. Otherwise, the compiler would execute alternate statement. This is performed using the syntax:

if(Condition)    Statement1;else    Statement2;

The above program would better be written as:

using System;

class NewProject{

static void Main(){

char Answer;string Ans;

// Request the availability of a credit card from the userConsole.Write("Do you consider yourself a hot-tempered

individual(y=Yes/n=No)? ");Ans = Console.ReadLine();Answer = char.Parse(Ans);

if( Answer == 'y' ) // First Condition{

Console.WriteLine("\nThis job involves a high level of self-control.");

Console.WriteLine("We will get back to you.\n");}else // Second Condition

Console.Write("\nYou are hired!\n");}

}

 

 

120

Page 121: Introduction to C#

Practical Learning: Using if...else

1. Make the following changes to the file: 

using System;

namespace MotorVehicleDivision{

class Exercise{

static void Main(){

string FirstName, LastName;string OrganDonorAnswer;char Gender;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Application ---");

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();Console.Write("Gender(F=Female/M=Male): ");Gender = char.Parse(Console.ReadLine());Console.Write("Are you willing to be an Organ

Donor(1=Yes/0=No)? ");OrganDonorAnswer = Console.ReadLine();

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Information ---");

Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);

Console.WriteLine("Gender: {0}", Gender);Console.Write("Organ Donor? ");if( OrganDonorAnswer == "1" )

Console.WriteLine("Yes");else if( OrganDonorAnswer == "0" )

Console.WriteLine("No");}

}}

2. Execute the application and test it twice, once with 1 to the question, then with 0 to the last question

3. Return to Visual C#

The Ternary Operator (?:)

The conditional operator behaves like a simple if…else statement. Its syntax is:

121

Page 122: Introduction to C#

Condition ? Statement1 : Statement2;

The compiler would first test the Condition. If the Condition is true, then it would execute Statement1, otherwise it would execute Statement2. When you request two numbers from the user and would like to compare them, the following program would do find out which one of both numbers is higher. The comparison is performed using the conditional operator:

using System;

class NewProject{

static void Main(){

int Number1, Number2, Maximum;string Num1, Num2;

Console.Write("Enter first numbers: ");Num1 = Console.ReadLine();Console.Write("Enter second numbers: ");Num2 = Console.ReadLine();

Number1 = int.Parse(Num1);Number2 = int.Parse(Num2);

Maximum = (Number1 < Number2) ? Number2 : Number1;

Console.Write("\nThe maximum of ");Console.Write(Number1);Console.Write(" and ");Console.Write(Number2);Console.Write(" is ");Console.WriteLine(Maximum);

Console.WriteLine();}

}

Here is an example of running the program:

Enter first numbers: 244Enter second numbers: 68

The maximum of 244 and 68 is 244Practical Learning: Using the Ternary Operator

122

Page 123: Introduction to C#

 

Formulating Expressions 

Counting and Looping 

While a Statement is True

The C# language provides a set of control statements that allows you to conditionally control data input and output. These controls are referred to as loops.

The while statement examines or evaluates a condition. The syntax of the while statement is:

while(Condition) Statement;

  

To execute this expression, the compiler first examines the Condition. If the Condition is true, then it executes the Statement. After executing the Statement, the Condition is checked again.

123

Page 124: Introduction to C#

AS LONG AS the Condition is true, it will keep executing the Statement. When or once the Condition becomes false, it exits the loop.

Here is an example:

int Number;

while( Number <= 12 ){

Console.Write("Number ");Console.WriteLine(Number);Number++;

}

To effectively execute a while condition, you should make sure you provide a mechanism for the compiler to use a get a reference value for the condition, variable, or expression being checked. This is sometimes in the form of a variable being initialized although it could be some other expression. Such a while condition could be illustrated as follows:

An example would be:

using System;

class NewProject{

static void Main(){

int Number = 0;

while( Number <= 12 ){

Console.Write("Number ");Console.WriteLine(Number);

124

Page 125: Introduction to C#

Number++;}

Console.WriteLine();}

}

This would produce:

Number 0Number 1Number 2Number 3Number 4Number 5Number 6Number 7Number 8Number 9Number 10Number 11Number 12

Press any key to continue

 

Practical Learning: Introducing Conditional Statements1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with DeptStore1 and specify the desired path in the Location

6. Click OK

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise

10. Click Open

11. In the empty file, type the following: 

using System;

enum TypeOfApplication{ NewDriversLicense = 1, UpgradeFromIDCard, TransferFromAnotherState};

125

Page 126: Introduction to C#

class Exercise{ static void Main() {

TypeOfApplication AppType; string FirstName, LastName;

string OrganDonorAnswer;char Sex;string Gender;char DLClass;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Application ---");

Console.WriteLine(" - Select the type of application -");Console.WriteLine("1 - Applying for a brand new Driver's

License");Console.WriteLine("2 - Applicant already had an ID Card and is

applying for a Driver's License");Console.WriteLine("3 - Applicant is transferring his/her

Driver's License from another state");Console.Write("Your Choice: ");int type = int.Parse(Console.ReadLine());

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();

Console.Write("Sex(F=Female/M=Male): ");Sex = char.Parse(Console.ReadLine());

Console.WriteLine(" - Driver's License Class -");Console.WriteLine("A - All Non-commercial vehicles except

motorcycles");Console.WriteLine("B - Non-commercial vehicles up to and

including 26,001/more lbs.");Console.WriteLine("C - Cars, pick-up trucks, non-commercial

vehicles 26,000 lbs.");Console.WriteLine("K - Mopeds");Console.WriteLine("M - Motorcycles");Console.Write("Your Choice: ");DLClass = char.Parse(Console.ReadLine());

Console.Write("Are you willing to be an Organ Donor(1=Yes/0=No)? ");

OrganDonorAnswer = Console.ReadLine();

if( Sex == 'f' ) Gender = "Female";else if( Sex == 'F' ) Gender = "Female";else if( Sex == 'm' ) Gender = "Male";else if( Sex == 'M' )

126

Page 127: Introduction to C#

Gender = "Male";else Gender = "Unknown";

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Information ---");

AppType = (TypeOfApplication)type;Console.Write("Type of Application: ");switch(AppType){case TypeOfApplication.NewDriversLicense: Console.WriteLine("New Driver's License"); break;

case TypeOfApplication.UpgradeFromIDCard: Console.WriteLine("Upgrade From Identity Card"); break;

case TypeOfApplication.TransferFromAnotherState: Console.WriteLine("Transfer From Another State"); break;default: Console.WriteLine("Not Specified"); break;}

Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);Console.WriteLine("Sex: {0}", Gender);

switch(DLClass){case 'a':case 'A': Console.WriteLine("Class: A"); break;case 'b':case 'B': Console.WriteLine("Class: B"); break;case 'c':case 'C': Console.WriteLine("Class: C"); break;case 'k':case 'K': Console.WriteLine("Class: K"); break;case 'm':case 'M': Console.WriteLine("Class: M"); break;default: Console.WriteLine("Class: Unknown"); break;}

Console.Write("Organ Donor? ");

127

Page 128: Introduction to C#

Console.WriteLine(OrganDonorAnswer == "1" ? "Yes" : (OrganDonorAnswer == "0" ? "No" : "Invalid Answer"));

return 0; }}

12. To execute the application, on the main menu, click Debug -> Start Without Debugging. Here is an example: 

-=- Motor Vehicle Administration -=- --- Driver's License Application --- - Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another stateYour Choice: 6First Name: HenryLast Name: DricksSex(F=Female/M=Male): P - Driver's License Class -A - All Non-commercial vehicles except motorcyclesB - Non-commercial vehicles up to and including 26,001/more lbs.C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.K - MopedsM - MotorcyclesYour Choice: GAre you willing to be an Organ Donor(1=Yes/0=No)? 4

-=- Motor Vehicle Administration -=- --- Driver's License Information ---Type of Application: Not SpecifiedFull Name: Henry DricksSex: UnknownClass: UnknownOrgan Donor? Invalid Answer

13. After using it, Return to Visual C#

The do...while Statement

The do…while statement uses the following syntax:

do Statement while (Condition); 

128

Page 129: Introduction to C#

  

The do…while condition executes a Statement first. After the first execution of the Statement, it examines the Condition. If the Condition is true, then it executes the Statement again. It will keep executing the Statement AS LONG AS the Condition is true. Once the Condition becomes false, the looping (the execution of the Statement) would stop.

If the Statement is a short one, such as made of one line, simply write it after the do keyword. Like the if and the while statements, the Condition being checked must be included between parentheses. The whole do…while statement must end with a semicolon.

Another version of the counting program seen previously would be:

using System;

class NewProject{

static void Main(){

int Number = 0;

do {Console.Write("Number ");Console.WriteLine(Number);Number++;

}while( Number <= 12 );

Console.WriteLine();}

}

If the Statement is long and should span more than one line, start it with an opening curly bracket and end it with a closing curly bracket.

The do…while statement can be used to insist on getting a specific value from the user. For example, since our ergonomic program would like the user to sit down for the subsequent exercise, you can modify your program to continue only once she is sitting down. Here is an example on how you would accomplish that:

129

Page 130: Introduction to C#

using System;

class NewProject{

static void Main(){

char SittingDown;string SitDown;

Console.Write("For the next exercise, you need to be sitting down\n");

do {Console.Write("Are you sitting down now(y/n)? ");SitDown = Console.ReadLine();SittingDown = char.Parse(SitDown);

} while( !(SittingDown == 'y') );

Console.WriteLine();}

}

Here is an example of running the program:

For the next exercise, you need to be sitting downAre you sitting down now(y/n)? tAre you sitting down now(y/n)? hAre you sitting down now(y/n)? GAre you sitting down now(y/n)? y

Practical Learning: Using do...While

1. Change the file as follows: 

using System;

enum TypeOfApplication{ NewDriversLicense = 1, UpgradeFromIDCard, TransferFromAnotherState};

class Exercise{ static void Main() {

TypeOfApplication AppType;int type;

string FirstName, LastName;string OrganDonorAnswer;char Sex;string Gender;char DLClass;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Application ---");

130

Page 131: Introduction to C#

do { Console.WriteLine(" - Select the type of application -");

Console.WriteLine("1 - Applying for a brand new Driver's License"); Console.WriteLine("2 - Applicant already had an ID Card and is applying for a

Driver's License"); Console.WriteLine("3 - Applicant is transferring his/her Driver's License from

another state"); Console.Write("Your Choice: "); type = int.Parse(Console.ReadLine()); Console.WriteLine("");} while(type > 3);

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();

Console.Write("\nSex(F=Female/M=Male): ");Sex = char.Parse(Console.ReadLine());

Console.WriteLine("\n - Driver's License Class -");Console.WriteLine("A - All Non-commercial vehicles except motorcycles");Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more lbs.");Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.");Console.WriteLine("K - Mopeds");Console.WriteLine("M - Motorcycles");Console.Write("Your Choice: ");DLClass = char.Parse(Console.ReadLine());

Console.Write("\nAre you willing to be an Organ Donor(1=Yes/0=No)? ");OrganDonorAnswer = Console.ReadLine();

if( Sex == 'f' ) Gender = "Female";else if( Sex == 'F' ) Gender = "Female";else if( Sex == 'm' ) Gender = "Male";else if( Sex == 'M' ) Gender = "Male";else Gender = "Unknown";

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Information ---");

AppType = (TypeOfApplication)type;Console.Write("Type of Application: ");switch(AppType){case TypeOfApplication.NewDriversLicense: Console.WriteLine("New Driver's License"); break;

case TypeOfApplication.UpgradeFromIDCard: Console.WriteLine("Upgrade From Identity Card");

131

Page 132: Introduction to C#

break; case TypeOfApplication.TransferFromAnotherState:

Console.WriteLine("Transfer From Another State"); break;default: Console.WriteLine("Not Specified"); break;}

Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);Console.WriteLine("Sex: {0}", Gender);

switch(DLClass){case 'a':case 'A': Console.WriteLine("Class: A"); break;case 'b':case 'B': Console.WriteLine("Class: B"); break;case 'c':case 'C': Console.WriteLine("Class: C"); break;case 'k':case 'K': Console.WriteLine("Class: K"); break;case 'm':case 'M': Console.WriteLine("Class: M"); break;default: Console.WriteLine("Class: Unknown"); break;}

Console.Write("Organ Donor? ");Console.WriteLine(OrganDonorAnswer == "1" ? "Yes" : (OrganDonorAnswer == "0" ? "No" :

"Invalid Answer"));

return 0; }}

2. Save, compile, and test it. Here is an example: 

-=- Motor Vehicle Administration -=- --- Driver's License Application --- - Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another

132

Page 133: Introduction to C#

stateYour Choice: 6

- Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another stateYour Choice: 8

- Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another stateYour Choice: 3

First Name: AlexandriaLast Name: McArthur

Sex(F=Female/M=Male): M

- Driver's License Class -A - All Non-commercial vehicles except motorcyclesB - Non-commercial vehicles up to and including 26,001/more lbs.C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.K - MopedsM - MotorcyclesYour Choice: C

Are you willing to be an Organ Donor(1=Yes/0=No)? 0

-=- Motor Vehicle Administration -=- --- Driver's License Information ---Type of Application: Transfer From Another StateFull Name: Alexandria McArthurSex: MaleClass: COrgan Donor? No

3. Return to Visual C#

The for Statement

The for statement is typically used to count a number of items. At its regular structure, it is divided in three parts. The first section specifies the starting point for the count. The second section sets the counting limit. The last section determines the counting frequency. The syntax of the for statement is:

for(Start; End; Frequency) Statement;

The Start expression is a variable assigned the starting value. This could be Count = 0;

The End expression sets the criteria for ending the counting. An example would be Count < 24;

133

Page 134: Introduction to C#

which means that the counting would continue as long as the Count variable is less than 24. When the count is about to rich 24, because in this case 24 is excluded, the counting would stop. To include the counting limit, use the <= or >= comparison operators depending on how you are counting.

The Frequency expression lets the compiler know how many numbers to add or subtract before continuing with the loop. This expression could be an increment operation such as ++Count.

Here is an example that applies the for statement:

using System;

class NewProject{

static void Main(){

for(int Count = 0; Count <= 12; Count++){

Console.Write("Number ");Console.WriteLine(Count);

}

Console.WriteLine();}

}

A variable declared as the counter of a for loop is available only in that for loop. This means that the scope of the counting variable is confined only to the for loop. This allows different for loops to use the same counter variable. Here is an example:

using System;

class NewProject{

static void Main(){

for(int Count = 0; Count <= 12; Count++){

Console.Write("Number ");Console.WriteLine(Count);

}

for(int Count = 10; Count >= 2; Count--){

Console.Write("Number ");Console.WriteLine(Count);

}

Console.WriteLine();}

}

Techniques of Writing Conditional Statements 

134

Page 135: Introduction to C#

Conditional Nesting

A condition can be created inside of another to write a more effective statement. This is referred to as nesting one condition inside of another. Almost any condition can be part of another and multiple conditions can be included inside of others.

As we have learned, different conditional statements are applied in specific circumstances. In some situations, they are interchangeable or one can be applied just like another, which becomes a matter of choice. Statements can be combined to render a better result with each playing an appropriate role.

Here is an example of an if condition nested inside of a do...while loop:

using System;

class NewProject{ static void Main() {

char SittingDown;string SitDown;

do { Console.Write("Are you sitting down now(y/n)? "); SitDown = Console.ReadLine(); SittingDown = char.Parse(SitDown);

if( SittingDown != 'y' )Console.WriteLine("Could you please sit down for the next

exercise? ");} while( !(SittingDown == 'y') );

Console.WriteLine(); }}

Here is an example of running the program:

Are you sitting down now(y/n)? nCould you please sit down for the next exercise?Are you sitting down now(y/n)? gCould you please sit down for the next exercise?Are you sitting down now(y/n)? y

One of the reasons you would need to nest conditions is because one would lead to another. Sometimes, before checking one condition, another primary condition would have to be met. The ergonomic program we have been simulating so far is asking the user whether she is sitting down. Once the user is sitting down, you would write an exercise she would perform. Depending on her strength, at a certain time, one user will be tired and want to stop while for the same amount of previous exercises, another user would like to continue. Before continuing with a subsequent exercise, you may want to check whether the user would like to continue. Of course, this would be easily done with:

using System;

class NewProject

135

Page 136: Introduction to C#

{ static void Main() {

char SittingDown;string SitDown;

do {Console.Write("Are you sitting down now(y/n)? ");SitDown = Console.ReadLine();SittingDown = char.Parse(SitDown);

if( SittingDown != 'y' )Console.WriteLine("Could you please sit down for the next

exercise? ");} while( !(SittingDown == 'y') );

Console.Write("Wonderful. Now we will continue today's exercise...");Console.WriteLine("\n...\nEnd of exercise");

char WantToContinue;

Console.Write("\nDo you want to continue(y=Yes/n=No)? ");string ToContinue = Console.ReadLine();WantToContinue = char.Parse(ToContinue);

Console.WriteLine(); }}

If the user answers No, you can stop the program. If she answers Yes, you would need to continue the program with another exercise. Because the user answered Yes, the subsequent exercise would be included in the previous condition because it does not apply for a user who wants to stop. In this case, one “if” could be inserted inside of another. Here is an example:

using System;

class NewProject{

static void Main(){

char SittingDown;string SitDown;

do {

Console.Write("Are you sitting down now(y/n)? ");SitDown = Console.ReadLine();SittingDown = char.Parse(SitDown);

if( SittingDown != 'y' )Console.WriteLine("Could you please sit down for the next

exercise? ");}while( SittingDown != 'y' );

Console.WriteLine("Wonderful. Now we will continue today's exercise...");

Console.WriteLine("\n...\nEnd of exercise\n");

136

Page 137: Introduction to C#

char WantToContinue;

Console.Write("\nDo you want to continue(1=Yes/0=No)? ");string ToContinue = Console.ReadLine();WantToContinue = char.Parse(ToContinue);

if(WantToContinue == '1'){

char LayOnBack;

Console.WriteLine("Good. For the next exercise, you should lay on your back");

Console.Write("Are you laying on your back(1=Yes/0=No)? ");string Lay = Console.ReadLine();LayOnBack = char.Parse(Lay);

if(LayOnBack == '1')Console.WriteLine("Great.\nNow we will start the next

exercise.");else

Console.WriteLine("\nWell, it looks like you are getting tired...");

}else

Console.WriteLine("We had enough today");

Console.WriteLine("We will stop the session now\nThanks.\n");}

}

Practical Learning: Nesting Conditions

1. Change the file as follows: 

using System;

enum TypeOfApplication{ NewDriversLicense = 1, UpgradeFromIDCard, TransferFromAnotherState};

class Exercise{ static void Main() {

. . . No Change

Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Application ---");

do { Console.WriteLine(" - Select the type of application -");

137

Page 138: Introduction to C#

Console.WriteLine("1 - Applying for a brand new Driver's License"); Console.WriteLine("2 - Applicant already had an ID Card and is applying for a Driver's

License"); Console.WriteLine("3 - Applicant is transferring his/her Driver's License from another

state"); Console.Write("Your Choice: "); type = int.Parse(Console.ReadLine());

if( type > 3 )Console.WriteLine("Please enter a valid number as 1, 2, or 3");

} while(type > 3);

. . . No Change

return 0; }}

2. Save, compile, and test the program

The break Statement

The break statement is used to stop a loop for any reason or condition the programmer considers fit. The break statement can be used in a while condition to stop an ongoing action. The syntax of the break statement is simply:

break;

Although made of only one word, the break statement is a complete statement. Therefore, it can (and should always) stay on its own line (this makes the program easy to read). The break statement applies to the most previous conditional statement to it, provided that previous statement is applicable. The break statement is typically used to handle the cases in a switch statement as we saw it in the previous lesson.

Besides the switch statement, the break statement can also be used in other conditional statements. For example, you can use a break statement inside of a for loop to interrupt its count. The following program would display letter from d to n but it is interrupted by a break when it encounters k:

using System;

class Exercise{

static void Main(){

for (char c = 'd'; c <= 'n'; c++) {

if (c == 'k') break;

Console.WriteLine(c);}

Console.WriteLine();return 0;

138

Page 139: Introduction to C#

}}

This would produce:

defghij

The break statement can also be used in a do…while or a for loop the same way. 

 

The continue Statement

The continue statement uses the following formula:

continue;

When processing a loop, if the statement finds a false value, you can use the continue statement inside of a while, do…while or a for conditional statements to ignore the subsequent statement or to jump from a false Boolean value to the subsequent valid value, unlike the break statement that would exit the loop. Like the break statement, the continue keyword applies to the most previous conditional statement and should stay on its own line.

The following programs asks the user to type 4 positive numbers and calculates the sum of the numbers by considering only the positive ones. If the user types a negative number, the program manages to ignore the numbers that do not fit in the specified category:

using System;

class Exercise{

static void Main(){

// Declare necessary variablesdouble Number, Sum = 0;string Nbr;

// Request 4 positive numbers from the userConsole.WriteLine("Type 4 positive numbers.");// Make sure the user types 4 positive numbersfor( int Count = 1; Count <= 4; Count++ ){

Console.Write("Number: ");Nbr = Console.ReadLine();Number = double.Parse(Nbr);

// If the number typed is not positive, ignore itif( Number < 0 )

continue;

// Add each number to the sum

139

Page 140: Introduction to C#

Sum += Number;}

// Display the sumConsole.WriteLine("Sum of the numbers you entered = {0}\n", Sum);return 0;

}}

This would produce:

Type 4 positive numbers.Number: 24.55Number: 826.08Number: 1450.85Number: 262.58Sum of the numbers you entered = 2564.06

The goto Statement

The goto statement allows a program execution to jump to another section of the function in which it is being used. In order to use the goto statement, insert a name on a particular section of your function so you can refer to that name. The name, also called a label, is made of one word and follows the rules we have learned about C# names (the name can be anything), then followed by a colon.

The following program uses a for loop to count from 0 to 12, but when it encounters 5, it jumps to a designated section of the program:

using System;

class NewProject{

static void Main(){

for(int Count = 0; Count <= 12; ++Count){

Console.WriteLine("Count {0}", Count);

if( Count == 5 )goto MamaMia;

}

MamaMia:Console.WriteLine("\nStopped at 5");

Console.WriteLine();}

}

This would produce:

Count 0Count 1Count 2Count 3Count 4

140

Page 141: Introduction to C#

Count 5

Stopped at 5

Logical Operations on Statements 

Introduction

The conditional statements we have used so far were applied to single situations. You can combine statements using techniques of logical operations to create more complex and complete expressions. One way to do this is by making sure that two conditions are met for the whole expression to be true. On the other hand, one or the other of two conditions can produce a true condition, as long as one of them is true. This is done with logical conjunction or disjunction.

In the beginning of the previous lesson, we described ways to write clear, simple, and logical statements. The techniques are even more important when combining statements because, once again, you should be able to make a decision based on a short, truthful, and clear statement.

Logical Conjunction: AND

One of the operations used in combining two statements consists of validating an expression made of two parts only if both parts of the statement are true. Imagine that a sport camp states on an application form that an interested candidate must be "a man at least 18 years old". This condition can be divided in two statements as follows:

1. A - The applicant is a man

2. B - The applicant is at least 18 years old

The program to process an application can look like this:

using System;

class SportCamp{

static void Main(){

string FullName;int Gender;int Age;

Console.WriteLine("Enter the following pieces of information");Console.Write("Full Name: ");FullName = Console.ReadLine();Console.Write("Gender(1=Male/0=Female): ");Gender = int.Parse(Console.ReadLine());Console.Write("Age: ");Age = int.Parse(Console.ReadLine());

Console.WriteLine();}

141

Page 142: Introduction to C#

}

Here is an example of running the program:

Enter the following pieces of informationFull Name: Ernestine MonayGender(1=Male/0=Female): 6Age: 16

Every time a new person has filled an application, his or her information would be evaluated according to the above statements:

If the applicant is a man, the first statement, represented here as A would be true

If the applicant is 18 or older, the second statement will be rendered true.

If we evaluate these statement separately, we can tabulate them as follows:

A

 

B

 

Suppose an applicant answers the first question as being a female, but the statement on the application said that an applicant must be a male. Once this statement produces a false result. We don't care how old the applicant is, the whole statement would be false. This would produce the following table:

A B Statement

False Don't Care False

Here is an example of processing and validating the first question:

using System;

class SportCamp{

static void Main(){

string FullName;int Gender;int Age;

Console.WriteLine("Enter the following pieces of information");Console.Write("Full Name: ");FullName = Console.ReadLine();Console.Write("Gender(1=Male/0=Female): ");Gender = int.Parse(Console.ReadLine());

if( Gender == 1 ){

// If the applicant is a man, then we can continueConsole.Write("Age: ");Age = int.Parse(Console.ReadLine());

}else // If the applicant is not a man, let the applicant know

Console.WriteLine("You don't qualify");

Console.WriteLine();

142

Page 143: Introduction to C#

}}

Here is an example of running the program:

Enter the following pieces of informationFull Name: Samuel GolferGender(1=Male/0=Female): 1Age: 16

Here is another example of running the same program:

Enter the following pieces of informationFull Name: Germain GobbGender(1=Male/0=Female): 6You don't qualify

If an application answers the first question and indicates that he is a man, then we can process the second question. If the applicant indicates that he is younger than 18, the second statement is false. Consequently, the applicant doesn't qualify. The process this second question, we can nest a condition and change the program as follows:

using System;

class SportCamp{

static void Main(){

string FullName;int Gender;int Age;

Console.WriteLine("Enter the following pieces of information");Console.Write("Full Name: ");FullName = Console.ReadLine();Console.Write("Gender(1=Male/0=Female): ");Gender = int.Parse(Console.ReadLine());

if( Gender == 1 ){

// If the applicant is a man, then we can continueConsole.Write("Age: ");Age = int.Parse(Console.ReadLine());

if( Age < 18 )Console.WriteLine("You are not old enough!");

}else // If the applicant is not a man, let the applicant know

Console.WriteLine("You don't qualify");

Console.WriteLine();}

}

Here is an example of running the program:

Enter the following pieces of informationFull Name: Joseph Davis

143

Page 144: Introduction to C#

Gender(1=Male/0=Female): 5You don't qualify

Here is another example of running the same program:

Enter the following pieces of informationFull Name: Thomas LemmGender(1=Male/0=Female): 1Age: 16You are not old enough!

This would produce the following table:

A B Statement

Don't Care False False

In the same way, if the applicant answers to be a female and younger than 18, the whole statement is still false. This would produce the following table:

A B Statement

False False False

As a consequence, an applicant qualifies only if he is a male and at least 18. The program that evaluates this c an be written as follows:

using System;

class SportCamp{

static void Main(){

string FullName;int Gender;int Age;

Console.WriteLine("Enter the following pieces of information");Console.Write("Full Name: ");FullName = Console.ReadLine();Console.Write("Gender(1=Male/0=Female): ");Gender = int.Parse(Console.ReadLine());

if( Gender == 1 ){

// If the applicant is a man, then we can continueConsole.Write("Age: ");Age = int.Parse(Console.ReadLine());

if( Age < 18 )Console.WriteLine("You are not old enough!");

elseConsole.WriteLine("Your application is valid");

}else // If the applicant is not a man, let the applicant know

Console.WriteLine("You don't qualify");

Console.WriteLine();

144

Page 145: Introduction to C#

}}

This shows that an applicant qualifies only if both statements are true.

In the above section, to evaluate related statements, we proceeded by nesting them, which made our program complete but longer. To process this type of related statements, you can combine them using the Boolean AND operator. Based on this, a statement such as "The applicant must be a man at least 18 years old" can be segmented in two statements joined as follows:

"The applicant is a man" AND "The applicant is at least 18 years old"

In C#, the logical conjunction is performed using the && operator. A combination of A AND B can be represented as follows:

A

 

B

 

A && B

 

The expression that evaluates this combined statement can be written as follows:

using System;

class SportCamp{

static void Main(){

string FullName;int Gender;int Age;

Console.WriteLine("Enter the following pieces of information");Console.Write("Full Name: ");FullName = Console.ReadLine();Console.Write("Gender(1=Male/0=Female): ");Gender = int.Parse(Console.ReadLine());

Console.Write("Age: ");Age = int.Parse(Console.ReadLine());

if( Gender == 1 && Age >= 18 )Console.WriteLine("Your application is valid");

elseConsole.WriteLine("You don't qualify");

Console.WriteLine();}

}

Based on the rules of operator, this logical operation will work. The compiler will evaluate the "Gender == 1" condition first, then it will evaluate the "Age >= 18" condition, then it will combine the first to the second. Sometime this expression can appear confusing to read. If you want, you can include each part of the combination in its own parentheses. Here is an example:

using System;

class SportCamp

145

Page 146: Introduction to C#

{static void Main(){

string FullName;int Gender;int Age;

Console.WriteLine("Enter the following pieces of information");Console.Write("Full Name: ");FullName = Console.ReadLine();Console.Write("Gender(1=Male/0=Female): ");Gender = int.Parse(Console.ReadLine());

Console.Write("Age: ");Age = int.Parse(Console.ReadLine());

if( (Gender == 1) && (Age >= 18) )Console.WriteLine("Your application is valid");

elseConsole.WriteLine("You don't qualify");

Console.WriteLine();}

}

This makes the program easier to read.

If the current applicant is a woman, our first statement, represented as A, is false

If the same applicant is younger than 18, our second statement, represented as B, is false

If both statements are false, the combined statement, represented as A&&B, or C, is false.

This can be represented in a truth table as follows:

A B A && B

False False False

Based on this table, here is an example of running the program:

Enter the following pieces of informationFull Name: Germain AsseGender(1=Male/0=Female): 0Age: 16You don't qualify

If the current applicant is a woman, our first statement is false

If the same applicant is at least 18 years old, our second statement is true

If the first statement is false and the second is true, the combined statement is false

This can be represented in a truth table as follows:

A B A && B

False True False

Based on this table, here is an example of running the program:

146

Page 147: Introduction to C#

Enter the following pieces of informationFull Name: Germain AsseGender(1=Male/0=Female): 0Age: 24You don't qualify

In the same way

If the current applicant is a man, our first statement is true

If the same applicant is at least 18 years old, our second statement is false

If the first statement is true and the second is false, the combined statement is false

This can be represented in a truth table as follows:

A B A && B

True False False

Based on this table, here is an example of running the program:

Enter the following pieces of informationFull Name: Germain AsseGender(1=Male/0=Female): 1Age: 14You don't qualify

If the current applicant is a man, our first statement is true

If the same applicant is at least 18 years old, our second statement is true

If both statements are true, the combined statement is true.

This results in:

A B A && B

True True True

Based on this table, here is an example of running the program:

Enter the following pieces of informationFull Name: Germain AsseGender(1=Male/0=Female): 1Age: 22Your application is valid

The above tables can be combined into one as follows:

A B A && B

False False False

False True False

True False False

True True True

 

 

147

Page 148: Introduction to C#

Practical Learning: Using the Logical AND Operator

1. To use logical conjunction, change the program as follows: 

using System;

enum TypeOfApplication{

NewDriversLicense = 1,UpgradeFromIDCard,TransferFromAnotherState

};

class Exercise{

static void Main(){

TypeOfApplication AppType;int type;string FirstName, LastName;string OrganDonorAnswer;char Sex;string Gender;char DLClass;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Application ---");

do {

Console.WriteLine(" - Select the type of application -");Console.WriteLine("1 - Applying for a brand new Driver's License");Console.WriteLine("2 - Applicant already had an ID Card and is applying for a

Driver's License");Console.WriteLine("3 - Applicant is transferring his/her Driver's License

from another state");Console.Write("Your Choice: ");type = int.Parse(Console.ReadLine());

if( type > 3 )Console.WriteLine("Please enter a valid number as 1, 2, or 3");

Console.WriteLine("");} while(type > 3);

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();

do {

Console.Write("Sex(F=Female/M=Male): ");Sex = char.Parse(Console.ReadLine());if( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') )

148

Page 149: Introduction to C#

Console.WriteLine("\nInvalid Answer - Try Again");} while( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') );

if( Sex == 'f' )Gender = "Female";

else if( Sex == 'F' )Gender = "Female";

else if( Sex == 'm' )Gender = "Male";

else if( Sex == 'M' )Gender = "Male";

elseGender = "Unknown";

Console.WriteLine(" - Driver's License Class -");Console.WriteLine("A - All Non-commercial vehicles except motorcycles");Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more

lbs.");Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.");Console.WriteLine("K - Mopeds");Console.WriteLine("M - Motorcycles");Console.Write("Your Choice: ");DLClass = char.Parse(Console.ReadLine());

do {

Console.Write("Are you willing to be an Organ Donor(y=Yes/n=No)? ");OrganDonorAnswer = Console.ReadLine();if( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") &&

(OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") )Console.WriteLine("\nInvalid Answer");

} while( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") && (OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") );

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Information ---");

AppType = (TypeOfApplication)type;Console.Write("Type of Application: ");switch(AppType){

case TypeOfApplication.NewDriversLicense:Console.WriteLine("New Driver's License");break;

case TypeOfApplication.UpgradeFromIDCard:Console.WriteLine("Upgrade From Identity Card");break;

case TypeOfApplication.TransferFromAnotherState:Console.WriteLine("Transfer From Another State");break;

default:Console.WriteLine("Not Specified");break;

}

Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);

149

Page 150: Introduction to C#

Console.WriteLine("Sex: {0}", Gender);

switch(DLClass){

case 'a':case 'A':

Console.WriteLine("Class: A");break;

case 'b':case 'B':

Console.WriteLine("Class: B");break;

case 'c':case 'C':

Console.WriteLine("Class: C");break;

case 'k':case 'K':

Console.WriteLine("Class: K");break;

case 'm':case 'M':

Console.WriteLine("Class: M");break;

default:Console.WriteLine("Class: Unknown");break;

}

Console.Write("Organ Donor? ");Console.WriteLine(OrganDonorAnswer == "1" ? "Yes" : (OrganDonorAnswer == "0" ? "No" :

"Invalid Answer"));

return 0;}

}

2. Save, compile, and test the program. Here is an example: 

-=- Motor Vehicle Administration -=- --- Driver's License Application --- - Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another stateYour Choice: 6Please enter a valid number as 1, 2, or 3

- Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another state

150

Page 151: Introduction to C#

Your Choice: 3

First Name: LarryLast Name: EdmonstonSex(F=Female/M=Male): g

Invalid Answer - Try AgainSex(F=Female/M=Male): m - Driver's License Class -A - All Non-commercial vehicles except motorcyclesB - Non-commercial vehicles up to and including 26,001/more lbs.C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.K - MopedsM - MotorcyclesYour Choice: bAre you willing to be an Organ Donor(1=Yes/0=No)? 8

Invalid AnswerAre you willing to be an Organ Donor(1=Yes/0=No)? 1

-=- Motor Vehicle Administration -=- --- Driver's License Information ---Type of Application: Transfer From Another StateFull Name: Larry EdmonstonSex: MaleClass: BOrgan Donor? YesPress any key to continue

3. Return to Visual C#

Logical Disjunction: OR

Imagine you are looking for a job and you come to an ad that states, "Programmer needed, an applicant should be a Microsoft Certified Application Developer (MCAD). If you are not an MCAD, you must have at least a bachelor's degree". This announcement can be resumed to the following statement: "The applicant must either be an MCAD or must have a bachelor's degree". It can be further segmented in two sub-statements as follows:

The applicant is an MCAD

The application has a bachelor's degree

The program to process an application can appear as follows:

using System;

class JobApplication{

static void Main(){

string FullName;int MCAD;int Degree;

Console.WriteLine("Enter the following pieces of information");

151

Page 152: Introduction to C#

Console.Write("Applicant's Name: ");FullName = Console.ReadLine();Console.Write("Are you an MCAD (1=Yes/0=No)? ");MCAD = int.Parse(Console.ReadLine());Console.Write("Do you hold a bachelor's degree (1=Yes/0=No)? ");Degree = int.Parse(Console.ReadLine());

Console.WriteLine();}

}

Here is an example of running the program:

Enter the following pieces of informationApplicant's Name: Paul BrowniesAre you an MCAD (1=Yes/0=No)? 4Do you hold a bachelor's degree (1=Yes/0=No)? 6

When a new applicant applies for a job, his or her information is evaluated:

If the applicant is an MCAD, the first statement, represented here as A, is true

If the applicant has a bachelor's degree, the second statement, represented as B, is true

If an applicant indicates in the first question that he or she is an MCAD, we wouldn't care about the second question. This resulting statement can be represented as follows:

A B Statement

True Don't Care True

Here is an example of processing and validating the first question:

using System;

class JobApplication{

static void Main(){

string FullName;int MCAD;int Degree;

Console.WriteLine("Enter the following pieces of information");Console.Write("Applicant's Name: ");FullName = Console.ReadLine();Console.Write("Are you an MCAD (1=Yes/0=No)? ");MCAD = int.Parse(Console.ReadLine());

if( MCAD == 1 )Console.WriteLine("Since you are an MCAD, you qualify");

Console.WriteLine();}

}

Here is an example of running the program:

Enter the following pieces of information

152

Page 153: Introduction to C#

Applicant's Name: Saul KemmelAre you an MCAD (1=Yes/0=No)? 4

Here is another example of running the same program:

Enter the following pieces of informationApplicant's Name: Saul KemmelAre you an MCAD (1=Yes/0=No)? 1Since you are an MCAD, you qualify

If the answer to the first question indicates that an applicant is not an MCAD, then we would be interested in the answer to the second question. If the second answers shows that the applicant has a bachelor's degree, then he or she qualifies. In fact in this case, the answer to the first question would not be important:

A B Statement

Don't Care True True

The second question can be addressed as follows:

using System;

class JobApplication{

static void Main(){

string FullName;int MCAD;int Degree;

Console.WriteLine("Enter the following pieces of information");Console.Write("Applicant's Name: ");FullName = Console.ReadLine();Console.Write("Are you an MCAD (1=Yes/0=No)? ");MCAD = int.Parse(Console.ReadLine());Console.Write("Do you hold a bachelor's degree (1=Yes/0=No)? ");Degree = int.Parse(Console.ReadLine());

Console.WriteLine();

if( MCAD == 1 )Console.WriteLine("Since you are an MCAD, you qualify");

if( Degree == 1 )Console.WriteLine("Since you have the necessary degree, you

qualify");

Console.WriteLine();}

}

Here is an example of running the program:

Enter the following pieces of informationApplicant's Name: Annie BaughAre you an MCAD (1=Yes/0=No)? 8Do you hold a bachelor's degree (1=Yes/0=No)? 1

153

Page 154: Introduction to C#

Since you have the necessary degree, you qualify

In the same way, if an applicant is an MCAD and holds a bachelor's degree, he or she still qualifies. This produce the following table:

A B Statement

True True True

Based on this, here is an example of running the program:

Enter the following pieces of informationApplicant's Name: Jamie KonloAre you an MCAD (1=Yes/0=No)? 1Do you hold a bachelor's degree (1=Yes/0=No)? 1

Since you are an MCAD, you qualifySince you have the necessary degree, you qualify

Therefore, if an application either is an MCAD or has a degree or fulfills both criteria, he or she qualifies. If a candidate neither is an MCAD nor has the degree, he or she cannot have the job. This can be resumed as follows:

A B Statement

False False False

Writing both statements separately can appear redundant. Instead, if you are evaluating two statements but the truthfulness of only one is enough to render both as true, you can perform a Boolean disjunction operation. This is also called an OR operator. Using this operator, our two statements can be combined as "The applicant is an MCAD" OR "The applicant has a bachelor's degree". 

In C#, the logical disjunction is performed using the || operator. A combination of A || B can be represented as follows:

A B A || B

True False True

False True True

True True True

False False False

The expression that evaluates this combined statement can be written as follows:

using System;

class JobApplication{

static void Main(){

string FullName;int MCAD;int Degree;

Console.WriteLine("Enter the following pieces of information");

154

Page 155: Introduction to C#

Console.Write("Applicant's Name: ");FullName = Console.ReadLine();Console.Write("Are you an MCAD (1=Yes/0=No)? ");MCAD = int.Parse(Console.ReadLine());Console.Write("Do you hold a bachelor's degree (1=Yes/0=No)? ");Degree = int.Parse(Console.ReadLine());

Console.WriteLine();

if( MCAD == 1 || Degree == 1 )Console.WriteLine("Since you are an MCAD OR you have the

necessary degree, you qualify");else

Console.WriteLine("We will get back to you");

Console.WriteLine();}

}

Once again, based on the rules of operators precedence, the "MCAD == 1 || Degree == 1" expression will work fine. If it appears difficult to read, you can include each part in its parentheses as (MCAD == 1) || (Degree == 1).

Here is an example of running the program:

Enter the following pieces of informationApplicant's Name: Sally KirkfieldAre you an MCAD (1=Yes/0=No)? 1Do you hold a bachelor's degree (1=Yes/0=No)? 8

Since you are an MCAD OR you have the necessary degree, you qualify

Here is another example of running the same program:

Enter the following pieces of informationApplicant's Name: Leslie StanislawAre you an MCAD (1=Yes/0=No)? 0Do you hold a bachelor's degree (1=Yes/0=No)? 0

We will get back to you

Practical Learning: Using the Logical OR Operator

1. To write logical disjunctions, change the program as follows: 

using System;

enum TypeOfApplication{

NewDriversLicense = 1,UpgradeFromIDCard,TransferFromAnotherState

};

class Exercise{

155

Page 156: Introduction to C#

static void Main(){

TypeOfApplication AppType;int type;string FirstName, LastName;string OrganDonorAnswer;char Sex;string Gender = null;char DLClass;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Application ---");

do {

Console.WriteLine(" - Select the type of application -");Console.WriteLine("1 - Applying for a brand new Driver's License");

Console.WriteLine("2 - Applicant already had an ID Card and is applying for a Driver's License");

Console.WriteLine("3 - Applicant is transferring his/her Driver's License from another state");

Console.Write("Your Choice: ");type = int.Parse(Console.ReadLine());

if( (type < 1) || (type > 3) )Console.WriteLine("\nPlease enter a valid number as 1, 2, or 3");

} while( (type < 1) || (type > 3) );

Console.WriteLine();

Console.Write("First Name: ");FirstName = Console.ReadLine();Console.Write("Last Name: ");LastName = Console.ReadLine();

do {

Console.Write("Sex(F=Female/M=Male): ");Sex = char.Parse(Console.ReadLine());if( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') )

Console.WriteLine("\nInvalid Answer - Try Again");} while( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') );

if( (Sex == 'f') || (Sex == 'F') )Gender = "Female";

else if( (Sex == 'm') || (Sex == 'M') )Gender = "Male";

Console.WriteLine();

Console.WriteLine(" - Driver's License Class -");Console.WriteLine("A - All Non-commercial vehicles except motorcycles");Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more

lbs.");Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.");Console.WriteLine("K - Mopeds");

156

Page 157: Introduction to C#

Console.WriteLine("M - Motorcycles");Console.Write("Your Choice: ");DLClass = char.Parse(Console.ReadLine());

Console.WriteLine();

do {

Console.Write("Are you willing to be an Organ Donor(y=Yes/n=No)? ");OrganDonorAnswer = Console.ReadLine();if( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") &&

(OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") )Console.WriteLine("\nInvalid Answer");

} while( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") && (OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") );

Console.WriteLine("\n=======================================");Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Information ---");

AppType = (TypeOfApplication)type;Console.Write("Type of Application: ");switch(AppType){

case TypeOfApplication.NewDriversLicense:Console.WriteLine("New Driver's License");break;

case TypeOfApplication.UpgradeFromIDCard:Console.WriteLine("Upgrade From Identity Card");break;

case TypeOfApplication.TransferFromAnotherState:Console.WriteLine("Transfer From Another State");break;

default:Console.WriteLine("Not Specified");break;

}

Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);Console.WriteLine("Sex: {0}", Gender);

switch(DLClass){

case 'a':case 'A':

Console.WriteLine("Class: A");break;

case 'b':case 'B':

Console.WriteLine("Class: B");break;

case 'c':case 'C':

Console.WriteLine("Class: C");break;

case 'k':

157

Page 158: Introduction to C#

case 'K':Console.WriteLine("Class: K");break;

case 'm':case 'M':

Console.WriteLine("Class: M");break;

default:Console.WriteLine("Class: Unknown");break;

}

Console.Write("Organ Donor? ");if( (OrganDonorAnswer == "y") || (OrganDonorAnswer == "Y") )

Console.WriteLine("Yes");else

Console.WriteLine("No");Console.WriteLine("=======================================");

Console.WriteLine();return 0;

}}

2. Save the file. Compile and test it. Here is an example: 

-=- Motor Vehicle Administration -=- --- Driver's License Application --- - Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another stateYour Choice: 0

Please enter a valid number as 1, 2, or 3 - Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another stateYour Choice: 4

Please enter a valid number as 1, 2, or 3 - Select the type of application -1 - Applying for a brand new Driver's License2 - Applicant already had an ID Card and is applying for a Driver's License3 - Applicant is transferring his/her Driver's License from another stateYour Choice: 1

First Name: JocelyneLast Name: Aladiere

158

Page 159: Introduction to C#

Sex(F=Female/M=Male): D

Invalid Answer - Try AgainSex(F=Female/M=Male): g

Invalid Answer - Try AgainSex(F=Female/M=Male): f - Driver's License Class -A - All Non-commercial vehicles except motorcyclesB - Non-commercial vehicles up to and including 26,001/more lbs.C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.K - MopedsM - MotorcyclesYour Choice: m

Are you willing to be an Organ Donor(y=Yes/n=No)? d

Invalid AnswerAre you willing to be an Organ Donor(y=Yes/n=No)? p

Invalid AnswerAre you willing to be an Organ Donor(y=Yes/n=No)? Y

======================================= -=- Motor Vehicle Administration -=- --- Driver's License Information ---Type of Application: New Driver's LicenseFull Name: Jocelyne AladiereSex: FemaleClass: MOrgan Donor? Yes=======================================

3. Close

 

Previous Copyright © 2004-2005 FunctionX, Inc. Next

159

Page 160: Introduction to C#

 

The Members of the Main Class 

Introduction to Members 

Introduction to Methods

One of the purposes of writing a program is to perform assignments. An assignment can also be called a function. The C# doesn't have the same notion of a function as did traditional and previous languages like C/C++, Pascal, etc. Instead, in C#, a function must belong to a class. Therefore, in C#, a function is a section whose job is to take care of an operation that would complement a class. Because a function in this case belongs to a particular class, the function is also called a method. From now on, the name method will be used for a function.

  C++ Note

In C++, a function can be declared anywhere, making it distinct from a method. In C#, a function can be declared only inside of, or as a member of, a class

Like everything else, a method must have a name. We will follow the same rules and conventions we defined for all names. An example of a method's name would be Welcome. To distinguish a method from a variable, it is always followed by parentheses, sometimes empty. Since a method is used to perform an assignment, its job is included between an opening curly bracket "{" and a closing curly bracket "}". Here is an example:

using System;

class Exercise{

Welcome() { }

static void Main(){

Console.WriteLine();}

}

If the assignment performed by a method is long, you can use many lines to define its behavior. For this reason, each bracket can be typed on its own line.

After a method has performed its assignment, it may provide a result. The result of a method is called a return value. Each return value follows a particular type based on one of the data types

160

Page 161: Introduction to C#

we reviewed on variables. Any time you create a method, the compiler needs to know the type of value that the method would return. The type of value that a method would return is specified on the left side of the method's name.

Some, even many, of the methods used in your programs will not return a value after they have performed an assignment. When a method doesn't return a value, it is considered void. The return type of such a method is the void keyword. Here is an example:

using System;

class Exercise{

void Welcome(){}

static void Main(){

Console.WriteLine();}

}

In the exercises of this lesson, we will perform calculations for a cylinder with circular base:

Figure Areas Volume

Base Area = R * R * PiLateral Area = 2 * Pi * R * HTotal Area = 2PiRH + 2PiR2      

PiR2H

 

Practical Learning: Introducing Global Members1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with Geometry1 and specify the desired path in the Location

6. Click OK

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise

161

Page 162: Introduction to C#

10. Click Open

11. In the empty file, type the following: 

using System;

namespace GeometricFormulas{

class Cylinder{

static void Main(){

Console.WriteLine();}

}}

12. Save the file

 

Static Methods

Unlike some other languages like C/C++, Pascal, Visual Basic, etc, but like Java, C# doesn't have the notion of global function: every method must belong to a class. For this reason, every program uses at least one class and we call this primary class, the main class. Because of this feature of the language, it imposes some rules on the way methods can be called. If you create a method in the main class of your project, you should indicate that the method will always belong to this main class. The method must be created as static.

To define a method as static, type the static keyword to its left. Here is an example:

using System;

class Exercise{

static void Welcome(){}

static void Main(){

Console.WriteLine();}

}

As we have used the Main() function so far, the behavior of a method is defined between its delimiting curly brackets. The section inside the curly brackets is called the body of a method. In the body of the method, you can simply display a sentence. Here is an example:

using System;

class Exercise{

static void Welcome(){

162

Page 163: Introduction to C#

Console.WriteLine("Welcome to the Wonderful World of C#");}

static void Main(){

Console.WriteLine();}

}

In the same way, you can use a method to perform any other assignment. After creating a method, you can use it where needed. Using a method is also referred to as calling it. If you create a simple method like the above Welcome(), to call it, type its name followed by parentheses and ending with a semi-colon. Here is an example:

using System;

class Exercise{

static void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C#");}

static void Main(){

Welcome();

Console.WriteLine();}

}

Practical Learning: Creating a Static Method

1. To define a static method, change the file as follows: 

using System;

class Cylinder{

static void ProcessCylinder(){

Console.WriteLine("Cylinder Characteristics");}

static void Main(){

ProcessCylinder();

Console.WriteLine();}

}

2. To execute the application, on the main menu, click Debug -> Start Without Debugging

3. Return to Visual C#

163

Page 164: Introduction to C#

Local Variables

In the body of a method, you can also declare variables that would be used internally. A variable declared in the body is referred to as a local variable. It cannot be accessed outside of the method it belongs to. After declaring a local variable, it is made available to the method and you can use it as you see fit, for example, you can assign it a value prior to using it.

Practical Learning: Using a Method's Local Variables

1. To declare and use local variables of a method, change the static method as follows: 

using System;

namespace GeometricFormulas{

class Cylinder{

static void ProcessCylinder(){

double Radius, Height;double BaseArea, LateralArea, TotalArea;double Volume;

Console.WriteLine("Enter the dimensions of the cylinder");

Console.Write("Radius: ");Radius = double.Parse(Console.ReadLine());Console.Write("Height: ");Height = double.Parse(Console.ReadLine());

BaseArea = Radius * Radius * Math.PI;LateralArea = 2 * Math.PI * Radius * Height;TotalArea = 2 * Math.PI * Radius * (Height +

Radius);Volume = Math.PI * Radius * Radius *

Height;

Console.WriteLine("\nCylinder Characteristics");Console.WriteLine("Radius: {0}", Radius);Console.WriteLine("Height: {0}", Height);Console.WriteLine("Base: {0:F}", BaseArea);Console.WriteLine("Lateral: {0:F}",

LateralArea);Console.WriteLine("Total: {0:F}", TotalArea);Console.WriteLine("Volume: {0:F}", Volume);

}

static void Main(){

ProcessCylinder();

Console.WriteLine();}

164

Page 165: Introduction to C#

}}

2. Execute the application. Here is an example: 

Enter the dimensions of the cylinderRadius: 38.64Height: 22.48

Cylinder CharacteristicsRadius: 38.64Height: 22.48Base: 4690.55Lateral: 5457.75Total: 14838.85Volume: 105443.65

3. Return to Visual C#

 

A Method that Returns a Value 

If a method has carried an assignment and must make its result available to other methods or other classes, the method must return a value and cannot be void. To declare a method that returns a value, provide its return type to the left of its name. Here is an example:

using System;

class Exercise{

static double Operation(){}

static void Main(){}

}

After a method has performed its assignment, it must clearly demonstrate that it is returning a value. To do this, you use the return keyword followed by the value that the method is returning. The value returned must be of the same type specified as the return type of the method. Here is an example:

using System;

class Exercise{

static double Operation(){

return 24.55;}

static void Main(){

165

Page 166: Introduction to C#

}}

A method can also return an expression, provided the expression produces a value that is conform to the return type. Here is an example:

using System;

class Exercise{

static double Operation(){

return 24.55 * 4.16;}

static void Main(){}

}

When a method returns a value, the compiler considers such a method as if it were a regular value. This means that you can use Console.Write() or Console.WriteLine() to display its value. To do this, simply type the name of the method and its parentheses in the Console.Write() of the Console.WriteLine() methods' parentheses. Here is an example:

using System;

class Exercise{

static double Operation(){

return 24.55;}

static void Main(){

Console.WriteLine(Operation());}

}

In the same way, a method returns a value can be assigned to a variable of the same type.

Practical Learning: Returning a Value From a Method 

1. To create methods that return values, change the program as follows: 

using System;

namespace GeometricFormulas{

class Cylinder{

static double GetRadius(){

double rad;

166

Page 167: Introduction to C#

Console.Write("Radius: ");rad = double.Parse(Console.ReadLine());

return rad;}

static double GetHeight(){

double h;

Console.Write("Height: ");h = double.Parse(Console.ReadLine());

return h;}

static void ProcessCylinder(){

double Radius, Height;double BaseArea, LateralArea, TotalArea;double Volume;

Console.WriteLine("Enter the dimensions of the cylinder");

Radius = GetRadius();Height = GetHeight();

BaseArea = Radius * Radius * Math.PI;LateralArea = 2 * Math.PI * Radius * Height;TotalArea = 2 * Math.PI * Radius * (Height +

Radius);Volume = Math.PI * Radius * Radius *

Height;

Console.WriteLine("\nCylinder Characteristics");

Console.WriteLine("Radius: {0}", Radius);Console.WriteLine("Height: {0}", Height);Console.WriteLine("Base: {0:F}", BaseArea);Console.WriteLine("Lateral: {0:F}",

LateralArea);Console.WriteLine("Total: {0:F}",

TotalArea);Console.WriteLine("Volume: {0:F}", Volume);

}

static void Main(){

ProcessCylinder();

Console.WriteLine();}

}}

2. Save, compile, and test the file. Here is an example of running the program:

167

Page 168: Introduction to C#

 

Enter the dimensions of the cylinderRadius: 52.08Height: 36.44

Cylinder CharacteristicsRadius: 52.08Height: 36.44Base: 8521.02Lateral: 11924.20Total: 28966.25Volume: 310506.14

3. Return to Visual C#

Pseudo-Global Variables

As mentioned with functions, C# doesn't have the notion of global variables (in C/C++, Visual Basic, Pascal, etc, a global variable is one that is declared outside of any class; such a variable is made available to any function, or even file, of the same program without being declared again where needed). Still, if you want to use the same variable in various methods of the main class, you can declare it outside of any method. The variable must be declared as static. That is, you must type the static keyword to its left when declaring it. Here is an example:

using System;

class Exercise{

static double Length;

static void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C#");}

static void Main(){

Welcome();

Console.WriteLine();}

}

After declaring such a variable, you can access from any method that belongs to the same class.

 

Practical Learning: Using Global Variables 

1. To declare and use global variables, change the file as follows: 

using System;

168

Page 169: Introduction to C#

namespace GeometricFormulas{

class Cylinder{

static double Radius;static double Height;static double BaseArea;static double LateralArea;static double TotalArea;static double Volume;

static double GetRadius(){

double rad;

Console.Write("Radius: ");rad = double.Parse(Console.ReadLine());

return rad;}

static double GetHeight(){

double h;

Console.Write("Height: ");h = double.Parse(Console.ReadLine());

return h;}

static void ProcessCylinder(){

Console.WriteLine("Enter the dimensions of the cylinder");

Radius = GetRadius();Height = GetHeight();

BaseArea = Radius * Radius * Math.PI;LateralArea = 2 * Math.PI * Radius * Height;TotalArea = 2 * Math.PI * Radius * (Height +

Radius);Volume = Math.PI * Radius * Radius *

Height;}

static void ShowCylinder(){

Console.WriteLine("\nCylinder Characteristics");Console.WriteLine("Radius: {0}", Radius);Console.WriteLine("Height: {0}", Height);Console.WriteLine("Base: {0:F}", BaseArea);Console.WriteLine("Lateral: {0:F}",

LateralArea);Console.WriteLine("Total: {0:F}", TotalArea);Console.WriteLine("Volume: {0:F}", Volume);

169

Page 170: Introduction to C#

}

static void Main(){

ProcessCylinder();ShowCylinder();

Console.WriteLine();}

}}

2. Save, compile, and test the file. Here is an example of running the program: 

Enter the dimensions of the cylinderRadius: 38.24Height: 32.58

Cylinder CharacteristicsRadius: 38.24Height: 32.58Base: 4593.94Lateral: 7827.96Total: 17015.85Volume: 149670.68

3. Return to Notepad

Methods and Conditional Statements 

Using Conditions in Functions

The use of methods in a program allows you to isolate assignments and confine them to appropriate entities. While the methods take care of specific requests, you can provide them with conditional statements to validate what these methods do. There are no set rules to the techniques involved. Everything depends on the tasks at hand. To make effective use of methods, you should be very familiar with different data types because you will need to return the right value.

Suppose you write an ergonomic program that needs to check different things including answers from the user in order to proceed. These various assignments can be given to methods that would simply hand the results to the Main() function that can, in turn, send these results to other functions for further processing. Here is an example:

using System;

public class NewProject{

static char GetPosition(){

char Position;string Pos;

170

Page 171: Introduction to C#

do {Console.Write("Are you sitting down now(y/n)? ");Pos = Console.ReadLine();;

Position = char.Parse(Pos);

if( Position != 'y' && Position != 'Y' && Position != 'n' && Position != 'N' )

Console.WriteLine("Invalid Answer\n");} while( Position != 'y' && Position != 'Y' &&

Position != 'n' && Position != 'N' );

return Position;}

static void Main(){

char Position;

Position = GetPosition();

if( Position == 'n' || Position == 'N' )Console.WriteLine("\nCould you please sit down for the next

exercise?\n");else

Console.WriteLine("\nWonderful!!!\n\n"); }

}

Here is an example of running the program:

Are you sitting down now(y/n)? VInvalid Answer

Are you sitting down now(y/n)? zInvalid Answer

Are you sitting down now(y/n)? n

Could you please sit down for the next exercise?

Methods don't have to return a value in order to be involved with conditional statements. In fact, both issues are fairly independent. This means that, void and non-void methods can manipulate values based on conditions internal to them.

 

Practical Learning: Using Conditional Statements With Methods

1. To include conditional statements in your methods, change the file as follows: 

using System;

namespace GeometricFormulas{

171

Page 172: Introduction to C#

class Cylinder{

static double Radius;static double Height;static double BaseArea;static double LateralArea;static double TotalArea;static double Volume;

static double GetRadius(){

double rad;

do {

Console.Write("Radius: ");rad = double.Parse(Console.ReadLine());if( rad < 0 )

Console.WriteLine("Please enter a positive number");

} while( rad < 0 );

return rad;}

static double GetHeight(){

double h;

do {

Console.Write("Height: ");h = double.Parse(Console.ReadLine());if( h < 0 )

Console.WriteLine("Please enter a positive number");

} while( h < 0 );

return h;}

static void ProcessCylinder(){

Console.WriteLine("Enter the dimensions of the cylinder");Radius = GetRadius();Height = GetHeight();

BaseArea = Radius * Radius * Math.PI;LateralArea = 2 * Math.PI * Radius * Height;TotalArea = 2 * Math.PI * Radius * (Height + Radius);Volume = Math.PI * Radius * Radius * Height;

}

static void ShowCylinder(){

Console.WriteLine("\nCylinder Characteristics");

172

Page 173: Introduction to C#

Console.WriteLine("Radius: {0}", Radius);Console.WriteLine("Height: {0}", Height);Console.WriteLine("Base: {0:F}", BaseArea);Console.WriteLine("Lateral: {0:F}", LateralArea);Console.WriteLine("Total: {0:F}", TotalArea);Console.WriteLine("Volume: {0:F}", Volume);

}

static void Main(){

ProcessCylinder();ShowCylinder();

Console.WriteLine();}

}}

2. Save, compile, and test the file. Here is an example of running the program: 

Enter the dimensions of the cylinderRadius: -24.55Please enter a positive numberRadius: 24.55Height: -18Please enter a positive numberHeight: 18.75

Cylinder CharacteristicsRadius: 24.55Height: 18.75Base: 1893.45Lateral: 2892.23Total: 6679.12Volume: 35502.11

3. Return to Notepad

Conditional Returns

A method defined other than void must always return a value. Sometimes, a method will perform some tasks whose results would lead to different results. A method can return only one value (this is true for this context, but there are ways to pass arguments so that a method can return more than one value) but you can make it render a result depending on a particular behavior. If a method is requesting an answer from the user, since the user can provide different answers, you can treat each result differently.

Imagine you write the following method:

using System;

public class NewProject{

static string GetPosition(){

173

Page 174: Introduction to C#

char Position;

Console.Write("Are you sitting down now(y/n)? ");string Pos = Console.ReadLine();Position = char.Parse(Pos);

if( Position == 'y' || Position == 'Y' )return "Yes";

else if( Position == 'n' || Position == 'N' )return "No";

}

static void Main(){

string Answer;

Answer = GetPosition();Console.Write("\nAnswer = ");Console.WriteLine(Answer);

}}

When running this program, you would receive an error. On paper, the function looks fine. If the user answers with y or Y, the function returns the string Yes. If the user answer with n or N, the function returns the string No. Unfortunately, this function has a problem: what if there is an answer that does not fit those we are expecting? The values that we have returned in the function conform only to the conditional statements and not to the function. Remember that in if(Condidion)Statement, the Statement executes only if the Condition is true. Here is what will happen. If the user answers y or Y, the function returns Yes and stops. If the user answers n or N, the function returns No, which also is a valid value. If the user enters another value (other than y, Y, n, or N), the execution of the function will not execute any of the returned statements and will not exit. This means that the execution will reach the closing curly bracket without encountering a return value. This would mean to the compiler that you wrote a non-void method that is supposed to return a value, but by the end of the method, it didn't return a value. That's why the compiler displays an error. The solution is to provide a return value so that, if the execution reaches the end of the function, it would still return something. Here is a solution to the problem:

using System;

public class NewProject{

static string GetPosition(){

char Position;

Console.Write("Are you sitting down now(y/n)? ");string Pos = Console.ReadLine();Position = char.Parse(Pos);

if( Position == 'y' || Position == 'Y' )return "Yes";

else if( Position == 'n' || Position == 'N' )return "No";

return "Invalid Answer";}

174

Page 175: Introduction to C#

static void Main(){

string Answer;

Answer = GetPosition();Console.Write("\nAnswer = ");Console.WriteLine(Answer);

}}

Here is an example of running the program:

Are you sitting down now(y/n)? g

Answer = Invalid Answer

void Methods and Conditional Returns

So far, we have seen that void methods don't return a value. Still, the return keyword can be used in a void method. It allows you to exit a method before the last line if you judge it necessary. Suppose that you ask a question in a method or validate a condition. Once the condition is valid, you may not see any reason to continue further. In this case, you would like to interrupt the flow in that method and continue with the next task in another method.

To get an early exit from a method, where you judge it necessary, type the return keyword followed by a semi-colon. Of course, since this is usually used to value a condition, the return keyword can be typed at the end of a conditional statement.

 

Previous Copyright © 2004-2005 FunctionX, Inc. Next

 

Methods and their Parameters 

Arguments 

Overview of an Argument

As we saw in the previous lesson, a method performs an assignment that completes the operations of a class. The methods we used in the previous lesson relied on global variables to exchange

 

175

Page 176: Introduction to C#

information with other sections of the program. Sometimes, a method would need one or more values in order to carry its assignment. The particularity of such a value or such values is that another method that calls this one must supply the needed values. When a method needs a value to complete its assignment, such a value is called an argument.

Practical Learning: Introducing Arguments1. Start Microsoft Visual C# or Visual Studio .NET

2. On the main menu, click File -> New -> Project

3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects

4. In the Templates list, click Empty Project

5. In the Name text box, replace the name with Geometry2 and specify the desired path in the Location

6. Click OK

7. To create a source file for the project, on the main menu, click Project -> Add New Item...

8. In the Add New Item dialog box, in the Templates list, click Code File

9. In the Name text box, delete the suggested name and replace it with Exercise

10. Click Open

11. In the empty file, type the following: 

using System;

namespace GeometricFormulas{

class Cylinder{

static double Radius;static double Height;static double BaseArea;static double LateralArea;static double TotalArea;static double Volume;

static double GetRadius(){

double rad;

do {

Console.Write("Radius: ");rad = double.Parse(Console.ReadLine());

if( rad < 0 )Console.WriteLine("Please enter a positive number");} while( rad < 0 );

return rad;}

176

Page 177: Introduction to C#

static double GetHeight(){

double h;

do {

Console.Write("Height: ");h = double.Parse(Console.ReadLine());

if( h < 0 )Console.WriteLine("Please enter a positive number");} while( h < 0 );

return h;}

static void ProcessCylinder(){Console.WriteLine("Enter the dimensions of the cylinder");

Radius = GetRadius();Height = GetHeight();

BaseArea = Radius * Radius * Math.PI;LateralArea = 2 * Math.PI * Radius * Height;

TotalArea = 2 * Math.PI * Radius * (Height + Radius);Volume = Math.PI * Radius * Radius * Height;

}

static void ShowCylinder(){

Console.WriteLine("\nCylinder Characteristics");Console.WriteLine("Radius: {0}", Radius);Console.WriteLine("Height: {0}", Height);Console.WriteLine("Base: {0:F}", BaseArea);Console.WriteLine("Lateral: {0:F}", LateralArea);Console.WriteLine("Total: {0:F}", TotalArea);Console.WriteLine("Volume: {0:F}", Volume);

}

static void Main(){

ProcessCylinder();ShowCylinder();

Console.WriteLine();}

}}

12. To execute the application, on the main menu, click Debug -> Start Without Debugging 

13. Return to Visual C#

Introduction to Arguments

Like a variable, an argument is represented by its type of value. For example, one method may need a character while another would need a string. Yet another method may require a decimal

177

Page 178: Introduction to C#

number. This means that the method or class that calls a method is responsible for supplying the right value, even though a method may have an internal mechanism of checking the validity of such a value.

The value supplied to a method is typed in the parentheses of the method and it's called an argument. In order to declare a method that takes an argument, you must specify its name and the argument between its parentheses. Because a method must specify the type of value it would need, the argument is represented by its data type and a name. If the method would not return a value, it can be declared as void.

Suppose you want to define a method that displays the side length of a square. Since you would have to supply the length, you can define such a method as follows:

using System;

public class NewProject{

void DisplaySide(double Length){}static void Main(){}

}

In the body of the method, you may or may not use the value of the argument. Otherwise, you can manipulate the supplied value as you see fit. In this example, you can display the value of the argument as follows:

using System;

public class NewProject{

void DisplaySide(double Length){

Console.Write("Length: ");Console.WriteLine(Length);

}static void Main(){}

}

In this case, remember to define the method as static if you plan to access it from Main(). In order to call a method that takes an argument, you must supply a value for the argument when calling the method; otherwise you would receive an error. Also, you should/must supply the right value; otherwise, the method may not work as expected and produce an unreliable result. Here is an example:

using System;

public class NewProject{

static void DisplaySide(double Length){

Console.Write("Length: ");Console.WriteLine(Length);

178

Page 179: Introduction to C#

}static void Main(){

DisplaySide(35.55);

Console.WriteLine();}

}

As mentioned already, a method that takes an argument can also declared its own variable. Such variables are referred to as local.

A method can take more than one argument. When defining such a method, provide each argument with its data type and a name. The arguments are separated by a comma.

 

Practical Learning: Passing Arguments

1. To pass arguments to a method, change the file as follows: 

using System;

class Cylinder{

static double GetTheRadius(){

double rad;

do {

Console.Write("Radius: ");rad = double.Parse(Console.ReadLine());if( rad < 0 )

Console.WriteLine("Please enter a positive number");

} while( rad < 0 );return rad;

}

static double GetTheHeight(){

double h;

do {

Console.Write("Height: ");h = double.Parse(Console.ReadLine());if( h < 0 )

Console.WriteLine("Please enter a positive number");

} while( h < 0 );

return h;}

179

Page 180: Introduction to C#

static double CalculateBaseArea(double rad){

return rad * rad * Math.PI;}

static double CalculateLateralArea(double rad, double hgt){

return 2 * Math.PI * rad * hgt;}

static double CalculateTotalArea(double rad, double hgt){

return 2 * Math.PI * rad * (hgt + rad);}

static double CalculateVolume(double rad, double hgt){

return Math.PI * rad * rad * hgt;}

static void ProcessCylinder(){

double Radius;double Height;double BaseArea;double LateralArea;double TotalArea;double Volume;

Console.WriteLine("Enter the dimensions of the cylinder");Radius = GetTheRadius();Height = GetTheHeight();

BaseArea = CalculateBaseArea(Radius);LateralArea = CalculateLateralArea(Radius, Height);TotalArea = CalculateTotalArea(Radius, Height);Volume = CalculateVolume(Radius, Height);

Console.WriteLine("\nCylinder Characteristics");Console.WriteLine("Radius: {0}", Radius);Console.WriteLine("Height: {0}", Height);Console.WriteLine("Base: {0:F}", BaseArea);Console.WriteLine("Lateral: {0:F}", LateralArea);Console.WriteLine("Total: {0:F}", TotalArea);Console.WriteLine("Volume: {0:F}", Volume);

}

static void Main(){

ProcessCylinder();

Console.WriteLine();}

}

180

Page 181: Introduction to C#

2. Execute the application Here is an example of executing the program: 

Enter the dimensions of the cylinderRadius: 35.96Height: 30.28

Cylinder CharacteristicsRadius: 35.96Height: 30.28Base: 4062.46Lateral: 6841.56Total: 14966.49Volume: 123011.33

3. Return to Visual C#

 

Techniques of Passing Arguments 

Passing Arguments by Value

When calling the methods that take one or more arguments, we made sure we provided the needed argument. This is because an argument is always required and the calling function or class must provided a valid value when calling such a method.

 

Practical Learning: Passing Arguments By Value

1. All the arguments we have used so far were passed by value. For one more example, change the program as follows: 

using System;

class Cylinder{

static double GetTheRadius(){

double rad;

do {

Console.Write("Radius: ");rad = double.Parse(Console.ReadLine());if( rad < 0 )

Console.WriteLine("Please enter a positive number");

} while( rad < 0 );return rad;

181

Page 182: Introduction to C#

}

static double GetTheHeight(){

double h;

do {

Console.Write("Height: ");h = double.Parse(Console.ReadLine());if( h < 0 )

Console.WriteLine("Please enter a positive number");

} while( h < 0 );

return h;}

static double CalculateBaseArea(double rad){

return rad * rad * Math.PI;}

static double CalculateLateralArea(double rad, double hgt){

return 2 * Math.PI * rad * hgt;}

static double CalculateTotalArea(double rad, double hgt){

return 2 * Math.PI * rad * (hgt + rad);}

static double CalculateVolume(double rad, double hgt){

return Math.PI * rad * rad * hgt;}

static void ProcessCylinder(){

double Radius;double Height;double BaseArea;double LateralArea;double TotalArea;double Volume;

Console.WriteLine("Enter the dimensions of the cylinder");Radius = GetTheRadius();Height = GetTheHeight();

BaseArea = CalculateBaseArea(Radius);LateralArea = CalculateLateralArea(Radius, Height);TotalArea = CalculateTotalArea(Radius, Height);Volume = CalculateVolume(Radius, Height);

182

Page 183: Introduction to C#

ShowCylinder(Radius, Height, BaseArea, LateralArea, TotalArea, Volume);

}

static void ShowCylinder(double rad, double hgt, double BArea, double Lateral, double Total, double

vol){

Console.WriteLine("\nCylinder Characteristics");Console.WriteLine("Radius: {0}", rad);Console.WriteLine("Height: {0}", hgt);Console.WriteLine("Base: {0:F}", BArea);Console.WriteLine("Lateral: {0:F}", Lateral);Console.WriteLine("Total: {0:F}", Total);Console.WriteLine("Volume: {0:F}", vol);

}

static void Main(){

ProcessCylinder();

Console.WriteLine();}

}

2. Execute the application and test it. Here is an example: 

Enter the dimensions of the cylinderRadius: 38.26Height: 28.48

Cylinder CharacteristicsRadius: 38.26Height: 28.48Base: 4598.75Lateral: 6846.44Total: 16043.94Volume: 130972.40

3. Return to Visual C#

Passing an Argument by Reference

Consider the following program:

using System;

public class Payroll{

static void Earnings(double ThisWeek, double Salary){

ThisWeek = 42.50;

Console.WriteLine("\nIn the Earnings() function,");Console.Write("Weekly Hours = ");

183

Page 184: Introduction to C#

Console.WriteLine(ThisWeek);Console.Write("Salary = ");Console.WriteLine(Salary);Console.Write("Weekly Salary: = ");Console.WriteLine(ThisWeek * Salary);

}

static void Main(){

double Hours, Rate;

Rate = 15.58;Hours = 26.00;

Console.WriteLine("In the Main() function,");Console.Write("\nWeekly Hours = ");Console.Write(Hours);Console.Write("\nSalary = ");Console.WriteLine(Rate); Console.Write("Weekly Salary = ");Console.WriteLine(Hours * Rate);

Console.WriteLine("\nCalling the Earnings() function");

Earnings(Hours, Rate);

Console.Write("\nAfter calling the Earnings() function, ");Console.WriteLine("\nin the Main() function,");Console.Write("\nWeekly Hours = ");Console.Write(Hours);Console.Write("\nSalary = ");Console.WriteLine(Rate);Console.Write("Weekly Salary = ");Console.WriteLine(Hours * Rate);

Console.Write("\n");}

}

 

Introduction to Classes 

A Re-Introduction to Classes 

 

184

Page 185: Introduction to C#

Overview

Based on a brief definition we had in the first lesson, we have been using classes in a very introductory manner. This was necessary because everything in C# is built around a concept of class. A class can be more elaborate than what we have been acquainted to. A class is a technique of creating one or a group of variables to be used as a foundation for a more detailed variable. A class must be created in a computer file. You can create such a file with only one class or you can create many classes inside of one file.

To create a class, you start with the class keyword followed by a name and its body delimited by curly brackets. Here is an example of a class called Book:

class Book{}

Practical Learning: Introducing Classes1. Start Microsoft Visual C# or Visual Studio .NET 2. On the main menu, click File -> New -> Project 3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects 4. In the Templates list, click Empty Project 5. In the Name text box, replace the name with DeptStore1 and specify the desired path in the

Location 6. Click OK 7. To create a source file for the project, on the main menu, click Project -> Add New Item... 8. In the Add New Item dialog box, in the Templates list, click Code File 9. In the Name text box, delete the suggested name and replace it with Exercise 10. Click Open 11. In the empty file, type the following:

 

using System;

class DepartmentStore{}

class Exercise{ static void Main() { }}

12. Save the file

 

Class Variable Declaration

Like any normal variable, to use a class in your program, you can first declare a variable for it

185

Page 186: Introduction to C#

(when we study static fields, we will see that you can use a class without primarily declaring it). Like the variables we introduced in the previous lesson, to declare a variable of a class, type its name followed by a name for the variable. For example, to declare a variable of the above Book in another class called NewProject, you could type the following:

using System;

class Book{}

class NewProject{

static void Main(){

Book ToRead;}

}

The variables we have declared so far are called value variables. This is because such a variable of a primitive type holds its value. The C# language supports another type of variable. This time, when you declare the variable, its name doesn't hold the value of the variable; it holds a reference to the address where the actual variable is stored. This reference type is the kind used to declare a variable for a class.

To use a variable as reference, you must initialize it using an operator called new. Here is an example:

using System;

class Book{}

class NewProject{

static void Main(){

Book ToRead;

ToRead = new Book();}

}

You can also use the new operator directly when declaring the variable as follows:

Book ToRead = new Book();

In C#, as well as Visual Basic, if you create a class in any of the files that belong to the same project, the class is made available to all other parts of the same project.

 

Practical Learning: Declaring a Class Variable

186

Page 187: Introduction to C#

1. To use the DepartmentStore class in the main class of the project, declare the variable as follows: 

using System;

class DepartmentStore{}

class Exercise{ static void Main() {

DepartmentStore dptStore = new DepartmentStore(); }}

2. Save the exercise.cs file

Sharing Classes

Unlike its sisters the C and the C++ languages, C# was developed with the idea of working complementarily with other languages such as C++, Visual Basic, and J#. In other words, code from these other languages should be able to "read" or access code written in a C# application. To make this possible, a C# class can be created as a public object.

If you want your class to be accessible to code written in other languages, precede the class keyword with public when creating it. Here is an example:

using System;

public class Exercise{

static void Main(){

int Number = 244;object Thing = 1450.58;

Console.WriteLine(Number);Console.WriteLine(Thing);

}}

Garbage Collection

When you initialize a variable using the new operator, you are in fact asking the compiler to provide you some memory space in the heap memory. The compiler is said to "allocate" memory for your variable. When that variable is no longer needed, such as when your program closes, it (the variable) must be removed from memory and the space it was using can be made available to other variables or other programs. This is referred to as garbage collection. In the past, namely in C/C++, this was a concern for programmers because they usually had to remember to manually delete such a variable (a pointer) and free its memory.

The .NET Framework solves the problem of garbage collection by letting the compiler "clean"

187

Page 188: Introduction to C#

memory after you. This is done automatically when the compiler judges it necessary so that the programmer doesn't need to worry about this issue.

A Program With Various Files

Instead of only one file, you can create a program with many of them. Each file can contain different assignments that, when put together, can create an application as complete as possible. To make this possible, you can create each file with a name and the extension .cs. To compile the project, after typing csc, you can list the files separated by commas.

In C++ and Pascal, if you create a class CA in a certain file FA and want to access anything included in that class from another file FB, you must type #include (C/C++) or uses (Pascal) followed by the name of the file FA somewhere in the top section of the file FB.

 

Class' Fields 

Introduction

Consider the Book class defined earlier:

public class Book{}

The section between the curly brackets of a class is referred to as its body. In the body of a class, you can create a list of the parts that make up the class. Each of these parts must be a complete variable with a name and a data type. For example, here are the characteristics that make up a book, declared as the parts of the above Book class and each declared as a variable:

public class Book{

string Title;string Author;short YearPublished;int NumberOfPages;char CoverType;

}

The variables declared in the body of a class are referred to as its member variables and each member variable is referred to as a field. The fields can be any type we have seen in the previous lesson. Based on this, when creating a class, it is your job to decide what your object is made of.

 

Practical Learning: Introducing Class Members

Imagine you want to write a (console-based) program for a department store and the customer

188

Page 189: Introduction to C#

has given you a preliminary catalog as follows:

Stock #: 437876Women Spring Coat Classy

Unit: $145.55

Stock #: 79475Women Bathing Suit Sandstone

$75.55

Stock #: 74797Women Suit Exchange

$225.75

Stock: 68432Men Jacket

$115.85

Stock #: 75947Children Summer Hat

$17.95

Stock #: 48746Children Playground Dress

$24.55

Each item in this catalog is represented by its Stock number, its name or description, and its price. Based on this, you can create a class that represents each item.

1. Change the DepartmentStore class as follows: 

using System;

public class DepartmentStore{ long ItemNumber; string ItemName; double UnitPrice;

189

Page 190: Introduction to C#

}

public class Exercise{ static void Main() {

DepartmentStore dptStore = new DepartmentStore(); }}

2. Save the file

 

Access to Class Members

The parts of an object fall into two main categories: those you can touch and those you don't have access to. For example, for a car parked at the mall, you can see or touch its doors and its tires but you don't see its engine or its spare tire, you don't even know if it has one. The parts of an object that you have access to are referred to as public. Those you can't see or touch are referred to as private.

A C# class also recognizes that some parts of a class can be made available to other classes and some other parts can be hidden from other classes. A part that must be hidden from other classes is private and it can be declared starting with the private keyword. If you declare a member variable and want to make it available to other classes, you must start its name with the public keyword. The public and private keywords are referred to as access level.

By default, if you declare a member variable (or anything else) in a class but don't specify its access level, the member is considered private and cannot be accessed from outside, that is by a non-member, of that class. Therefore, to make a member accessible by other classes, you must declare it as public.

You can use a mix of public and private members in a class and there is no rule on which access level should be listed first or last. Here is an example:

public class Book{

public string Title;public string Author";short YearPublished;private int NumberOfPages;char CoverType;

}

Just keep in mind that if you omit or forget the access level of a member of a class, the member is automatically made private.

After declaring a member of a class, to access it from another class, first declare a variable from its class as we saw earlier. To actually access the method, use the period operator as follows:

using System;

class Book{

public string Title;

190

Page 191: Introduction to C#

public string Author;short YearPublished;private int NumberOfPages;char CoverType;

}

class BookClub{

static void Main(){

Book ToRead = new Book();ToRead.Author = "Francis Patrick Kouma";

Console.WriteLine(ToRead.Author);}

}

To reduce confusion as to what member is public or private, we will always specify the access level of a member variable.

 

Practical Learning: Using a Class' Fields1. To make public the members of the DepartmentStore class, type the public keyword to their

left 2. To access the members of the DepartmentStore class, change Main() as follows:

 

using System;

public class DepartmentStore{

public long ItemNumber;public string ItemName;public double UnitPrice;

}

public class Exercise{

static void Main(){

DepartmentStore dptStore = new DepartmentStore();

dptStore.ItemNumber = 846785;dptStore.ItemName = "Women Underwear";dptStore.UnitPrice = 15.65;

Console.WriteLine("Department Store");Console.WriteLine("Item #: {0}",

dptStore.ItemNumber);Console.WriteLine("Description: {0}",

dptStore.ItemName);Console.WriteLine("Unit Price: {0:C}\n",

dptStore.UnitPrice);}

191

Page 192: Introduction to C#

}

3. Execute the application  4. Return to Visual C#

 

Characteristics of Members of a Class 

Constants

Unlike C/C++, in C#, you can create a constant variable in a class. As done in Lesson 2 when studying variables, to declare a constant variable, type the const keyword to the left of the variable. Once again, when declaring a constant, you must initialize it with an appropriate constant value. Here is an example:

using System;

public class Exercise{

public static void MeterToInch(){

const double ConversionFactor = 39.37;double Meter, Inch;string Answer;

do {

Console.Write("Enter the value in meters: ");Meter = double.Parse(Console.ReadLine());

Inch = Meter * ConversionFactor;Console.WriteLine("{0}m = {1}in", Meter, Inch);

Console.Write("\nDo you want to perform another conversions(y/n)? ");

Answer = Console.ReadLine();Console.WriteLine();

} while(Answer == "y" || Answer == "Y");}

static void Main(){

MeterToInch();}

}

Here is an example of running the program:

Enter the value in meters: 25.3825.38m = 999.2106in

Do you want to perform another conversions(y/n)? y

192

Page 193: Introduction to C#

Enter the value in meters: 44.6844.68m = 1759.0516in

Do you want to perform another conversions(y/n)? Y

Enter the value in meters: 18.2818.28m = 719.6836in

Do you want to perform another conversions(y/n)? n

 

Practical Learning: Creating a Constant1. To start a new project, on the main menu, click File -> New -> Project 2. In the Templates section, click Empty Project 3. Set the Name to IceCream1 and press Enter 4. To add a source file, on the main menu, click Project -> Add New Item... 5. In addition to what we have learned so far, to create two constants, type the following:

 

using System;

class IceCream{

public const decimal BasePrice = 1.05M;public const string DefaultFlavor = "Diet Mint Fudge High

Carb";

public char Flavor;public char Container;public char Ingredient;public int Scoops;public decimal TotalPrice;

}

class Exercise{

static void Main(){}

}

6. Save the file in a new folder named IceCream1 7. Save the file itself as Exercise.cs 8. At the Command Prompt, switch to the IceCream1 directory 9. Compile and test the file then return to Notepad

 

Read-Only Fields

When creating a member variable of a class, one of the decisions you make consists of deciding how the field would get its value(s). Sometimes you will allow the clients of the class to change the values of the field. In some other cases, you may want the field to only hold or present the

193

Page 194: Introduction to C#

value without being able to change it. This can still allow the clients to access the field and its value but on a read-only basis.

To create a field whose value can only be read, precede its data type, during declaration, with the readonly keyword. Here is an example:

public readonly double PI;

After declaring the variable, you should initialize it. You have two main alternatives. You can initialize the field when declaring it. Here is an example:

using System;

namespace Geometry{

class Circle{

public double Radius;

public Circle(double rad){

Radius = rad;}

public readonly double PI = 3.14159;}

class Exercise{

static int Main(){

Circle circ = new Circle(24.72);

Console.WriteLine("Circle Characteristics");Console.WriteLine("Radius: {0}", circ.Radius);Console.WriteLine("PI: {0}\n", circ.PI);

return 0;}

}}

Alternatively, you can initialize the field in the(a) constructor of its class. This would be done as follows:

using System;

namespace Geometry{

class Circle{

public double Radius;

public Circle(double rad){

Radius = rad;PI = 3.14159;

194

Page 195: Introduction to C#

}

public readonly double PI;}

class Exercise{

static int Main(){

Circle circ = new Circle(24.72);

Console.WriteLine("Circle Characteristics");Console.WriteLine("Radius: {0}", circ.Radius);Console.WriteLine("PI: {0}\n", circ.PI);

return 0;}

}}

If the value held by a read-only field is gotten from an expression, then the field must be initialized in the(a) construction with the desired expression. Based on this, the following code will not compile:

using System;

namespace Geometry{

class Circle{

public double Radius;

public Circle(double rad){

Radius = rad;PI = 3.14159;

}

public readonly double PI;public readonly double Diameter = Radius * 2;

}

class Exercise{

static int Main(){

Circle circ = new Circle(24.72);

Console.WriteLine("Circle Characteristics");Console.WriteLine("Radius: {0}", circ.Radius);Console.WriteLine("PI: {0}\n", circ.PI);Console.WriteLine("Diameter: {0}\n", circ.Diameter);

return 0;}

}}

195

Page 196: Introduction to C#

This would produce:

C:\Programs\MSVCS\Project1\Exercise.cs(16): A field initializer cannot reference the nonstatic field, method, or property 'Geometry.Circle.Radius'

One solution to this error is to declare the field as read-only in the class and then initialize it in the(a) constructor with the expression. Here are a few examples:

using System;

namespace Geometry{

class Circle{

public double Radius;

public Circle(double rad){

Radius = rad;Diameter = Radius * 2;Circumference = Diameter * PI;Area = Radius * Radius * PI;

}

public readonly double PI = 3.14159;public readonly double Diameter;public readonly double Circumference;public readonly double Area;

}

class Exercise{

static int Main(){

Circle circ = new Circle(24.72);

Console.WriteLine("Circle Characteristics");Console.WriteLine("Radius: {0}", circ.Radius);Console.WriteLine("Diameter: {0}", circ.Diameter);Console.WriteLine("Circumference: {0}",

circ.Circumference);Console.WriteLine("Area: {0}\n", circ.Area);

return 0;}

}}

This would produce:

Circle CharacteristicsRadius: 24.72Diameter: 49.44Circumference: 155.3202096Area: 1919.757790656

196

Page 197: Introduction to C#

In the previous section, we saw that a constant variable must be initialized when it is created. Although a read-only variable seems to follow the same rule, it doesn't. Remember that you don't need to initialize a read-only variable when you declare it since you can do this in the(a) constructor of the class. Also, because a constructor can be overloaded, a read-only field can hold different values depending on the particular constructor that is accessed at a particular time but the value of a constant variable cannot change: it is initialized once, in the class (or in a method) and it keeps that value throughout the class (or method).

 

Static Fields

Imagine you create a class called Book. To access it in the Main() function, you can declare its variable, as we have done so far. A variable you have declared of a class is also called an instance of the class. In the same way, you can declare various instances of the same class as necessary:

using System;

public class Book{

public string Title;public string Author";public short YearPublished;public int NumberOfPages;public char CoverType;

}

public class NewProject{

static void Main(){

Book written = new Book();Book bought = new Book();

}}

Each one of these instances gives you access to the members of the class but each instance holds the particular values of the members of its instance. Consider the results of the following program:

using System;

public class Book{ public string Title; public string Author; public short YearPublished; public int NumberOfPages; public char CoverType;}

public class Program{ static void Main()

197

Page 198: Introduction to C#

{ Book First = new Book();

First.Title = "Psychology and Human Evolution"; First.Author = "Jeannot Lamm"; First.YearPublished = 1996; First.NumberOfPages = 872; First.CoverType = 'H';

Console.WriteLine("Book Characteristics"); Console.WriteLine("Title: {0}", First.Title); Console.WriteLine("Author: {0}", First.Author); Console.WriteLine("Year: {0}", First.YearPublished); Console.WriteLine("Pages: {0}", First.NumberOfPages); Console.WriteLine("Cover: {0}\n", First.CoverType);

Book Second = new Book();

Second.Title = "C# First Step"; Second.Author = "Alexandra Nyango"; Second.YearPublished = 2004; Second.NumberOfPages = 604; Second.CoverType = 'P';

Console.WriteLine("Book Characteristics"); Console.WriteLine("Title: {0}", Second.Title); Console.WriteLine("Author: {0}", Second.Author); Console.WriteLine("Year: {0}", Second.YearPublished); Console.WriteLine("Pages: {0}", Second.NumberOfPages); Console.WriteLine("Cover: {0}\n", Second.CoverType); }}

This would produce:

Book CharacteristicsTitle: Psychology and Human EvolutionAuthor: Jeannot LammYear: 1996Pages: 872Cover: H

Book CharacteristicsTitle: C# First StepAuthor: Alexandra NyangoYear: 2004Pages: 604Cover: P

All of the member variables and methods of classes we have used so far are referred to as instance members because, in order to access them, you must have an instance of a class declared in another class in which you want to access them.

C# allows you to declare a class member and refer to it regardless of which instance of an object you are using. Such a member variable is called static. To declare a member variable of a class as static, type the static keyword on its left. Whenever you have a static member, in order to refer to it, you must "qualify" it in the class in which you want to call it. Qualifying a

198

Page 199: Introduction to C#

member means you must give its complete location, including the name of its class and the namespace (if any) in which its class was created. Here is an example:

using System;

public class Book{ public static string Title; public static string Author; public short YearPublished; public int NumberOfPages; public char CoverType;}

public class Program{ static void Main() { Book First = new Book();

Book.Title = "Psychology and Human Evolution"; Book.Author = "Jeannot Lamm"; First.YearPublished = 1996; First.NumberOfPages = 872; First.CoverType = 'H';

Console.WriteLine("Book Characteristics"); Console.WriteLine("Title: {0}", Book.Title); Console.WriteLine("Author: {0}", Book.Author); Console.WriteLine("Year: {0}", First.YearPublished); Console.WriteLine("Pages: {0}", First.NumberOfPages); Console.WriteLine("Cover: {0}\n", First.CoverType);

Book Second = new Book();

Book.Title = "C# First Step"; Book.Author = "Alexandra Nyango"; Second.YearPublished = 2004; Second.NumberOfPages = 604; Second.CoverType = 'P';

Console.WriteLine("Book Characteristics"); Console.WriteLine("Title: {0}", Book.Title); Console.WriteLine("Author: {0}", Book.Author); Console.WriteLine("Year: {0}", Second.YearPublished); Console.WriteLine("Pages: {0}", Second.NumberOfPages); Console.WriteLine("Cover: {0}\n", Second.CoverType);

Console.ReadLine(); }}

Notice that when a member variable has been declared as static, you don't need an instance of the class to access that member variable outside of the class. Based on this, if you declare all members of a class as static, you don't need to declare a variable of their class in order to access them. In the following example, the Title and Author fields of the Book class are

199

Page 200: Introduction to C#

accessed from the Program class without using an instance of the Book class:

using System;

public class Book{ public static string Title; public static string Author;}

public class Program{ static void Main() { Book.Title = "Psychology and Human Evolution"; Book.Author = "Jeannot Lamm"; Console.WriteLine("Book Characteristics"); Console.WriteLine("Title: {0}", Book.Title); Console.WriteLine("Author: {0}\n", Book.Author);

Book.Title = "C# First Step"; Book.Author = "Alexandra Nyango";

Console.WriteLine("Book Characteristics"); Console.WriteLine("Title: {0}", Book.Title); Console.WriteLine("Author: {0}\n", Book.Author);

Console.ReadLine(); }}

As we saw in Lesson 6, you can also declare member variables of the main class as static. If you are referring to a static member variable in the same class in which it was declared, you don't have to qualify it. Here is an example:

using System;

public class NewProject{

static double Length;static double Width;

static void Main(){

Console.WriteLine("Rectangles Characteristics");

Length = 22.55;Width = 20.25;

Console.WriteLine("\nRectangle 1");Console.Write("Length: ");Console.WriteLine(Length);Console.Write("Width: ");Console.WriteLine(Width);

200

Page 201: Introduction to C#

Length = 254.04;Width = 408.62;

Console.WriteLine("\nRectangle 2");Console.Write("Length: ");Console.WriteLine(Length);Console.Write("Width: ");Console.WriteLine(Width);

Console.WriteLine();}

}

Practical Learning: Using Static Fields1. To use static fields, change the program as follows:

 

using System;

class IceCream{

public const decimal BasePrice = 1.05M;public const string DefaultFlavor = "Diet Mint Fudge High Carb";

public static char Flavor;public static char Container;public static char Ingredient;public static int Scoops;public static decimal TotalPrice;

}

class Exercise{

static void Main(){

Console.WriteLine("Ice Cream Vendor Machine");Console.WriteLine("What type of flavor do you want?");Console.WriteLine("a - French Vanilla");Console.WriteLine("b - Strawberry Vanilla");Console.WriteLine("c - Butter Pecan");Console.WriteLine("d - Cream of Cocoa");Console.Write("Your Choice? " );IceCream.Flavor = char.Parse(Console.ReadLine());

Console.WriteLine("What type of container do you want?");Console.WriteLine("1 - Cone\n2 - Cup\n3 - Bowl");Console.Write("Your Choice? ");IceCream.Container = char.Parse(Console.ReadLine());

Console.WriteLine("Do you want an ingredient or not");Console.WriteLine("0 - No Ingredient");Console.WriteLine("1 - Peanuts");Console.WriteLine("2 - M & M");Console.WriteLine("3 - Cookies");Console.Write("Your Choice? ");

201

Page 202: Introduction to C#

IceCream.Ingredient = char.Parse(Console.ReadLine());

Console.Write("How many scoops(1, 2, or 3)? ");IceCream.Scoops = int.Parse(Console.ReadLine());IceCream.TotalPrice = IceCream.BasePrice * IceCream.Scoops;

Console.WriteLine("\nIce Cream Order");switch(IceCream.Flavor){

case 'a':case 'A':

Console.WriteLine("Flavor: French Vavilla");break;

case 'b':case 'B':

Console.WriteLine("Flavor: Strawberry Vanilla");break;

case 'c':case 'C':

Console.WriteLine("Flavor: Butter Pecan");break;

case 'd':case 'D':

Console.WriteLine("Flavor: Cream of Cocoa");break;

default:Console.WriteLine("Confused Flavor - {0}",

IceCream.DefaultFlavor);break;

}

switch(IceCream.Container){

case '1':Console.WriteLine("Container: Cone");break;

case '2':Console.WriteLine("Container: Cup");break;

case '3':Console.WriteLine("Container: Bowl");break;

default:Console.WriteLine("Container: Cone");break;

}

switch(IceCream.Ingredient){

case '0':Console.WriteLine("Ingredient: None");break;

case '1':Console.WriteLine("Ingredient: Peanuts");break;

202

Page 203: Introduction to C#

case '2':Console.WriteLine("Ingredient: M & M");break;

case '3':Console.WriteLine("Ingredient: Cookies");break;

default:Console.WriteLine("Ingredient: None");break;

}

Console.WriteLine("Scoops: {0}", IceCream.Scoops);Console.WriteLine("Total Price: {0:C}", IceCream.TotalPrice);Console.WriteLine();

}}

2. Save the file, compile and test it at the Command Prompt. Here is an example: 3. Return to Notepad

Static Methods

Like a member variable, a method of a class can be define as static. This means that this particular method can access any member of the class regardless of the instance if there are many instances of the class declared.

To define a method as static, type the static keyword to its left. Here is an example:

using System;

public class Book{

private static string Title;static private string Author;private static int Pages;static private double Price;

static public void CreateBook(){

Title = "Psychology and Human Evolution";Author = "Jeannot Lamm";Pages = 472;Price = 24.95;

}

static public void ShowBook(){

Console.WriteLine("Book Characteristics");Console.WriteLine("Title: {0}", Book.Title);Console.WriteLine("Author: {0}", Book.Author);Console.WriteLine("Pages: {0}", Pages);Console.WriteLine("Price: {0:C}\n", Price);

}}

203

Page 204: Introduction to C#

public class Program{

static void Main(){

Book.CreateBook();Book.ShowBook();

}}

This would produce:

Book CharacteristicsTitle: Psychology and Human EvolutionAuthor: Jeannot LammPages: 472Price: $24.95

The ReadLine(), the Write(), and the WriteLine() methods of the Console class that we have used so far are examples of static methods. There are many others in the C# language. The documentation can guide you to know what method of what class is static or not.

 

Practical Learning: Using Static Methods1. To use static methods, change the file as follows:

 

using System;

class IceCream{

public const decimal BasePrice = 1.05M;public const string DefaultFlavor = "Diet Mint Fudge High Carb";

private static char Flavor;private static char Container;private static char Ingredient;private static int Scoops;private static decimal TotalPrice;

public static void ProcessAnOrder(){

Console.WriteLine("Ice Cream Vendor Machine");Console.WriteLine("What type of flavor do you want?");Console.WriteLine("a - French Vanilla");Console.WriteLine("b - Strawberry Vanilla");Console.WriteLine("c - Butter Pecan");Console.WriteLine("d - Cream of Cocoa");Console.Write("Your Choice? " );IceCream.Flavor = char.Parse(Console.ReadLine());

Console.WriteLine("What type of container do you want?");Console.WriteLine("1 - Cone\n2 - Cup\n3 - Bowl");Console.Write("Your Choice? ");IceCream.Container = char.Parse(Console.ReadLine());

204

Page 205: Introduction to C#

Console.WriteLine("Do you want an ingredient or not");Console.WriteLine("0 - No Ingredient");Console.WriteLine("1 - Peanuts");Console.WriteLine("2 - M & M");Console.WriteLine("3 - Cookies");Console.Write("Your Choice? ");IceCream.Ingredient = char.Parse(Console.ReadLine());

Console.Write("How many scoops(1, 2, or 3)? ");IceCream.Scoops = int.Parse(Console.ReadLine());IceCream.TotalPrice = IceCream.BasePrice * IceCream.Scoops;

}

public static void DisplayReceipt(){

Console.WriteLine("\nIce Cream Order");switch(IceCream.Flavor){

case 'a':case 'A':

Console.WriteLine("Flavor: French Vavilla");break;

case 'b':case 'B':

Console.WriteLine("Flavor: Strawberry Vanilla");break;

case 'c':case 'C':

Console.WriteLine("Flavor: Butter Pecan");break;

case 'd':case 'D':

Console.WriteLine("Flavor: Cream of Cocoa");break;

default:Console.WriteLine("Confused Flavor - {0}",

IceCream.DefaultFlavor);break;

}

switch(IceCream.Container){

case '1':Console.WriteLine("Container: Cone");break;

case '2':Console.WriteLine("Container: Cup");break;

case '3':Console.WriteLine("Container: Bowl");break;

default:Console.WriteLine("Container: Cone");break;

}

205

Page 206: Introduction to C#

switch(IceCream.Ingredient){

case '0':Console.WriteLine("Ingredient: None");break;

case '1':Console.WriteLine("Ingredient: Peanuts");break;

case '2':Console.WriteLine("Ingredient: M & M");break;

case '3':Console.WriteLine("Ingredient: Cookies");break;

default:Console.WriteLine("Ingredient: None");break;

}

Console.WriteLine("Scoops: {0}", IceCream.Scoops);Console.WriteLine("Total Price: {0:C}", IceCream.TotalPrice);

}}

class Exercise{

static void Main(){

IceCream.ProcessAnOrder();IceCream.DisplayReceipt();

Console.WriteLine();}

}

2. Save, compile, and test the file 3. Return to Notepad

this Instance

We have mentioned two techniques of accessing the members of a class, one consisted of declaring a variable of the class, the other had to do with static members. None of these techniques is important if you want to access a field or method of a class from another method of the same class. We know already that the members of a class are made available to all other members of the same class without being declared or qualified. Consider the following class:

class Triangle{

double Base;double Height;double Area;

void Show(){

206

Page 207: Introduction to C#

double Area;

Area = Base * Height / 2;}

}

When the Area variable is used in the Show() method, there are two variables available and named Area. It makes it confusing to know what particular variable is being accessed. C#, like many other languages, provides a special member of a class that allows you to specify the member of a class when accessing it. This member is called this.

When using the this member variable (in C/C++, it is a pointer), you can access any member of a class within any method of the same class. Here is an example:

using System;

class Triangle{

double Base;double Height;

public void Show(){

double Area;

// Using "this" to access the members of this classthis.Base = 24.55;this.Height = 20.75;

// You cannot use this to access Area because Area // is not a member of this classArea = this.Base * this.Height / 2;

Console.WriteLine("Triangle Characteristics");Console.WriteLine("Base: {0}", this.Base);Console.WriteLine("Height: {0}", this.Height);Console.WriteLine("Area: {0}", Area);

}}

class Exercise{

static void Main(){

Triangle tri = new Triangle();tri.Show();

}}

This would produce:

Triangle CharacteristicsBase: 24.55Height: 20.75Area: 254.70625

There are exceptions when using this:

207

Page 208: Introduction to C#

The this member can never be declared: it is automatically implied when you create a class this cannot be used in a class A to access a member of class B. The following will cause an

error: 

class Exercise{

static void Main(){

Triangle tri = new Triangle();this.tri.Show();

}}

this cannot be used in a static method. The following program will not compile because this is used in the Show() method declared as a static method: 

using System;

class Triangle{

static double Base;static double Height;

static public void Show(){

double Area;

// Using "this" to access the members of this classthis.Base = 24.55;this.Height = 20.75;

// You cannot use this to access Area because Area // is not a member of this classArea = this.Base * this.Height / 2;

Console.WriteLine("Triangle Characteristics");Console.WriteLine("Base: {0}", this.Base);Console.WriteLine("Height: {0}", this.Height);Console.WriteLine("Area: {0}", this.Area);

}}

class Exercise{

static void Main(){

Triangle.Show();}

}

  

208

Page 209: Introduction to C#

Practical Learning: Using this1. To start a new file, on the main menu of Notepad, click File -> New 2. In addition to what we have learned so far, to use this, type the following:

 

using System;

public class StoreItem{

private long ItemNumber;private string ItemName;private double UnitPrice;

public void CreateAnItem(long nbr, string name, double price){

this.ItemNumber = nbr;this.ItemName = name;this.UnitPrice = price;

}

public void DisplayAnItem(){

Console.WriteLine("Department Store");Console.WriteLine("Item #: {0}", this.ItemNumber);Console.WriteLine("Description: {0}", this.ItemName);Console.WriteLine("Unit Price: {0:C}\n", this.UnitPrice);

}}

public class Exercise{

static void Main(){

StoreItem item = new StoreItem();

item.CreateAnItem(348649, "Men 3-Piece Jacket", 275.95);item.DisplayAnItem();

}}

3. Save the file in a new folder named DeptStore2 4. Save the file itself as Exercise.cs 5. Compile it at the Command Prompt and test it. This would produce:

 

Department StoreItem #: 348649Description: Men 3-Piece JacketUnit Price: $275.95

6. Return to Notepad

 

209

Page 210: Introduction to C#

Structures 

Introduction

A structure is an enhanced version of the primitive data types we have used in previous lessons. The difference is that, like a class, a structure is created from one primitive type or by combining various primitive types, resulting in an advanced data type that is not inherently built in the C# language.

To create a structure, you use the same formula as a class but with the struct keyword. Here is an example of a structure:

struct Box{}

Like a class, a structure can have fields. They are listed in the body of the structure.

 

Practical Learning: Creating a Structure1. To create a structure, change the file as follows:

 

using System;

public struct StoreItem{

public long ItemNumber;public string ItemName;public double UnitPrice;

public void DisplayAnItem(){

Console.WriteLine("Department Store");Console.WriteLine("Item #: {0}",

this.ItemNumber);Console.WriteLine("Description: {0}", this.ItemName);Console.WriteLine("Unit Price: {0:C}\n",

this.UnitPrice);}

}

public class Exercise{

static void Main(){

}}

2. Save the Exercise.cs file

210

Page 211: Introduction to C#

 

Structure Declaration

Like any other data type, to use a structure, you can first declare a variable from it. Like primitive data types and unlike a class, a structure is a value type. Therefore, you can declare it without the new keyword. Here is an example:

using System;

struct Box{}

class DeptStore{

static void Main(){

Box boite;}

}

Like the primitive data type, the memory for a variable of a structure is allocated on the stack but you can use the new operator to declare a structure:

using System;

struct Box{}

class DeptStore{

static void Main(){

Box boite = new Box();

Console.WriteLine();}

}

Although there are many similarities in the behaviors of classes and structures, you should use a structure when the object you are creating is meant to represent relatively small values.

Practical Learning: Declaring a Structure1. To declare and initialize a variable of a structure, change the file as follows:

 

using System;

public struct StoreItem{

public long ItemNumber;public string ItemName;

211

Page 212: Introduction to C#

public double UnitPrice;

public void DisplayAnItem(){

Console.WriteLine("Department Store");Console.WriteLine("Item #: {0}",

this.ItemNumber);Console.WriteLine("Description: {0}", this.ItemName);Console.WriteLine("Unit Price: {0:C}\n",

this.UnitPrice);}

}

public class Exercise{

static void Main(){

StoreItem item;

item.ItemNumber = 348649;item.ItemName = "Men 3-Piece Jacket";item.UnitPrice = 275.95;

item.DisplayAnItem();}

}

2. Save the Exercise.cs file 3. Compile and execute it

 

Class Construction and Destruction 

The Default Constructor

When you declare a variable of a class, a special method must be called to initialize the members of that class. This method is automatically provided for every class and it is called a constructor. Whenever you create a new class, a constructor is automatically provided to it. This  particular constructor is called the default constructor. You have the option of creating it or not. Although a constructor is created for your class, you can customize its behavior or change it tremendously.

A constructor holds the same name as its class and doesn't return any value, not even void. Here is an example:

public class Book{ Book()}

Like every method, a constructor can be equipped with a body. In this body, you can access any of the member variables (or method(s)) of the same class. When introducing classes other than the main class, we saw that, to use such a class, you can declare its variable and allocate

212

Page 213: Introduction to C#

memory using the new operator. You can notice that we always included the parentheses when declaring such a variable. Here is an example:

public class Class1{ static void Main() {

Exercise exo = new Exercise(); }}

In this case, the parentheses indicate that we are calling the default constructor to instantiate the class.

C++ NoteIn C++, if you declare a class variable without having to initialize it, you can omit the parentheses.

Consider the following program:

using System;

public class Exercise{ public void Welcome() {

Console.WriteLine("The wonderful world of C# programming"); }

public Exercise() {

Console.WriteLine("The Exercise class is now available"); }}

public class Class1{ static void Main() {

Exercise exo = new Exercise(); }}

When executed, it would produce:

The Exercise class is now available

This shows that, when a class has been instantiated, its constructor is the first method to be called. For this reason, you can use a constructor to initialize a class, that is, to assign default values to its member variables. When a constructor is used to initialize a variable declared for a class. That constructor is referred to as an instance constructor.

 

Practical Learning: Constructing a Class

213

Page 214: Introduction to C#

1. Start a new file in Notepad and, in the empty file, type the following: 

using System;

public class Applicant{

public string FullName;public string Address;public string City;public string State;public string ZIPCode;public string Sex;public string DateOfBirth;public int Weight;public string Height;public int Race;

public Applicant(){

this.FullName = "Unknown";this.Sex = "Ungenerated";

}}

public class Exercise{

public static int Main(){

Applicant App = new Applicant();

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Information ---");

Console.WriteLine("Full Name: {0}", App.FullName);Console.WriteLine("Sex: {0}\n",

App.Sex);

return 0;}

}

2. Save the file in a new folder named MVA1 3. Save the file itself as Exercise.cs 4. Switch to the Command Prompt and change the directory to the MVA1 folder that contains the

current file 5. Compile the program at the Command Prompt with

csc Exercise.cs and execute it with Exercise. This would produce: 

-=- Motor Vehicle Administration -=- --- Driver's License Information ---Full Name: Unknown

214

Page 215: Introduction to C#

Sex: Ungenerated

6. Return to Notepad

 

A Constructor With Argument

In the previous section, we saw that there is always a default constructor for a new class that you create; you just the option of explicitly creating one or not. The default constructor as we saw it doesn't take arguments: this is not a rule, it is simply assumed. Instead of a default constructor, you may want to create a constructor that takes an argument. Here is an example:

public class Quadrilateral{

public Quadrilateral(double side){

}}

With this type of constructor, when you declare an instance of the class, you can use this new constructor to initialize the class. Here is an example:

using System;

public class Quadrilateral{

public Quadrilateral(double side){

}}

public class Class1{

static void Main(){

Quadrilateral Square = new Quadrilateral(6.55);}

}

If you create one constructor for your class and pass at least one argument to that constructor, the automatic default constructor created by the compiler disappears. This implies that if you declare an instance of the class and use the default constructor to initialize it, you would receive an error when you compiler the program. Based on this rule, the following program will not compile:

using System;

public class Quadrilateral{

public Quadrilateral(double side){

}}

215

Page 216: Introduction to C#

public class Class1{

static void Main(){

Quadrilateral Square = new Quadrilateral();}

}

If you still want to use the default constructor in a class after creating a constructor that takes at least one argument, you must explicitly create that default constructor.

 

Practical Learning: Constructing a Class1. To pass an argument to a constructor, change the file as follows:

 

using System;

public class Applicant{

public string FullName;public string Address;public string City;public string State;public string ZIPCode;public string Sex;public string DateOfBirth;public int Weight;public string Height;public int Race;

public Applicant(string n){

this.FullName = n;}

}

public class Exercise{

static Applicant RegisterPersonalInformation(){

string name;char sex;string gender = null;

Console.WriteLine("Applicant's Registration");Console.Write("Full Name: ");name = Console.ReadLine();

do {

Console.Write("Sex(F=Female/M=Male): ");sex = char.Parse(Console.ReadLine());

216

Page 217: Introduction to C#

if( (sex != 'f') && (sex != 'F') && (sex != 'm') && (sex != 'M') )

Console.WriteLine("Please enter a valid character");

}while( (sex != 'f') && (sex != 'F') && (sex != 'm') && (sex != 'M') );

if( (sex == 'f') || sex == 'F' )gender = "Female";

else if( (sex == 'm') || (sex == 'M') )gender = "Male";

Applicant person = new Applicant(name);person.Sex = gender;

return person;}

public static int Main(){

Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine(" --- Driver's License Application ---");

Applicant App = RegisterPersonalInformation();

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Driver's License Information ---");

Console.WriteLine("Full Name: {0}", App.FullName);Console.WriteLine("Sex: {0}\n", App.Sex);

return 0;}

}

2. Save, compile, and execute the program. Here is an example: 

-=- Motor Vehicle Administration -=- --- Driver's License Application ---Applicant's RegistrationFull Name: Ernestine KoullahSex(F=Female/M=Male): GPlease enter a valid characterSex(F=Female/M=Male): F

-=- Motor Vehicle Administration -=- --- Driver's License Information ---Full Name: Ernestine KoullahSex: Female

3. Return to Notepad

217

Page 218: Introduction to C#

Static Constructors

Like the above described instance constructors, a static constructor is used to initialize a class. The main difference is that a static constructor works internally, in the class. Therefore, it is not used to initialize a variable of the class and you can never declare a variable of a class using a static constructor.

To make a constructor static, when creating it, type the static keyword to its left. Here is an example:

using System;

public class Quadrilateral{

static Quadrilateral(){

}}

public class Class1{

static void Main(){

// Use the default constructor to initialize an instance of the class

Quadrilateral Square = new Quadrilateral();}

}

In the above class, a static constructor is created for the class but the default constructor is still available and it is used to instantiate the class.

Constructor Overloading

A constructor is the primary method of a class. It allows the programmer to initialize a variable of a class when the class is instantiated. A constructor that plays this role of initializing an instance of a class is also called an instance constructor. Most of the time, you don't even need to create a constructor, since one is automatically provided to any class you create. Sometimes too, as we have seen in some classes, you need to create your own class as you judge it necessary. And sometimes, a single constructor may not be sufficient. For example, when creating a class, you may decide, or find out, that there must be more than one way for a user to initialize a variable.

Like any other method, a constructor can be overloaded. In other words, you can create a class and give it more than one constructor. The same rules used on overloading regular methods also apply to constructors: the different constructors must have different number of arguments or a different number of arguments.

 

Practical Learning: Overloading a Constructor1. To overload the constructor, change the file as follows:

218

Page 219: Introduction to C#

 

using System;

public class Applicant{

public string FullName;public string Address;public string City;public string State;public string ZIPCode;public string Sex;public string DateOfBirth;public int Weight;public string Height;public int Race;

// The default constructor, used to initialize an // Applicant instance without much informationpublic Applicant(){

this.FullName = "Unknown";this.Sex = "Ungenerated";

}

// A constructor that is passed only one argumentpublic Applicant(string n){

this.FullName = n;}

// A constructor with more than one argument// This type is suitable to completely initialize a variablepublic Applicant(string n, string s, string dob){

this.FullName = n;this.Sex = s;this.DateOfBirth = dob;

}}

public class Exercise{

static Applicant RegisterPersonalInformation(){

string name;char sex;string gender = null;string dob;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");Console.WriteLine("Applicant's Registration");Console.Write("Full Name: ");name = Console.ReadLine();

219

Page 220: Introduction to C#

do {

Console.Write("Sex(F=Female/M=Male): ");sex = char.Parse(Console.ReadLine());

if( (sex != 'f') && (sex != 'F') && (sex != 'm') && (sex != 'M') )

Console.WriteLine("Please enter a valid character");

}while( (sex != 'f') && (sex != 'F') && (sex != 'm') && (sex != 'M') );

if( (sex == 'f') || sex == 'F' )gender = "Female";

else if( (sex == 'm') || (sex == 'M') )gender = "Male";

Console.Write("Date of Birth(mm/dd/yyyy): ");dob = Console.ReadLine();

Applicant person = new Applicant(name, gender, dob);

return person;}

static void Show(Applicant person){

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" --- Applicant's Personal Information ---");

Console.WriteLine("Full Name: {0}", person.FullName);Console.WriteLine("Sex: {0}", person.Sex);Console.WriteLine("Date of Birth: {0}\n",

person.DateOfBirth);}

public static int Main(){

Applicant App = new Applicant();

App = RegisterPersonalInformation();Show(App);

return 0;}

}

2. Save, compile, and test the file. Here is an example: 

-=- Motor Vehicle Administration -=-Applicant's RegistrationFull Name: Dominique MonaySex(F=Female/M=Male): dPlease enter a valid characterSex(F=Female/M=Male): M

220

Page 221: Introduction to C#

Date of Birth(mm/dd/yyyy): 06/10/1972

-=- Motor Vehicle Administration -=- --- Applicant's Personal Information ---Full Name: Dominique MonaySex: MaleDate of Birth: 06/10/1972

3. Return to Notepad

The Destructor of a Class

While a constructor is a method used to initialize an instance of a class, a destructor is used to destruct an instance of class when that variable is not used anymore. Like the constructor, the destructor has the same name as the class. To indicate that the method is a destructor, its name is preceded with a tilde.

Here is an example of a destructor in a class:

using System;

class SampleClass{

// Constructorpublic SampleClass(){

Console.WriteLine("SampleClass - Constructor");}

~SampleClass(){

Console.WriteLine("Destructor of SampleClass");}

}

public class NewProject{

static void Main(){

SampleClass Sample = new SampleClass();

Console.WriteLine();}

}

This would produce:

SampleClass - Constructor

Destructor of SampleClass

Like a (default) constructor, a destructor is automatically created for your class but you can also create it if you want. A class can have only one constructor. If you don't create it, the compiler would create it for your class. If you create it, the compiler would not create another. A destructor cannot have an access level. A destructor is called when the memory that a class

221

Page 222: Introduction to C#

was used is no longer needed. This is done automatically by the compiler. For this reason, you will hardly need to create a constructor, since its job is automatically taken care of behind the scenes by the compiler.

 

 

Previous Copyright © 2004-2005 FunctionX, Inc. Next

 

 

The Properties of a Class 

Overview of Properties 

Introduction

In C++ and Java, when creating member variables of a class, programmers usually "hide" these members in private sections (C++) or create them as private (Java). This technique makes sure that a member variable is not accessed outside the class so that the clients of the class cannot directly influence the value of the member variable. If you create a member variable as private but still want other classes or methods to access or get the value of such a member variable, you must then create one or two methods used as "accessories", like a door in which the external methods or classes must pass through to access the member variable.

A property is a member of a class that acts as an intermediary to a member variable of the class. For example, if you have a member variable of class and that member represents the salary of an

 

222

Page 223: Introduction to C#

employee, a property can be the "door" that other methods or classes that need the salary must present their requests to. As such, these external methods and class cannot just change the salary or retrieve it as they wish. A property can be used to validate their request, to reject or to accept them.

Accessories for Properties

As mentioned already, a property is used to "filter" access to a member variable of a class. Therefore, you start by declaring a (private (if you don't make it private, you may be deceiving the purpose of creating a property)) member variable:

using System;

namespace Geometry{ public class Square {

private double _side; }

public class Applied {

static void Main(){

} }}

Obviously this private member variable cannot be accessed by a method or class outside of its class. Therefore, to let outside classes access this variable, you would/can create a property.  To create a property, start with a member whose formula resembles a method without the parentheses. Here is an example:

using System;

namespace Geometry{ public class Square {

private double _side;

// This is a new property public double Side { } }}

With regards to their role, there are two types of properties.

 

223

Page 224: Introduction to C#

Practical Learning: Introducing Properties1. Start a new file in Notepad

2. From what we know so far, type the following: 

using System;

namespace LamackEnterprises{ public class DepartmentStore {

private string itemNo;private string cat;private string name;private string size;private double price;

}

public class Exercise {

static void Main(){

} }}

3. Save it as exercise.cs in a new folder named DeptStore3 inside of your CSharp Lessons folder

4. To create a property for each member variable, change the DepartmentStore class as follows: 

using System;

namespace LamackEnterprises{ public class DepartmentStore {

private string itemNo;private string cat;private string name;private string size;private double price;

// A property for the stock number of an item public string ItemNumber { }

// A property for the category of item public string Category { }

224

Page 225: Introduction to C#

// A property for an item's name of an item public string ItemName { }

// A property for size of a merchandise public string Size { }

// A property for the marked price of an item public double UnitPrice { } }

public class Exercise {

static void Main(){

} }}

5. Save the file

Types of Properties 

Property Readers

A property is referred to as read if its role is only to make available the value of the member variable it represents. To create a read property, in the body of the property, type the get keyword and create a body for the property, using the traditional curly brackets that delimit a section of code. Here is an example:

using System;

namespace Geometry{ public class Square {

private double _side;

// This is a new property public double Side { get { } } }}

225

Page 226: Introduction to C#

In the body of the get clause, you can implement the behavior that would be used to make the member variable's value available outside. The simplest way consists of just returning the corresponding member variable. Here is an example:

using System;

namespace Geometry{ public class Square {

private double _side;

// This is a new property public double Side { get { return _side; } } }}

A read property is also referred to as a read-only property because the clients of the class can only retrieve the value of the property but they cannot change it. Therefore, if you create (only) a read property, you should provide the users with the ability to primarily specify the value of the member variable. To do this, you can create an accessory method or a constructor for the class . Here is an example of such a constructor:

using System;

namespace Geometry{ public class Square {

private double _side;

// This is a new property public double Side { get { return _side; } }

public Square(double s) { _side = s; } }}

Once a read property has been created, other classes or methods can access it, for example they read its value as follows:

using System;

226

Page 227: Introduction to C#

namespace Geometry{ public class Square {

private double _side;

// This is a new property public double Side { get { return _side; } }

public Square(double s) { _side = s; } }

class Applied {

static void Main(){

Square sq = new Square(-25.55);

Console.WriteLine("Square Side: {0}", sq.Side);

} }}

This would produce:

Square Side: -25.55

Press any key to continue

We described a property as serving as a door from outside to its corresponding member variable, preventing those outside classes or method to alter the member variable. Notice that the Square class was given a negative value for the member variable, which is usually unrealistic for the side of a square. In this case and others, while still protecting the member variable as private, you can use the read property to reset the value of the member variable or even to reject it. To provide this functionality, you can create a conditional statement in the read property to perform a checking process. Here is an example:

using System;

namespace Geometry{ public class Square {

private double _side;

// This is a new property

227

Page 228: Introduction to C#

public double Side { get {

if( _side < 0 ) return 0;

// else is implied return _side; } }

public Square(double s) { _side = s; } }

class Applied {

static void Main(){

Square sq1 = new Square(-12.48); Square sq2 = new Square(25.55);

Console.WriteLine("First Square Characteristics"); Console.WriteLine("Side: {0}\n", sq1.Side);

Console.WriteLine("Second Square Characteristics"); Console.WriteLine("Side: {0}\n", sq2.Side);}

}}

This would produce:

First Square CharacteristicsSide: 0

Second Square CharacteristicsSide: 25.55

 

Practical L

 

228

Page 229: Introduction to C#

Classes Interactions 

Combinations of Classes 

Class Nesting

A class can be created inside of another class. A class created inside of another is referred to as nested:

To nest a class, simply create it as you would any other. Here is an example of a class called Inside that is nested in a class called Outside:

public class Outside{

public class Inside{}

}

In the same way, you can nest as many classes as you wish in another class and you can nest as many classes inside of other nested classes if you judge it necessary. Just as you would manage any other class so can you exercise control on a nested class. For example, you can declare all necessary fields, properties, or methods in the nested class or in the nesting class. When you create one class inside of another, there is no special programmatic relationship between both classes:  just because a class is nested does not mean that the nested class has immediate access to the members of the nesting class. They are two different classes and they can be used separately. 

The name of a nested class is not "visible" outside of the nesting class. To access a nested class outside of the nesting class, you must qualify the name of the nested class anywhere you want to use it. For example, if you want to declare an Inside variable somewhere in the program but outside of Outside, you must qualify its name. Here is an example:

using System;

public class Outside{

public class Inside{

 

229

Page 230: Introduction to C#

public Inside(){

Console.WriteLine(" -= Inside =-");}

}

public Outside(){

Console.WriteLine(" =- Outside -=");}

}

class Exercise{

static void Main(){

Outside Recto = new Outside();Outside.Inside Ins = new Outside.Inside();

}}

This would produce:

=- Outside -= -= Inside =-

Because there is no programmatically privileged relationship between a nested class and its "container" class, if you want to access the nested class in the nesting class, you can use its static members. In other words, if you want, you can declare static all members of the nested class that you want to access in the nesting class. Here is an example:

using System;

public class Outside{

public class Inside{

public static string InMessage;

public Inside(){

Console.WriteLine(" -= Insider =-");InMessage = "Sitting inside while it's raining";

}

public static void Show(){

Console.WriteLine("Show me the wonderful world of C# Programming");

}}

public Outside(){

Console.WriteLine(" =- The Parent -=");}

230

Page 231: Introduction to C#

public void Display(){

Console.WriteLine(Inside.InMessage);Inside.Show();

}}

class Exercise{

static void Main(){

Outside Recto = new Outside();Outside.Inside Ins = new Outside.Inside();

Recto.Display();}

}

In the same way, if you want to access the nesting class in the nested class, you can go through the static members of the nesting class. To do this, you can declare static all members of the nesting class that you want to access in the nested class. Here is an example:

using System;

public class Outside{

public class Inside{

public static string InMessage;

public Inside(){

Console.WriteLine(" -= Insider =-");InMessage = "Sitting inside while it's raining";

}

public static void Show(){

Console.WriteLine("Show me the wonderful world of C# Programming");

}

public void FieldFromOutside(){

Console.WriteLine(Outside.OutMessage);}

}

private static string OutMessage;

public Outside(){

Console.WriteLine(" =- The Parent -=");OutMessage = "Standing outside! It's cold and raining!!";

}

public void Display()

231

Page 232: Introduction to C#

{Console.WriteLine(Inside.InMessage);Inside.Show();

}}

class Exercise{

static void Main(){

Outside Recto = new Outside();Outside.Inside Ins = new Outside.Inside();

Recto.Display();Console.WriteLine();Ins.FieldFromOutside();

}}

This would produce:

=- The Parent -= -= Insider =-Sitting inside while it's rainingShow me the wonderful world of C# Programming

Standing outside! It's cold and raining!!

Instead of static members, if you want to access members of a nested class in the nesting class, you can first declare a variable of the nested class in the nesting class. In the same way, if you want to access members of a nesting class in the nested class, you can first declare a variable of the nesting class in the nested class. Here is an example:

using System;

public class Outside{

// A member of the nesting classprivate string OutMessage;

// The nested classpublic class Inside{

// A field in the nested classpublic string InMessage;

// A constructor of the nested classpublic Inside(){

Console.WriteLine(" -= Insider =-");this.InMessage = "Sitting inside while it's raining";

}

// A method of the nested classpublic void Show(){

// Declare a variable to access the nesting class

232

Page 233: Introduction to C#

Outside outsider = new Outside();Console.WriteLine(outsider.OutMessage);

}} // End of the nested class

// A constructor of the nesting classpublic Outside(){

this.OutMessage = "Standing outside! It's cold and raining!!";

Console.WriteLine(" =- The Parent -=");}

// A method of the nesting classpublic void Display(){

Console.WriteLine(insider.InMessage);}

// Declare a variable to access the nested classInside insider = new Inside();

}

class Exercise{

static void Main(){

Outside Recto = new Outside();Outside.Inside Ins = new Outside.Inside();

Ins.Show();Recto.Display();

}}

This would produce:

-= Insider =- =- The Parent -= -= Insider =- -= Insider =- =- The Parent -=Standing outside! It's cold and raining!!Sitting inside while it's raining

A Class as a Field

Just like any of the variables we have used so far, you can make a class or a structure a member variable, also called a field, of another class. To use a class in your own class, of course you must have that class. You can use one of the classes already available in C# or you can first create your own class. Here is an example of a class:

public class MVADate{ private int DayHired; private int MonthHired; private int YearHired;

233

Page 234: Introduction to C#

}

A field is a member variable created from another class instead of primitive types. To use a class as a member of your class, simply declare its variable as you would proceed with any of the member variables we have declared so far. Here is an example:

using System;

public class MVADate{ private int DayHired; private int MonthHired; private int YearHired;}

public class MotorVehicleAdministration{ MVADate MDate;

static int Main() {

return 0; }}

After a class has been declared as a member variable of another class, it can be used regularly. Because the member is a class, declared as a reference (in (Managed) C++, it would be declared as a pointer) instead of a value type, there are some rules you must follow to use it. After declaring the member variable, you must make sure you have allocated memory for it on the heap. You must also make sure that the variable is initialized appropriately before it can be used; otherwise you would receive an error when compiling the file.

Imagine you declare a variable of class A as a member of class B. If you declare a variable of class B in an external method, as we have done so far in Main(), to access a member of class A, you must fully qualify it.

 

Practical Learning: Declaring a Class as a Member Variable

1. Start a new file in Notepad and, in the empty file, type the following: 

using System;

public class MVADate{ private int dayOfBirth; private int monthOfBirth; private int yearOfBirth;

public void SetDate(int d, int m, int y) {

dayOfBirth = d;monthOfBirth = m;yearOfBirth = y;

}

234

Page 235: Introduction to C#

public string ProduceDate() {

string result = dayOfBirth + "/" + monthOfBirth + "/" + yearOfBirth;

return result; }}

public class MotorVehicleAdministration{ public MotorVehicleAdministration() {

birthdate = new MVADate(); }

private string fullName; private MVADate birthdate; private bool isAnOrganDonor;

static int Main() {

MotorVehicleAdministration MVA = new MotorVehicleAdministration(); Console.WriteLine("To process a registration, enter the information"); Console.Write("Full Name: ");

MVA.fullName = Console.ReadLine();Console.Write("Day of Birth: ");

int d = int.Parse(Console.ReadLine());Console.Write("Month of Birth: ");int m = int.Parse(Console.ReadLine());Console.Write("Year of Birth: ");int y = int.Parse(Console.ReadLine());

Console.Write("Is the application an organ donor (0=No/1=Yes)? "); string ans = Console.ReadLine();

if( ans == "0" ) MVA.isAnOrganDonor = false; else MVA.isAnOrganDonor = true;

MVA.birthdate.SetDate(d, m, y);

Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); Console.WriteLine(" -=- Driver's License Application -=-");

Console.WriteLine("Full Name: {0}", MVA.fullName);Console.WriteLine("Dateof Birth: {0}",

MVA.birthdate.ProduceDate());Console.WriteLine("Organ Donor? {0}", MVA.isAnOrganDonor);

Console.WriteLine();

235

Page 236: Introduction to C#

return 0; }}

2. Save the file as exercise.cs in a new folder called MVA2 created inside your CSharp Lessons folder

3. Open the Command Prompt and switch to the folder that contains the current exercise

4. Compile the program with csc exercise.cs

5. Execute it by typing exerciseHere is an example: 

C:\CSharp Lessons\MVA2>csc exercise.csMicrosoft (R) Visual C# .NET Compiler version 7.10.3052.4for Microsoft (R) .NET Framework version 1.1.4322Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

C:\CSharp Lessons\MVA2>exerciseTo process a registration, enter the informationFull Name: Ernestine MvouamaDay of Birth: 8Month of Birth: 14Year of Birth: 1984Is the application an organ donor (0=No/1=Yes)? 1

-=- Motor Vehicle Administration -=- -=- Driver's License Application -=-Full Name: Ernestine MvouamaDateof Birth: 8/14/1984Organ Donor? True

6. Return to Notepad

 

Classes and External Methods 

Returning a Class From a Method

Like a value from regular type, you can return a class object from a method of a class. To do this, you can first declare the method and specify the class as the return type. Here is an example:

using System;

public class MVADate{}

public class MotorVehicleAdministration{ private static MVADate RequestDateOfBirth()

236

Page 237: Introduction to C#

{ }

static int Main() {

MotorVehicleAdministration MVA = new MotorVehicleAdministration();

Console.WriteLine(); return 0; }}

After implementing the method, you must return a value that is conform to the class, otherwise you would receive an error when compiling the application. You can proceed by declaring a variable of the class in the body of the method, initializing the variable, and then returning it.

Once a method has returned a value of a class, the value can be used as normally as possible.

 

Practical Learning: Returning a Class From a Method

1. Change the file as follows: 

using System;

public class MVADate{ private int dayOfBirth; private int monthOfBirth; private int yearOfBirth;

public void SetDate(int d, int m, int y) {

dayOfBirth = d;monthOfBirth = m;yearOfBirth = y;

}

public string ProduceDate() {

string result = dayOfBirth + "/" + monthOfBirth + "/" + yearOfBirth;

return result; }}

public class MotorVehicleAdministration{ public MotorVehicleAdministration() {

birthdate = new MVADate(); }

237

Page 238: Introduction to C#

private string fullName; private MVADate RequestDateOfBirth(); private bool isAnOrganDonor;

private static MVADate RequestDateOfBirth() {

MVADate date = new MVADate();

Console.Write("Day of Birth: ");int d = int.Parse(Console.ReadLine());Console.Write("Month of Birth: ");int m = int.Parse(Console.ReadLine());Console.Write("Year of Birth: ");int y = int.Parse(Console.ReadLine());

date.SetDate(d, m, y);return date;

}

static int Main() {

MotorVehicleAdministration MVA = new MotorVehicleAdministration(); Console.WriteLine("To process a registration, enter the information"); Console.Write("Full Name: ");

MVA.fullName = Console.ReadLine();

MVA.birthdate = RequestDateOfBirth();

Console.Write("Is the application an organ donor (0=No/1=Yes)? ");

string ans = Console.ReadLine();

if( ans == "0" ) MVA.isAnOrganDonor = false;else

MVA.isAnOrganDonor = true;

Console.WriteLine("\n -=- Motor Vehicle Administration -=-"); Console.WriteLine(" -=- Driver's License Application -=-");

Console.WriteLine("Full Name: {0}", MVA.fullName);Console.WriteLine("Dateof Birth: {0}",

MVA.birthdate.ProduceDate());Console.WriteLine("Organ Donor? {0}", MVA.isAnOrganDonor);

Console.WriteLine(); return 0; }}

2. Save it and switch to the Command Prompt

3. Compile and execute the application

238

Page 239: Introduction to C#

 

Passing an Object as Argument

Once a class has been created, it can be used like any other variable. For example, its variable can be passed as argument to a method of another class. When a class is passed as argument, its public members are available to the method that uses it. Here is an example:

class CarRegistration{ public void DisplayDriversLicense(MotorVehicleAdministration mva) {

Console.WriteLine("Full Name: {0}", mva.fullName);Console.WriteLine("Dateof Birth: {0}", mva.birthdate.ProduceDate());Console.WriteLine("Organ Donor? {0}", mva.isAnOrganDonor);

}}

public class MotorVehicleAdministration{ public string fullName; public MVADate birthdate; public bool isAnOrganDonor;}

In the same way, you can pass more than one class as arguments to a method. Because classes are always used by reference, when passing a class as argument, it is implied to be passed by reference. To reinforce this, you can type the ref keyword to the left of the argument. Here is an example:

class CarRegistration{ public void DisplayDriversLicense(ref MotorVehicleAdministration mva) {

Console.WriteLine("Full Name: {0}", mva.fullName);Console.WriteLine("Dateof Birth: {0}", mva.birthdate.ProduceDate());Console.WriteLine("Organ Donor? {0}", mva.isAnOrganDonor);

}}

 

Practical Learning: Passing a Class as Argument

1. To pass a class as argument, change the file as follows: 

using System;

namespace ConsoleApplication1{

public class MVADate{

private int dayOfBirth;private int monthOfBirth;private int yearOfBirth;

239

Page 240: Introduction to C#

public void SetDate(int d, int m, int y){

dayOfBirth = d;monthOfBirth = m;yearOfBirth = y;

}

public string ProduceDate(){

string result = dayOfBirth + "/" + monthOfBirth + "/" + yearOfBirth;

return result;}

}

class Car{

public string Make;public string Model;public int CarYear;

}

class CarRegistration{

public void DisplayDriversLicense(MotorVehicleAdministration mva){

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

Console.WriteLine(" -=- Car Registration -=-");Console.WriteLine("Full Name: {0}", mva.fullName);Console.WriteLine("Dateof Birth: {0}",

mva.birthdate.ProduceDate());Console.WriteLine("Organ Donor? {0}", mva.isAnOrganDonor);

}

public void DisplayCarInformation(Car v){

Console.WriteLine("Car: {0} {1}", v.Make, v.Model);Console.WriteLine("Year: {0}", v.CarYear);

}}

public class MotorVehicleAdministration{

public MotorVehicleAdministration(){

birthdate = new MVADate();}

public string fullName;public MVADate birthdate;public bool isAnOrganDonor;public Car NewCar;

240

Page 241: Introduction to C#

private static MVADate RequestDateOfBirth(){

MVADate date = new MVADate();

Console.Write("Day of Birth: ");int d = int.Parse(Console.ReadLine());Console.Write("Month of Birth: ");int m = int.Parse(Console.ReadLine());Console.Write("Year of Birth: ");int y = int.Parse(Console.ReadLine());

date.SetDate(d, m, y);return date;

}

private static Car RegisterCar(){

Car c = new Car();

Console.Write("Make: ");c.Make = Console.ReadLine();Console.Write("Model: ");c.Model = Console.ReadLine();Console.Write("Year: ");c.CarYear = int.Parse(Console.ReadLine());

return c;}

static void Main(){

MotorVehicleAdministration MVA = new MotorVehicleAdministration();

CarRegistration regist = new CarRegistration();Car vehicle = new Car();

Console.WriteLine("To process a registration, enter the

information");Console.Write("Full Name: ");MVA.fullName = Console.ReadLine();MVA.birthdate = RequestDateOfBirth();Console.Write("Is the application an organ donor

(0=No/1=Yes)? ");string ans = Console.ReadLine();

if( ans == "0" )MVA.isAnOrganDonor = false;

elseMVA.isAnOrganDonor = true;

Console.WriteLine("Enter the following information for Car Registration");

MVA.NewCar = RegisterCar();

regist.DisplayDriversLicense(MVA);regist.DisplayCarInformation(MVA.NewCar);

241

Page 242: Introduction to C#

Console.WriteLine();}

}}

2. Save it and switch to the Command Prompt

3. Compile and execute the application. Here is an example: 

To process a registration, enter the informationFull Name: Alan ScottDay of Birth: 2Month of Birth: 10Year of Birth: 1974Is the application an organ donor (0=No/1=Yes)? 0Enter the following information for Car RegistrationMake: FordModel: Crown VictoriaYear: 1998

-=- Motor Vehicle Administration -=- -=- Car Registration -=-Full Name: Alan ScottDateof Birth: 2/10/1974Organ Donor? FalseCar: Ford Crown VictoriaYear: 1998

4. Return to Notepad

 

Previous Copyright © 2004-2006 FunctionX, Inc. Next

 

Inheritance 

Introduction to Inheritance 

Definition 

242

Page 243: Introduction to C#

Volley Ball Football Basketball Handball Golf

The primary characteristic of the objects on the above pictures is that they are balls used in different sports. Another characteristic they share is that they are round. Although these balls are used in sport, one made for one sport cannot (or should not) be used in another sport (of course, it is not unusual for a footballer to mess with a volley ball on a lawn but it is not appropriate). The common characteristics of these objects can be listed in a group like a C# class. The class would appear as:

class Ball{ TypeOfSport; Size;}

If you were asked to create a class to represent these balls, you may be tempted to implement a general class that defines each ball. This may be a bad idea because, despite their round resemblance, there are many internal differences among these balls. Programming languages like C# provide an alternate solution to this type of situation.

Inheritance consists of creating a class whose primary definition or behavior is based on another class. In other words, inheritance starts by having a class that can provide behavior that other classes can improve on.

 

Practical Learning: Introducing Inheritance

1. Start Notepad and, in the empty file, type the following: 

using System;

public class Applicant{

private string name;private string addr;private string ct;private string stt;private string ZIP;private string sex;private string dob;private int wgt;private string hgt;private int race;

public Applicant(){}

 

243

Page 244: Introduction to C#

public Applicant(string n){

this.name = n;}

public Applicant(string n, string s){

this.name = n;this.sex = s;

}

public string FullName{

get { return name; }set { name = value; }

}

public string Gender{

get { return sex; }set { sex = value; }

}

public void Registration(){

char s;

Console.Write("Full Name: ");this.name = Console.ReadLine();

do {

Console.Write("Sex(F=Female/M=Male): ");s = char.Parse(Console.ReadLine());

if( (s != 'f') && (s != 'F') && (s != 'm') && (s != 'M') )

Console.WriteLine("Please enter a valid character");

sex = "Male";}while( (s != 'f') && (s != 'F') && (s != 'm') && (s !=

'M') );

if( (s == 'f') || s == 'F' )sex = "Female";

else if( (s == 'm') || (s == 'M') )sex = "Male";

}

public void Show(){

Console.WriteLine("Full Name: {0}", this.name);Console.WriteLine("Sex: {0}", this.sex);

}}

244

Page 245: Introduction to C#

public class Exercise{

public static int Main(){

Applicant person = new Applicant();

Console.WriteLine(" -=- Motor Vehicle Administration -=-");person.Registration();

Console.WriteLine("\n -=- Motor Vehicle Administration -=-");

person.Show();

Console.WriteLine();return 0;

}}

2. Save the file in a new folder named MVA3

3. Save the file itself as Exercise.cs in the MVA3 folder

4. Open the Command Prompt and change to the MVA3 folder

5. Compile the program by typing csc Exercise.cs and press Enter

6. Execute the program by typing Exercise and press Enter. Here is an example: 

-=- Motor Vehicle Administration -=-Full Name: Alexis LangaSex(F=Female/M=Male): PPlease enter a valid characterSex(F=Female/M=Male): F

-=- Motor Vehicle Administration -=-Full Name: Alexis LangaSex: Female

7. Return to Notepad

Class Derivation

As you may have guess, in order to implement inheritance, you must first have a class that provides the fundamental definition or behavior you need. There is nothing magical about such a class. It could appear exactly like any of the classes we have used so far. Here is an example:

using System;

class Circle{

private double _radius;

public double Radius{

get{

245

Page 246: Introduction to C#

if( _radius < 0 )return 0.00;

elsereturn _radius;

}set{

_radius = value;}

}public double Diameter{

get{

return Radius * 2;}

}public double Circumference{

get{

return Diameter * 3.14159;}

}public double Area{

get{

return Radius * Radius * 3.14159;}

}}

class Exercise{

public static int Main(){

Circle c = new Circle();c.Radius = 25.55;

Console.WriteLine("Circle Characteristics");Console.WriteLine("Side: {0}", c.Radius);Console.WriteLine("Diameter: {0}", c.Diameter);Console.WriteLine("Circumference: {0}", c.Circumference);Console.WriteLine("Area: {0}", c.Area);

return 0;}

}

This would produce:

Circle CharacteristicsSide: 25.55Diameter: 51.1Circumference: 160.535249Area: 2050.837805975

246

Page 247: Introduction to C#

Press any key to continue

The above class is used to process a circle. It can request or provide a radius. It can also calculate the circumference and the area of a circle. Now, suppose you want to create a class for a sphere. You could start from scratch as we have done so far. On the other hand, since a sphere is primarily a 3-dimensional circle, and if you have a class for a circle already, you can simply create your sphere class that uses the already implemented behavior of a circle class.

Creating a class that is based on another class is also referred to as deriving a class from another. The first class serves as parent or base. The class that is based on another class is also referred to as child or derived. To create a class based on another, you use the following formula:

class NewChild : Base{ // Body of the new class}

In this formula, you start with the class keyword followed by a name from your class. On the right side of the name of your class, you must type the : operator, followed by the name of the class that will serve as parent. Of course, the Base class must have been defined; that is, the compiler must be able to find its definition. Based on the above formula, you can create a sphere class based on the earlier mentioned Circle class as follows:

class Sphere : Circle{ // The class is ready}

After deriving a class, it becomes available and you can use it just as you would any other class. Here is an example:

using System;

class Circle{

private double _radius;

public double Radius{

get{

if( _radius < 0 )return 0.00;

elsereturn _radius;

}set{

_radius = value;}

}public double Diameter{

get{

return Radius * 2;}

247

Page 248: Introduction to C#

}public double Circumference{

get{

return Diameter * 3.14159;}

}public double Area{

get{

return Radius * Radius * 3.14159;}

}}

class Sphere : Circle{}

class Exercise{

public static int Main(){

Circle c = new Circle();c.Radius = 25.55;

Console.WriteLine("Circle Characteristics");Console.WriteLine("Side: {0}", c.Radius);Console.WriteLine("Diameter: {0}", c.Diameter);Console.WriteLine("Circumference: {0}", c.Circumference);Console.WriteLine("Area: {0}", c.Area);

Sphere s = new Sphere();s.Radius = 25.55;

Console.WriteLine("\nSphere Characteristics");Console.WriteLine("Side: {0}", s.Radius);Console.WriteLine("Diameter: {0}", s.Diameter);Console.WriteLine("Circumference: {0}", s.Circumference);Console.WriteLine("Area: {0}", s.Area);

return 0;}

}

This would produce:

Circle CharacteristicsSide: 25.55Diameter: 51.1Circumference: 160.535249Area: 2050.837805975

Sphere CharacteristicsSide: 25.55

248

Page 249: Introduction to C#

Diameter: 51.1Circumference: 160.535249Area: 2050.837805975Press any key to continue

When a class is based on another class, all public (we will also introduce another inheritance-oriented keyword for this issue) members of the parent class are made available to the derived class that can use them as easily. While other methods and classes can also use the public members of a class, the difference is that the derived class can call the public members of the parent as if they belonged to the derived class. That is, the child class doesn't have to "qualify" the public members of the parent class when these public members are used in the body of the derived class. This is illustrated in the following program:

using System;

class Circle{

private double _radius;

public double Radius{

get{

if( _radius < 0 )return 0.00;

elsereturn _radius;

}set{

_radius = value;}

}public double Diameter{

get{

return Radius * 2;}

}public double Circumference{

get{

return Diameter * 3.14159;}

}public double Area{

get{

return Radius * Radius * 3.14159;}

}

public void ShowCharacteristics(){

249

Page 250: Introduction to C#

Console.WriteLine("Circle Characteristics");Console.WriteLine("Side: {0}", Radius);Console.WriteLine("Diameter: {0}", Diameter);Console.WriteLine("Circumference: {0}", Circumference);Console.WriteLine("Area: {0}", Area);

}}

class Sphere : Circle{

public void ShowCharacteristics(){

// Because Sphere is based on Circle, you can access// any public member(s) of Circle without qualifying it(them)Console.WriteLine("\nSphere Characteristics");Console.WriteLine("Side: {0}", Radius);Console.WriteLine("Diameter: {0}", Diameter);Console.WriteLine("Circumference: {0}", Circumference);Console.WriteLine("Area: {0}\n", Area);

}}

class Exercise{

public static int Main(){

Circle c = new Circle();

c.Radius = 25.55;c.ShowCharacteristics();

Sphere s = new Sphere();

s.Radius = 25.55;s.ShowCharacteristics();

return 0;}

}

This would produce the same result.

 

Practical Learning: Inheriting

1. To derive a class, change the file as follows: 

using System;

public enum TypeOfIDCardApplication{

NewCard = 1,Replacement,Correction,

250

Page 251: Introduction to C#

Renewal,Transfer

}

public class Applicant{

private string name;private string addr;private string ct;private string stt;private string ZIP;private string sex;private string dob;private int wgt;private string hgt;private int race;

public Applicant(){}

public Applicant(string n){

this.name = n;}

public Applicant(string n, string s){

this.name = n;this.sex = s;

}

public string FullName{

get { return name; }set { name = value; }

}

public string Gender{

get { return sex; }set { sex = value; }

}

public void Registration(){

char s;

Console.Write("Full Name: ");this.name = Console.ReadLine();

do {

Console.Write("Sex(F=Female/M=Male): ");s = char.Parse(Console.ReadLine());

251

Page 252: Introduction to C#

if( (s != 'f') && (s != 'F') && (s != 'm') && (s != 'M') )Console.WriteLine("Please enter a valid character");

sex = "Male";}while( (s != 'f') && (s != 'F') && (s != 'm') && (s != 'M') );

if( (s == 'f') || s == 'F' )sex = "Female";

else if( (s == 'm') || (s == 'M') )sex = "Male";

}

public void Show(){

Console.WriteLine("Full Name: {0}", this.name);Console.WriteLine("Sex: {0}", this.sex);

}}

public class IdentityCard : Applicant{

public int ApplicationReason;public int TypeOfAnswer;public string TransferFrom;public string NotesOrComments;

public void CreateIDCard(){

do {

Console.WriteLine("What's the reason you need an Identity Card?");Console.WriteLine("1 - I need a new ID Card");Console.WriteLine("2 - I need to replace my ID Card");Console.WriteLine("3 - I need a correction on my ID Card");Console.WriteLine("4 - I want to renew my current ID Card");Console.WriteLine("5 - I want to transfer my ID Card");Console.Write("Your Choice: ");ApplicationReason = int.Parse(Console.ReadLine());

if( ApplicationReason < 1 || ApplicationReason > 5 )Console.WriteLine("\nPlease enter a valid number between 1 and 5");

} while(ApplicationReason < 1 || ApplicationReason > 5);

this.Registration();

switch((TypeOfIDCardApplication)ApplicationReason){

case TypeOfIDCardApplication.NewCard:break;

case TypeOfIDCardApplication.Replacement:do{

Console.WriteLine("What is the reason you want a replacement?");Console.WriteLine("1 - I lost my ID Card");Console.WriteLine("2 - My ID Card was stolen");

252

Page 253: Introduction to C#

Console.WriteLine("3 - My ID Card is damaged");Console.WriteLine("4 - The court/a judge ordered that I replace my ID");Console.Write("Your Choice: ");TypeOfAnswer = int.Parse(Console.ReadLine());if( TypeOfAnswer < 1 || TypeOfAnswer > 4 )

Console.WriteLine("Please enter the correct answer");} while( TypeOfAnswer < 1 || TypeOfAnswer > 4 );

Console.WriteLine("Write the applican't description");NotesOrComments = Console.ReadLine();break;

case TypeOfIDCardApplication.Correction:do{

Console.WriteLine("What is the reason you want a correction?");Console.WriteLine("5 - There is an error on the card");Console.WriteLine("6 - The picture (only the picture) on it is not

wright");Console.WriteLine("7 - I got married");Console.WriteLine("8 - I got a divorce");Console.Write("Your Choice: ");TypeOfAnswer = int.Parse(Console.ReadLine());if( TypeOfAnswer < 5 || TypeOfAnswer > 8 )

Console.WriteLine("Please enter the correct answer");} while( TypeOfAnswer < 5 || TypeOfAnswer > 8 );

Console.WriteLine("Write the applican't description");NotesOrComments = Console.ReadLine();break;

case TypeOfIDCardApplication.Renewal:break;

case TypeOfIDCardApplication.Transfer:Console.WriteLine("What US state or Canadian province are you transferring your ID

from?");TransferFrom = Console.ReadLine();break;

}}

public void ShowIDCard(){

switch((TypeOfIDCardApplication)ApplicationReason){

case TypeOfIDCardApplication.NewCard:Console.WriteLine("Application for New Identity Card");break;

case TypeOfIDCardApplication.Replacement:Console.WriteLine("Application for Identity Card Replacement");break;

case TypeOfIDCardApplication.Correction:Console.WriteLine("Application for Identity Card Correction");break;

case TypeOfIDCardApplication.Renewal:

253

Page 254: Introduction to C#

Console.WriteLine("Application for Identity Card Renewal");break;

case TypeOfIDCardApplication.Transfer:Console.WriteLine("Application for Identity Card Transfer");Console.WriteLine("Identity Card Transferred From: {0}", TransferFrom);break;

}

Console.WriteLine("Applicant's Information");this.Show();Console.WriteLine("Administration Notes or Comments");Console.WriteLine(NotesOrComments);

}}

public class Exercise{

public static int Main(){

int ReasonToBeHereToday;

Console.WriteLine(" -=- Motor Vehicle Administration -=-");

do {

Console.WriteLine("So what brought you to the Motor Vehicle Department today?");Console.WriteLine("1 - I need an Identity Card");Console.WriteLine("2 - I need a Driver's License");Console.WriteLine("3 - I want to register to vote");Console.Write("Your Choice: ");ReasonToBeHereToday = int.Parse(Console.ReadLine());if( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 )

Console.WriteLine("Please enter a valid number as 1, 2, or 3");} while( ReasonToBeHereToday < 1 || ReasonToBeHereToday > 3 );

switch(ReasonToBeHereToday){

case 1:IdentityCard IDApplicant = new IdentityCard();IDApplicant.CreateIDCard();Console.WriteLine("\n -=- Motor Vehicle Administration -=-");IDApplicant.ShowIDCard();break;

case 2:Console.WriteLine("Come back tomorrow");break;

case 3:Console.WriteLine("Come back in two days");break;

}

Console.WriteLine();return 0;

}

254

Page 255: Introduction to C#

}

2. Save, compile, and test the file. Here is an example: 

-=- Motor Vehicle Admi

 

Polymorphism and Abstraction 

Characteristics of Inheritance 

Namespaces and Inheritance

Imagine you had created a class named Person in a namespace named People as follows:

Source File: Persons.csusing System;

namespace People{

public class Person{

private string _name;private string _gdr;

public Person(){

this._name = "Not Available";this._gdr = "Unknown";

}public Person(string name, string gender){

this._name = name;this._gdr = gender;

}

private string FullName{

get { return _name; }set { _name = value; }

}

private string Gender{

get { return _gdr; }set { _gdr = value; }

}}

 

255

Page 256: Introduction to C#

}

If you decide to derive a class from it, remember that this class belongs to a namespace. To inherit from this class, the compiler will need to know the namespace in which the was created. Class inheritance that involves namespaces relies on qualification, like the calling of the members of a namespace. To derive a class from a class member of a namespace, type the name of the namespace, followed by the period operator ".", and followed by the name of the base namespace. Here is an example:

Source File: StaffMembers.cs using System;

namespace HighSchool{

public class Teacher : People.Person{

private string _pos;

public Teacher(){

this._pos = "Staff Member";}

public Teacher(string pos){

this._pos = pos;}

private string Position{

get { return _pos; }set { _pos = value; }

}}

}

If you need to call the class that was defined in a different namespace, remember to qualify its name with the scope access operator. Here is an example:

Source File: Exercise.cs using System;

class Exercise{

static void Main(){

People.Person man = new People.Person("Hermine Sandt", "Male");HighSchool.Teacher staff = new HighSchool.Teacher("Vice Principal");

Console.WriteLine();}

}

Alternatively, as mentioned in the past, to use the contents of a namespace, prior to calling a member of that namespace, you can type the using keyword followed by the name of the namespace. Here is an example:

256

Page 257: Introduction to C#

Source File: Exercise.csusing System;using People;using HighSchool;

class Exercise{

static void Main(){

Person man = new Person("Hermine Sandt", "Male");Teacher staff = new Teacher("Vice Principal");

Console.WriteLine();}

}

 

Practical Learning: Using Inheritance With Namespaces

1. Open Notepad and type the following: 

using System;

namespace FlatShapes{

class Square{

private double _side;

public Square(){

_side = 0.00;}

public Square(double s){

_side = s;}

}

class Rectangle{

double _length;double _height;

public Rectangle(){

_length = 0.00;_height = 0.00;

}

public Rectangle(double L, double H){

_length = L;_height = H;

257

Page 258: Introduction to C#

}}

}

2. Save the file in a new folder named Shapes1

3. Save the file itself as Regulars.cs

4. Start another instance of Notepad and type the following: 

using System;

class Exercise{

static void Main(){

FlatShapes.Square Sq = new FlatShapes.Square(24.55);}

}

5. Save the file as Exercise.cs in the same Shapes1 folder

6. Open the Command Prompt and switch to the above Shapes1 folder

7. To compile the project, type csc Exercise.cs Regulars.cs and press Enter

8. To execute the project, type Exercise and press Enter

9. Return to Notepad

Protected Members

To maintain a privileged relationship with its children, a parent class can make a set list of members available only to classes derived from it. With this relationship, some members of a parent class have a protected access level. Of course, as the class creator, it is your job to specify this relationship.

To create a member that derived classes only can access, type the protected keyword to its left. Here are examples:

Source File: Persons.csusing System;

namespace People{

public class Person{

private string _name;private string _gdr;

public Person(){

this._name = "Not Available";this._gdr = "Unknown";

}public Person(string name, string gender){

this._name = name;this._gdr = gender;

258

Page 259: Introduction to C#

}

protected string FullName{

get { return _name; }set { _name = value; }

}

protected string Gender{

get { return _gdr; }set { _gdr = value; }

}

public void Show(){

Console.WriteLine("Full Name: {0}", this.FullName);Console.WriteLine("Gender: {0}", this.Gender);

}}

}

You can access protected members only in derived classes. Therefore, if you instantiate a class outside, you can call only public members:

Source File: Exercise.csusing System;

class Exercise{

static void Main(){

People.Person man = new People.Person("Hermine Sandt", "Male");

Console.WriteLine("Staff Member");man.Show();

Console.WriteLine();}

}

This would produce:

Staff MemberFull Name: Hermine SandtGender: Male

Virtual Members

We have just mentioned that you can create a new version of a member in a derived class for a member that already exists in the parent class. After doing this, when you call that member in your program, you need to make sure that the right member gets called, the member in the base class or the equivalent member in the derived class.

When you create a base class, if you anticipate that a certain property or method would need to be

259

Page 260: Introduction to C#

redefined in the derived class, you can indicate this to the compiler. On the other hand, while creating your classes, if you find out that you are customizing a property of method that already existed in the base class, you should let the compiler that you are providing a new version. In both cases, the common member should be created as virtual. To do this, in the base class, type the virtual keyword to the left of the property or method. Based on this, the Area property of our Circle class can be created as follows:

class Circle{

public virtual double Area{

get{

return Radius * Radius * 3.14159;}

}}

In fact, in C#, unlike C++, if you omit the virtual keyword, the compiler would display a warning.

When you derive a class from an abstract, since the methods (if any) of the abstract class were not implemented, you must implement each one of them in the derived class. When customizing virtual members in a derived class, to indicate that a member is already virtual in the base class and that you are defining a new version, type the override keyword to the left of its declaration. For example, the Area property in our Sphere class can be created as follows:

class Sphere : Circle{

public override double Area{

get{

return 4 * Radius * Radius * 3.14159;}

}

public double Volume{

get{

return 4 * 3.14159 * Radius * Radius * Radius;}

}}

In the same way, when implementing an abstract method of a class, type the override keyword to its left.

  

Practical Learning: Using Virtual Members

1. Start a new instance of Notepad and type the following: 

260

Page 261: Introduction to C#

class ShapeDescription{

public virtual string Description(){

string Msg = "A quadrilateral is a geometric figure that has " + "four sides and four angles.";

return Msg;}

}

2. Save the file as Quadrilaterals.cs in the Shapes1 folder

3. Access the Regulars.cs file and override the Description method in the FlatShapes.Square and the FlatShapes.Rectangle classes as follows: 

using System;

namespace FlatShapes{

class Square : ShapeDescription{

private double _side;

public Square(){

_side = 0.00;}

public Square(double s){

_side = s;}

public override string Description(){

// Get the introduction from the parentstring Introduction = base.Description() +"\nA square is a quadrilateral that has four " +

"equal sides and four right angles";

return Introduction;}

}

class Rectangle : ShapeDescription{

double _length;double _height;

public Rectangle(){

_length = 0.00;_height = 0.00;

}

261

Page 262: Introduction to C#

public Rectangle(double L, double H){

_length = L;_height = H;

}

public override string Description(){

// Get the introduction from the parentstring Introduction = base.Description();

string Msg = Introduction + "\nA rectangle is a quadrilateral that has adjacent " +

"perpendicular sides. This implies that its four " + "angles are right.";

return Msg;}

}}

4. Save the Regulars.cs file

5. To access the new methods, access the Exercise.cs and change it as follows: 

using System;

class Exercise{

static void DisplaySquare(FlatShapes.Square S){

Console.WriteLine("Square Characteristics");Console.WriteLine("Description: {0}", S.Description());

}

static void DisplayRectangle(FlatShapes.Rectangle R){

Console.WriteLine("Rectangle Characteristics");Console.WriteLine("Description: {0}", R.Description());

}

static void Main(){

FlatShapes.Square Sq = new FlatShapes.Square();FlatShapes.Rectangle Rect = new FlatShapes.Rectangle();

Console.WriteLine("========================================");DisplaySquare(Sq);

Console.WriteLine("========================================");DisplayRectangle(Rect);

Console.WriteLine("========================================");

262

Page 263: Introduction to C#

Console.WriteLine();}

}

6. Save the Exercise.cs

7. Switch to the Command Prompt to the Shapes1 folder

8. To compile the project, type csc Exercise.cs Regulars.cs Quadrilaterals.cs and press Enter

9. To execute the project, type Exercise and press Enter. This would produce: 

========================================Square CharacteristicsDescription: A quadrilateral is a geometric figure that has four sides and fourangles.A square is a quadrilateral that has four equal sides and four right angles========================================Rectangle CharacteristicsDescription: A quadrilateral is a geometric figure that has four sides and fourangles.A rectangle is a quadrilateral that has adjacent perpendicular sides. This implies that its four angles are right.========================================

10. Return to Notepad

 

Abstract Classes

In C#, you can create a class whose role is only meant to provide fundamental characteristics for other classes. This type of class cannot be used to declare a variable of the object. Such a class is referred to as abstract. Therefore, an abstract class can be created only to serve as a parent class for others. To create an abstract class, type the abstract keyword to the left of its name. Here is an example:

abstract class Ball{

protected int TypeOfSport;protected string Dimensions;

}

Practical Learning: Creating an Abstract Class

1. To create an abstract class, in the Regulars.cs class, type the abstract keyword at the beginning of the class creation line: 

abstract class ShapeDescription{

public virtual string Description(){

263

Page 264: Introduction to C#

string Msg = "A quadrilateral is a geometric figure that has " +"four sides and four angles.";

return Msg;}

}

2. Save the file

Abstract Properties and Methods

When creating a class that would mainly be used as base for future inheritance, you can create one or more properties and make them abstract. To do this, when creating the property, type the abstract keyword to its left. Because you would not define the property, you can simply type the get keyword and its semi-colon in the body of the property.

A method of a class also can be made abstract. An abstract method can be a member of only an abstract class. If you make a method abstract in a class, you must not implement the method. To create an abstract method, when creating its class, type the abstract keyword to the left of the method's name. End the declaration with a semi-colon and no body for the method since you cannot implement it. Here is an example:

public abstract class Ball{

protected int TypeOfSport;protected string Dimensions;

public abstract CalculateArea();}

In the same way, you can create as many properties and methods as you see fit. You can choose what properties and methods to make abstract. This is important for inheritance.

Practical Learning: Creating an Abstract Property

1. To create an abstract property, access the Quadrilaterals.cs file and change its class as follows: 

abstract class ShapeDescription{

public abstract string Name { get; }

public virtual string Description(){string Msg = "A quadrilateral is a geometric figure that has "

+ "four sides and four angles.";

return Msg;}

}

2. Save the Quadrilaterals.cs file

3. To define the property, open the Regulars.cs file and change it as follows: 

264

Page 265: Introduction to C#

using System;

namespace FlatShapes{

class Square : ShapeDescription{

private double _side;

public Square(){

_side = 0.00;}

public Square(double s){

_side = s;}

public override string Name{

get { return "Square"; }}

public override string Description(){

// Get the introduction from the parentstring Introduction = base.Description() + "A square is a quadrilateral that has four " +

"equal sides and four right angles";

return Introduction;}

}

class Rectangle : ShapeDescription{

double _length;double _height;

public Rectangle(){

_length = 0.00;_height = 0.00;

}

public Rectangle(double L, double H){

_length = L;_height = H;

}

public override string Name{

get { return "Rectangle"; }}

265

Page 266: Introduction to C#

public override string Description(){

// Get the introduction from the parentstring Introduction = base.Description();

string Msg = Introduction + "\nA rectangle is a quadrilateral that has adjacent " +

"perpendicular sides. This implies that its four " +

"angles are right.";return Msg;

}}

}

4. Save the Regulars.cs file

5. To include the new property in the test, access the Exercise.cs file and change it as follows: 

using System;

class Exercise{

static void DisplaySquare(FlatShapes.Square S){

Console.WriteLine("Square Characteristics");Console.WriteLine("Name: {0}", S.Name);Console.WriteLine("Description: {0}", S.Description());

}

static void DisplayRectangle(FlatShapes.Rectangle R){

Console.WriteLine("Rectangle Characteristics");Console.WriteLine("Name: {0}", R.Name);Console.WriteLine("Description: {0}", R.Description());

}

static void Main(){

FlatShapes.Square Sq = new FlatShapes.Square();FlatShapes.Rectangle Rect = new FlatShapes.Rectangle();

Console.WriteLine("========================================");DisplaySquare(Sq);Console.WriteLine("========================================");DisplayRectangle(Rect);Console.WriteLine("========================================");

Console.WriteLine();}

}

6. Save the Exercise.cs

266

Page 267: Introduction to C#

7. To compile the project, type csc Exercise.cs Regulars.cs Quadrilaterals.cs and press Enter

8. To execute the project, type Exercise and press Enter. This would produce: 

========================================Square CharacteristicsName: SquareDescription: A quadrilateral is a geometric figure that has four sides and fourangles.A square is a quadrilateral that has four equal sides and four right angles========================================Rectangle CharacteristicsName: RectangleDescription: A quadrilateral is a geometric figure that has four sides and fourangles.A rectangle is a quadrilateral that has adjacent perpendicular sides. This implies that its four angles are right.========================================

9. Return to Notepad

Sealed Classes

Any of the classes we have used so far in our lessons can be inherited from. If you create a certain class and don't want anybody to derive another class from it, you can mark it as sealed. In other words, a sealed class is one that cannot serve as base for another class.

To mark a class as sealed, type the sealed keyword to its left. Here is an example:

public sealed class Ball{

public int TypeOfSport;public string Dimensions;

}

Interfaces 

Introduction

Imagine you start creating a class and, while implementing or testing it, you find out that this particular class can be instead as a general base that other classes can be derived from. An interface is a special class whose purpose is to serve as a template that actual classes can be based on.

An interface is primarily created like a class: it has a name, a body and can have members. To create an interface, instead of the class keyword, you use the interface keyword. By convention, the name of an interface starts with I. Here is an example:

interface ICourtDimensions

267

Page 268: Introduction to C#

{}

Practical Learning: Introducing Interfaces

1. Access the Quadrilaterals.cs file. To create an interface, change the file as follows: 

interface IQuadrilateral{}

abstract class ShapeDescription{

public abstract string Name { get; }

public virtual string Description(){

string Msg = "A quadrilateral is a geometric figure that has " + "four sides and four angles.";

return Msg;}

}

2. Save the file

The Members of an Interface

As done for a class, the members of an interface are listed in its body. In an interface, you cannot declare fields like those we have used in other classes. Instead, if you want some type of member variable, you can create a property. If you create a property in an interface, you cannot define that property. One of the rules of an interface is that you cannot define any of its members. This is also valid for its properties. Therefore, if you create a property in an interface:

You can indicate that it would be read-only by adding it an empty getter. Here is an example: 

public interface ICourtDimensions{

double Length { get; }}

You can indicate that it would be write-only. Here is an example: 

public interface ICourtDimensions{

double Length { set; }}

You can indicate that it would be used to write values to it and to read values from it. Here is an example: 

public interface ICourtDimensions{

268

Page 269: Introduction to C#

double Length { get; set; }}

In the same way, you can create as many properties as you judge them necessary in an interface. Besides the properties, an interface can also have other types of members such as methods. Here is an example of an interface that has one read-only property named NameOfSport, one read/write property named NumberOfPlayers, and one method named SportCharacteristics:

public interface IBall{

int NumberOfPlayers{

get;set;

}

string NameOfSport{

get;}

void SportCharacteristics();}

Practical Learning: Creating Members of an Interface

1. To create a member int the new interface, access the Quadrilateral.cs file and change it as follows: 

interface IQuadrilateral{

double Area { get; }}

abstract class ShapeDescription{

public abstract string Name { get; }

public virtual string Description(){

string Msg = "A quadrilateral is a geometric figure that has " +

"four sides and four angles.";return Msg;

}}

2. Save the file

An Interface as a Base Class

An interface is mostly used to lay a foundation for other classes. For this reason, it is the prime candidate for class derivation. To derive from an interface, use the same technique we have applied in inheritance so far. Here is an example of a class named SportBall that derives from an interface named ISportType:

269

Page 270: Introduction to C#

public class SportBall : ISportType{

int players;string sport;

}

Just as you can derive a class from an interface, you can create an interface that itself is based on another interface. Here is an example:

public interface ISportType : IBall{

SportCategory Type{

get;}

}

The C# language doesn't allow multiple inheritance which is the ability to create a class based on more than one class. Multiple inheritance is allowed only if the bases are interfaces. To create multiple inheritance, separate the names of interface with a comma. Here is an example:

public interface ISportType : IBall, ICourtDimensions{

SportCategory Type{

get;}

}

You can also involve a class as parent in a multiple inheritance scenario but there must be only one class. Here is an example in which a class called Sports derives from one class and various interfaces:

public interface Sports: Player, IBall, ICourtDimensions{}

Practical Learning: Inheriting From an Interface

1. To inherit an interface from another interface, access the Quadrilateral.cs file and change it as follows: 

interface IQuadrilateral{

double Area { get; }}

abstract class ShapeDescription{

public abstract string Name { get; }

public virtual string Description(){

string Msg = "A quadrilateral is a geometric figure that has " +

"four sides and four angles.";return Msg;

270

Page 271: Introduction to C#

}}

interface IRightAngles : IQuadrilateral{

double Base { get; set; }double Height { get; set; }double Perimeter { get; }

}

2. Save the file

3. To inherit a class from an interface, access the Regulars.cs file and change it as follows: 

using System;

namespace FlatShapes{

class Square : ShapeDescription, IRightAngles{

. . . No Change}

class Rectangle : ShapeDescription, IRightAngles{

. . . No Change}

}

4. Save the file

Implementation of Derived Classes of an Interface

After creating an interface, you can derive other interfaces or other classes from it. If you are deriving other interfaces from an interface, you can just proceed as you see fit. For example, you can add or not add one or more new properties, you can add or not add one or more properties, etc. Here is an example:

Source File: Preparation.cspublic enum SportCategory{

SinglePlayer,Collective,Unknown

}

public interface ICourtDimensions{

double Length { get; set; }double Width { get; set; }

}

public interface IBall{

int NumberOfPlayers

271

Page 272: Introduction to C#

{get;set;

}

string NameOfSport{

get;}

void SportCharacteristics();}

public interface ISportType : IBall, ICourtDimensions{

SportCategory Type{

get;}

}

If you derive a class, from an interface, you must implement all properties that created in the interface. This means that you must define them so that, when a variable is declared of that class, the properties have meaning. In the same way, if you create a class that is based on an interface, you must implement all methods that were declared in the interface. If you derive a class from an interface that itself was derived from another interface, in your class, you must define all properties that were created in the whole lineage and you must implement all methods that were created in the parent and grant-parent interfaces. Here is an example:

Source File: Sport.csusing System;

public class SportBall : ISportType{

int players;string sport;SportCategory _type;double Len;double Wdt;

public SportBall(int nbr, SportCategory tp, string name){

players = nbr;_type = tp;sport = name;

}

public int NumberOfPlayers{

get { return players;}set { players = value;}

}

public string NameOfSport{

get { return sport; }

272

Page 273: Introduction to C#

}

public SportCategory Type{

get { return _type; }}

public double Length{

get { return Len; }set { Len = value; }

}

public double Width{

get { return Wdt; }set { Wdt = value; }

}

public void SportCharacteristics(){

Console.WriteLine("Sport Characteristics");Console.WriteLine("Name of Sport: {0}", NameOfSport);Console.WriteLine("Type of Sport: {0}", Type);Console.WriteLine("# of Players: {0}", NumberOfPlayers);Console.WriteLine("Court Dimensions: {0}m x {1}m", Len, Wdt);

}}

Once the class is ready, you can then use it as you see fit. Here is an example:

Source File: Exercise.csusing System;

class Exercise{

static void Main(){

SportBall volley = new SportBall(6, SportCategory.Collective, "Volley Ball");volley.Length = 18;volley.Width = 9;volley.SportCharacteristics();

Console.WriteLine();

SportBall tennis = new SportBall(1, SportCategory.SinglePlayer, "Table Tennis");tennis.Length = 23.7;tennis.Width = 8.25;tennis.SportCharacteristics();

Console.WriteLine();}

}

This would produce:

Sport Characteristics

273

Page 274: Introduction to C#

Name of Sport: Volley BallType of Sport: Collective# of Players: 6Court Dimensions: 18m x 9m

Sport CharacteristicsName of Sport: Table TennisType of Sport: SinglePlayer# of Players: 1Court Dimensions: 23.7m x 8.25m

Practical Learning: Implementing Derived Members of an Interface

1. To implement the members of an interface, access the Regulars.cs file and change it as follows: 

using System;

namespace FlatShapes{

class Square : ShapeDescription, IRightAngles{

private double _side;

public Square(){

_side = 0.00;}

public Square(double s){

_side = s;}

public override string Name{

get { return "Square"; }}

public override string Description(){

// Get the introduction from the parentstring Introduction = base.Description() +

"A square is a quadrilateral that has four " +

"equal sides and four right angles";

return Introduction;}

public double Base{

get{

274

Page 275: Introduction to C#

return (_side < 0) ? 0.00 : _side;}set{

_side = value;}

}

public double Height{

get{

return (_side < 0) ? 0.00 : _side;}set{

_side = value;}

}

public double Area{

get{

return Base * Base;}

}

public double Perimeter{

get{

return Base * 4;}

}}

class Rectangle : ShapeDescription, IRightAngles{

double _length;double _height;

public Rectangle(){

_length = 0.00;_height = 0.00;

}

public Rectangle(double L, double H){

_length = L;_height = H;

}

public override string Name{

275

Page 276: Introduction to C#

get { return "Rectangle"; }}

public override string Description(){

// Get the introduction from the parentstring Introduction = base.Description();

string Msg = Introduction + "\nA rectangle is a quadrilateral that has adjacent " +

"perpendicular sides. This implies that its four " +

"angles are right.";return Msg;

}

public double Base{

get{

return _length;}set{

_length = value;}

}

public double Height{

get{

return _height;}set{

_height = value;}

}

public double Area{

get{

return Base * Height;}

}

public double Perimeter{

get{

return 2 * (Base + Height);}

276

Page 277: Introduction to C#

}}

}

2. Save the file

3. To test the new properties, access the Exercise.cs file and change it as follows: 

using System;

class Exercise{

static FlatShapes.Square CreateASquare(){

double side;

Console.Write("Enter the side of the square: ");side = double.Parse(Console.ReadLine());

FlatShapes.Square Sqr = new FlatShapes.Square(side);return Sqr;

}

static void DisplaySquare(FlatShapes.Square S){

Console.WriteLine("Square Characteristics");Console.WriteLine("Name: {0}", S.Name);Console.WriteLine("Description: {0}", S.Description());Console.WriteLine("----------------------------");Console.WriteLine("Side: {0}", S.Base);Console.WriteLine("Perimeter: {0}", S.Perimeter);Console.WriteLine("Area: {0}", S.Area);

}

static FlatShapes.Rectangle CreateARectangle(){

double Len, Hgt;

Console.WriteLine("Enter the dimensions of the rectangle");Console.Write("Base: ");Len = double.Parse(Console.ReadLine());Console.Write("Height: ");Hgt = double.Parse(Console.ReadLine());

FlatShapes.Rectangle Recto = new FlatShapes.Rectangle(Len, Hgt);return Recto;

}

static void DisplayRectangle(FlatShapes.Rectangle R){

Console.WriteLine("Rectangle Characteristics");Console.WriteLine("Name: {0}", R.Name);Console.WriteLine("Description: {0}", R.Description());Console.WriteLine("----------------------------");Console.WriteLine("Base: {0}", R.Base);

277

Page 278: Introduction to C#

Console.WriteLine("Height: {0}", R.Height);Console.WriteLine("Perimeter: {0}", R.Perimeter);Console.WriteLine("Area: {0}", R.Area);

}

static void Main(){

FlatShapes.Square Sq = new FlatShapes.Square();FlatShapes.Rectangle Rect = new FlatShapes.Rectangle();

Sq = CreateASquare();Rect = CreateARectangle();

Console.WriteLine("============================");DisplaySquare(Sq);Console.WriteLine("============================");DisplayRectangle(Rect);Console.WriteLine("============================");

Console.WriteLine();}

}

4. To compile the project, type csc Exercise.cs Regulars.cs Quadrilaterals.cs and press Enter

5. To execute the project, type Exercise and press Enter. This would produce: 

Enter the side of the square: 24.55Enter the dimensions of the rectangleBase: 32.25Height: 26.85============================Square CharacteristicsName: SquareDescription: A quadrilateral is a geometric figure that has four sides and fourangles.A square is a quadrilateral that has four equal sides and four right angles----------------------------Side: 24.55Perimeter: 98.2Area: 602.7025============================Rectangle CharacteristicsName: RectangleDescription: A quadrilateral is a geometric figure that has four sides and fourangles.A rectangle is a quadrilateral that has adjacent perpendicular sides. This implies that its four angles are right.----------------------------Base: 32.25Height: 26.85Perimeter: 118.2

278

Page 279: Introduction to C#

Area: 865.9125============================

6. Return to Notepad

 

Previous Copyright © 2004-2005 FunctionX, Inc. Next

 

The .NET Framework and its Classes 

The Object Class 

Introduction

C# was clearly created to improve on C++ and possibly offer a new alternative. To achieve this goal, Microsoft created a huge library to accompany the language. The .NET Framework is a huge library made of various classes and constants you can directly use in your C# application without necessarily explicitly loading an external library. To start, this main library of C# provides a class called Object.

As you may have realized by now, every variable or function in C# (as in Java) must belong to a class, unlike C/C++ where you can have global variables or functions. Therefore, you always have to create at least one class for your application. As such, when you create a class, it automatically inherits its primary characteristics from the parent of all classes: Object.

Practical Learning: Introducing Ancestor Classes

1. Start Notepad and, in the empty file, type the following: 

using System;

 

279

Page 280: Introduction to C#

class Sport{ private double _ballWeight; private int _players; private double _courtLength; private double _courtWidth;

public double BallWeight {

get { return _ballWeight; }set { _ballWeight = value; }

}

public int NumberOfPlayers {

get { return _players; }set { _players = value; }

}

public double CourtLength {

get { return _courtLength; }set { _courtLength = value; }

}

public double CourtWidth {

get { return _courtWidth; }set { _courtWidth = value; }

}}

class Exercise{ static int Main() {

Sport tennis = new Sport();

tennis.BallWeight = 57.50; // gramstennis.NumberOfPlayers = 1; // Singles galmetennis.CourtLength = 23.70; // meterstennis.CourtWidth = 8.23; // meters;

Console.WriteLine("\nGame Characteristics");Console.WriteLine("Ball Weight: {0} grams", tennis.BallWeight);Console.WriteLine("Players on each side: {0}", tennis.NumberOfPlayers);

Console.WriteLine("Court Dimensions(LxW): {0}m X {1}m\n", tennis.CourtLength, tennis.CourtWidth);

return 0; }}

2. Save the file as exercise.cs in a new folder called Inherited created inside your CSharp Lessons folder

3. Open the Command Prompt and switch to the folder that contains the current exercise

280

Page 281: Introduction to C#

4. Compile the program with csc exercise.cs

5. Execute it by typing exerciseHere is an example: 

C:\CSharp Lessons\Inherited>csc exercise.csMicrosoft (R) Visual C# .NET Compiler version 7.10.3052.4for Microsoft (R) .NET Framework version 1.1.4322Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

C:\CSharp Lessons\Inherited>exercise

Game CharacteristicsBall Weight: 57.5 gramsPlayers on each side: 1Court Dimensions(LxW): 23.7m X 8.23m

C:\CSharp Lessons\Inherited>

6. Return to Notepad

 

Equality of Two Class Variables

When you declare and initialize two variables, one of the operations you may want to subsequently perform is to compare their value. To support this operation, the Object class provides its children with a method called Equals. The Equals() method comes in two versions. The first has the following syntax:

public virtual bool Equals(object obj);

This version allows you to call the Equals() method on a declared variable and pass the other variable as argument. Here is an example:

using System;

class BookCollection{ static void Main() {

// First bookint NumberOfPages1 = 422;// Second bookint NumberOfPages2 = 858;// Third bookint NumberOfPages3 = 422;

if( NumberOfPages1.Equals(NumberOfPages2) == true )Console.WriteLine("The first and the second books have the same number of pages");

elseConsole.WriteLine("The first and the second books have different number of pages");

if( NumberOfPages1.Equals(NumberOfPages3) == true )Console.WriteLine("The first and the third books have the same number of pages");

else

281

Page 282: Introduction to C#

Console.WriteLine("The first and the third books have different number of pages"); }}

This would produce:

The first and the second books have different number of pagesThe first and the third books have the same number of pages

The first version of the Object.Equals method is declared as virtual, which means you can override it if you create your own class. The second version of the Object.Equals() method is:

public static bool Equals(object obj2, object obj2);

As a static method, to use it, you can pass the variables of the two classes whose values you want to compare.

In both cases, if the values of the variables are similar, the Equals() method returns true. If they are different, the method returns false. If you are using the Equals() method to compare the variables of two primitive types, the comparison should be straight forward. If you want to use this methods on variables declared from your own class, you should provide your own implementation of this method.

 

Practical Learning: Implementing Equality

1. To create your own implementation of the Equals() method, change the file as follows: 

using System;

class Sport{ private double _ballWeight; private int _players; private double _courtLength; private double _courtWidth;

public double BallWeight {

get { return _ballWeight; }set { _ballWeight = value; }

}

public int NumberOfPlayers {

get { return _players; }set { _players = value; }

}

public double CourtLength {

get { return _courtLength; }set { _courtLength = value; }

282

Page 283: Introduction to C#

}

public double CourtWidth {

get { return _courtWidth; }set { _courtWidth = value; }

}

public override int GetHashCode() {

return base.GetHashCode(); }

public override bool Equals(Object obj) {

Sport sp = (Sport)obj;

if( (_ballWeight == sp._ballWeight) && (_players == sp._players) &&

(_courtLength == sp._courtLength) && (_courtWidth == sp._courtWidth) )

return true;

return false; }}

class Exercise{ static int Main() {

Sport Euro2002 = new Sport();Sport CAN2004 = new Sport();Sport tennis = new Sport();

Euro2002.BallWeight = 435; // gramsEuro2002.NumberOfPlayers = 11; // persons for each teamEuro2002.CourtLength = 100; // metersEuro2002.CourtWidth = 60; // meters

tennis.BallWeight = 57.50; // gramstennis.NumberOfPlayers = 1; // Singles gametennis.CourtLength = 23.70; // meterstennis.CourtWidth = 8.23; // meters;

CAN2004.BallWeight = 435; // gramsCAN2004.NumberOfPlayers = 11; // persons for each teamCAN2004.CourtLength = 100; // metersCAN2004.CourtWidth = 60; // meters

if( CAN2004.Equals(tennis) == true ) Console.WriteLine("The CAN2004 and the tennis variables are equal");

elseConsole.WriteLine("The Euro2002 and the tennis variables are not equal");

283

Page 284: Introduction to C#

if( Euro2002.Equals(CAN2004) == true ) Console.WriteLine("The Euro2002 and CAN2004 variables are equal");

else Console.WriteLine("The Euro2002 and CAN2004 variables are not equal");

return 0; }}

2. Save the file and switch to the Command Prompt

3. Compile and test the application. This would produce: 

The Euro2002 and the tennis variables are not equalThe Euro2002 and CAN2004 variables are equal

4. Return to Notepad

Stringing a Class

In previous lessons, we learned that, to convert the value of a variable declared from a primitive type to a string, you could call the ToString() function. Here is an example:

using System;

class BookCollection{ static int Main() {

int NumberOfPages = 422;

Console.WriteLine("Number of Pages: {0}", NumberOfPages.ToString()); }}

In many programming languages such as C++, programmers usually have to overload an (extractor) operator to display the value(s) of class' variable to the screen. The Object class provides an alternative to this somewhat complicated solution, through the ToString() method. It syntax is:

public virtual string ToString();

Although the Object class provides this method as non abstract, its implemented version is more useful if you use a primitive type such as int, double and their variances or a string variable. The best way to rely on it consists of overriding it in your own class if you desired to use its role.

 

Practical Learning: Converting to String

1. To implement and use a ToString() method, change the file as follows: 

284

Page 285: Introduction to C#

using System;

class Sport{ private double _ballWeight; private int _players; private double _courtLength; private double _courtWidth;

public double BallWeight {

get { return _ballWeight; }set { _ballWeight = value; }

}

public int NumberOfPlayers {

get { return _players; }set { _players = value; }

}

public double CourtLength {

get { return _courtLength; }set { _courtLength = value; }

}

public double CourtWidth {

get { return _courtWidth; }set { _courtWidth = value; }

}

public override int GetHashCode() {

return base.GetHashCode(); }

public override bool Equals(Object obj) {

Sport sp = (Sport)obj;

if( (_ballWeight == sp._ballWeight) && (_players == sp._players) &&

(_courtLength == sp._courtLength) && (_courtWidth == sp._courtWidth) )

return true;

return false; }

public override string ToString() { string person = null;

if( NumberOfPlayers.Equals(1) )

285

Page 286: Introduction to C#

person = " person"; else person = " persons";

string result = "\nBall Weight: " + BallWeight + " grams" + "\nPlayers on each side: " + NumberOfPlayers + person + "\nCourt Dimensions(LxW): " + CourtLength + "m X " + CourtWidth + "m";

return result; }}

class Exercise{ static int Main() {

Sport CAN2004 = new Sport();Sport tennis = new Sport();

tennis.BallWeight = 57.50; // gramstennis.NumberOfPlayers = 1; // Singles gametennis.CourtLength = 23.70; // meterstennis.CourtWidth = 8.23; // meters;

CAN2004.BallWeight = 435; // gramsCAN2004.NumberOfPlayers = 11; // persons for each teamCAN2004.CourtLength = 100; // metersCAN2004.CourtWidth = 60; // meters

Console.Write("\nCup Game Characteristics"); Console.WriteLine(CAN2004);

Console.WriteLine();

Console.WriteLine("\nTennis Game Characteristics"); Console.WriteLine(tennis);

return 0; }}

2. Save the file and switch to the Command Prompt

3. Compile and test the application. This would produce: 

Cup Game CharacteristicsBall Weight: 435 gramsPlayers on each side: 11 personsCourt Dimensions(LxW): 100m X 60m

Tennis Game CharacteristicsBall Weight: 57.5 gramsPlayers on each side: 1 personCourt Dimensions(LxW): 23.7m X 8.23m

286

Page 287: Introduction to C#

4. Return to Notepad

Finalizing a Variable

While a constructor, created for each class, is used to instantiate a class. The Object class provides the Finalize() method as a type of destructor.

 

Built-In Assemblies and Classes 

Introduction

The System namespace provides one of the largest definition of classes of the .NET Framework, but it doesn't contain everything. For example, when you start writing graphical user interface (GUI) applications, you will have to use other namespaces. The namespaces are contained in libraries called assemblies. The actual classes used in various applications are created and defined in these libraries. Before using a class, you must know the name of the assembly in which it is defined. You must also know the name of its namespace. These three pieces of information, the name of the class, the namespace in which it is defined, and the name of the assembly in which the namespace is contained, are very important. Because there are so many classes, namespaces, and libraries, the MSDN documentation is your best reference. We can only mention a few, especially those that are relevant for the subjects we are reviewing.

Microsoft Visual Basic Functions

One of the strengths of Visual Basic, from its beginning, was its huge library of functions. Unfortunately, even when Visual Basic was part of the Visual Studio 6.0 environment, its functions belonged only to it and to its child languages such as VBA and VBScript. When Visual Studio .NET was created, the developers of Visual Basic added all of its valuable functions and in fact made them available to the other languages that use the .NET Framework. This means that those wonderful functions are available to use in your C# programs.

The functions of Microsoft Visual Basic still belong to it and they can be called transparently in a Visual Basic application. If you want to use them in a non-Visual Basic application, you must remember to reference its library. Most (if not all) of the functions of Visual Basic are created in the Microsoft.VisualBasic.dll library but they might be in different namespaces. Based on this, you can include any Visual Basic function in your program. Here is an example:

Source File: Exercise.csusing System;

class Exercise{ static void Main() {

double Number;double Result;

Console.Write("Enter a number: ");

287

Page 288: Introduction to C#

string strNbr = Console.ReadLine();

if( !Microsoft.VisualBasic.Information.IsNumeric(strNbr) ) Number = 0.00;else Number = Microsoft.VisualBasic.Conversion.Val(strNbr);

Result = Number * 2;

Console.WriteLine("{0} * 2 = {1}", Number, Result); }}

When compiling the program, you must reference the Microsoft.VisualBasic.dll library. Here is an example:

csc /reference:Microsoft.VisualBasic.dll Exercise.cs

 

Custom Libraries 

Introduction

If the .NET Framework doesn't have a class you are looking for, you can create one and be able to use it over and over again in different programs. You can even create a commercial class and be able to distribute or sell it. To make this possible, you can "package" one or more classes in a library. A library is a program that contains classes and/or other resources that other programs can use. Such a program is created with the same approach as the programs we have done so far. Because a library is not an executable, it doesn't need the Main() function. A library usually has the extension .dll.

 

Creating a Library

A library can be made of a single file or as many files as necessary. A file that is part of a library can contain one or more classes. Each class should implement a behavior that can eventually be useful and accessible to other classes. The classes in a library are created exactly like those we have used so far. Everything depends on how you compile it.

To create a library, start by typing its code in a text file. Once the library is ready, to compile it, at the Command Prompt, you would type

csc /target:library NameOfFile.cs

and press Enter. After doing this, a library with the name of the file and the extension .dll would be created. If you want a custom name, use the following syntax:

csc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.cs

Practical Learning: Creating a Library

1. Start a new file in Notepad and type the following in it:

288

Page 289: Introduction to C#

 

using System;

namespace Arithmetic{ public class Operations {

public static double Addition(double x, double y){ return x + y;}

public static double Subtraction(double x, double y){ return x - y;}

public static double Multiplication(double x, double y){ return x * y;}

public static double Division(double x, double y){ if( y == 0 )

return 0; return x / y;}

}}

2. Save the file in a new folder named Operations1

3. Save the file itself as exo.cs

4. Switch to the Command Prompt and change to the Operations1 folder

5. To create the library, type csc /target:library /out:Arithmetic.dll exo.cs and press Enter

6. Start another file in Notepad and type the following: 

using System;using Arithmetic;

public class Exercise{ static void Main() {

double Number1 = 244.58;double Number2 = 5082.88;double Result = Operations.Addition(Number1, Number2);

Console.WriteLine("{0} + {1} = {2}\n", Number1, Number2, Result); }}

289

Page 290: Introduction to C#

7. Save the file in a new folder named Algebra1

8. Save the file itself as Exercise.cs

9. Switch to the Command Prompt and change to the Algebra1 folder

10. To compile the program, type csc /reference:Arithmetic.dll Exercise.cs and press Enter

11. To execute the application, type Exercise and press Enter

 

Delegates and Events 

Delegates 

Introduction

The C and C++ languages have long enjoyed the concept of function pointer. This was even more useful when programming for the Microsoft Windows operating systems because the Win32 library relies on the concept of callback functions. Callback functions are used in Microsoft Windows programming to process messages. For this reason and because of their functionality, callback functions were carried out in the .NET Framework but they were defined with the name of delegate.

A delegate is a special type of user-defined variable that is declared globally, like a class. In fact, a delegate is created like an interface but as a method. Based on this, a delegate provides a template for a method, like an interface provides a template for a class. Like

290

Page 291: Introduction to C#

an interface, a delegate is not defined. Its role is to show what a useful method would look like. To support this concept, a delegate can provide all the necessary information that would be used on a method. This includes a return type, no argument or one or more arguments.

 

Practical Learning: Introducing Delegates

1. Start Notepad and, in the empty file, type the following: 

using System;

namespace LoanProcessing{

class Exercise{

static double GetPrincipal(){

double P;

Console.Write("Enter the Principal: $");P = double.Parse(Console.ReadLine());return P;

}

static double GetInterestRate(){

double r;

Console.Write("Enter the Interest Rate (%): ");r = double.Parse(Console.ReadLine());return r;

}

static int GetPeriod(){

int t;

Console.Write("Enter the number of months: ");t = int.Parse(Console.ReadLine());return t;

} static int Main(){

double Principal, IntRate;int NumberOfPeriods;

Console.WriteLine("This program allows you to calculate the amount of money a ");

Console.WriteLine("customer will owe at the end of the lifetime of a loan\n");

291

Page 292: Introduction to C#

Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");Console.WriteLine("Loan Processing\n");Principal = GetPrincipal();IntRate = GetInterestRate();NumberOfPeriods= GetPeriod();Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");

Console.WriteLine("==================================");Console.WriteLine("Estimate on loan");Console.WriteLine("----------------------------------");Console.WriteLine("Principal: {0:C}", Principal);Console.WriteLine("Interest: {0:P}", IntRate / 100);Console.WriteLine("Period: {0} months", NumberOfPeriods);Console.WriteLine("==================================\n");

return 0;

}}

}

2. Save the file as Exercise.cs in a new folder called LoanProcess1 created inside your CSharp Lessons folder

3. Open the Command Prompt and switch to the folder that contains the current exercise

4. To compile the program, type csc /out:"Loan Evaluation".exe Exercise.cs and press Enter

5. To execute it, type "Loan Evaluation" and press EnterHere is an example: 

This program allows you to calculate the amount of money acustomer will owe at the end of the lifetime of a loan

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Loan Processing

Enter the Principal: $4500Enter the Interest Rate (%): 12.55Enter the number of months: 24%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

==================================Estimate on loan----------------------------------Principal: $4,500.00Interest: 12.55 %Period: 24 months==================================

6. Return to Notepad

7. In the above program, the clerk was asked to provide the number of months for the period of the loan. Depending on the loan, one customer may want to specify the number of days necessary to pay the loan. Another customer may want to pay a loan over a number of years. To make this possible, we will allow the clerk to select the type of period for a loan.Change the Exercise.cs file as follows: 

292

Page 293: Introduction to C#

using System;

namespace LoanProcessing{

class Exercise{

static double GetPrincipal(){

double P;

Console.Write("Enter the Principal: $");P = double.Parse(Console.ReadLine());return P;

}

static double GetInterestRate(){

double r;

Console.Write("Enter the Interest Rate (%): ");r = double.Parse(Console.ReadLine());return r;

}

static void GetPeriod(ref int TypeOfPeriod, ref int Periods){

Console.WriteLine("How do you want to enter the length of time?");Console.WriteLine("1 - In Days");Console.WriteLine("2 - In Months");Console.WriteLine("3 - In Years");Console.Write("Your Choice: ");TypeOfPeriod = int.Parse(Console.ReadLine());

if( TypeOfPeriod == 1 ){

Console.Write("Enter the number of days: ");Periods = int.Parse(Console.ReadLine());

}else if( TypeOfPeriod == 2 ){

Console.Write("Enter the number of months: ");Periods = int.Parse(Console.ReadLine());

}else if( TypeOfPeriod == 3 ){

Console.Write("Enter the number of years: ");Periods = int.Parse(Console.ReadLine());

}else{

TypeOfPeriod = 0;// The user made an invalid selection. So, we will give upConsole.WriteLine("Bad Selection\n");

}}

293

Page 294: Introduction to C#

static int Main(){

double Principal, IntRate;int TypeOfPeriod = 0;int Periods = 0;string PeriodName = null;

Console.WriteLine("This program allows you to calculate the amount of money a ");

Console.WriteLine("customer will owe at the end of the lifetime of a loan\n");

Console.WriteLine("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");Console.WriteLine("Loan Processing\n");Principal = GetPrincipal();IntRate = GetInterestRate();

GetPeriod(ref TypeOfPeriod, ref Periods);if( TypeOfPeriod == 0 ){

// Since the user made a bad selection, stop the program herereturn 0;

} // Since this "if" condition has a "return 0" line, if the "if"// condition produces true, the "return 0" means the function// would be terminated. If the condition is false, the inside of// this "if" condition would not execute and the function would// continue. This means that, if the condition is false, then// the "else' is implied. Therefore, we don't have to write an// "else" condition: it is automatic.

if( TypeOfPeriod == 1 ){

PeriodName = "days";}else if( TypeOfPeriod == 2 ){

PeriodName = "months";}else if( TypeOfPeriod == 3 ){

PeriodName = "years";}

Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");

Console.WriteLine("==================================");Console.WriteLine("Estimate on loan");Console.WriteLine("----------------------------------");Console.WriteLine("Principal: {0:C}", Principal);Console.WriteLine("Interest: {0:P}", IntRate / 100);Console.WriteLine("Period: {0} {1}", Periods, PeriodName);Console.WriteLine("==================================\n");

return 0;}

}

294

Page 295: Introduction to C#

}

8. Save the file

9. Compile and test it

10. Select the whole contents of the file (Ctrl+A) and copy it to the clipboard (Ctrl+C)

11. Start a new instance of Notepad

12. To make the program more professional, paste the file in it and change it as follows: 

using System;

namespace LoanProcessing{

class Evaluation{

public double GetPrincipal(){

double P;

Console.Write("Enter the Principal: $");P = double.Parse(Console.ReadLine());return P;

}

public double GetInterestRate(){

double r;

Console.Write("Enter the Interest Rate (%): ");r = double.Parse(Console.ReadLine());return r;

}

public double GetPeriod(ref int TypeOfPeriod, ref double Periods)

{Console.WriteLine("How do you want to enter the

length of time?");Console.WriteLine("1 - In Days");Console.WriteLine("2 - In Months");Console.WriteLine("3 - In Years");Console.Write("Your Choice: ");TypeOfPeriod = int.Parse(Console.ReadLine());

if( TypeOfPeriod == 1 ){

Console.Write("Enter the number of days: ");

Periods = double.Parse(Console.ReadLine());return Periods / 360;

}else if( TypeOfPeriod == 2 ){

Console.Write("Enter the number of months: ");

295

Page 296: Introduction to C#

Periods = double.Parse(Console.ReadLine());return Periods / 12;

}else if( TypeOfPeriod == 3 ){

Console.Write("Enter the number of years: ");

Periods = double.Parse(Console.ReadLine());return Periods;

}else{

TypeOfPeriod = 0;// The user made an invalid selection. So,

we will give upConsole.WriteLine("Bad Selection\n");return 0.00;

}}

// Interest = Principal * rate * time in yearspublic double InterestAmount(double P, double r, double t){

return P * (r / 100) * t;}

}}

13. Save the file as Evaluation.cs

14. Change the Exercise.cs file as follows: 

using System;

namespace LoanProcessing{

class Exercise{

static int Main(){

Evaluation Borrow = new Evaluation();double Principal, IntRate, Period, AmountPaidAsInterest;int TypeOfPeriod = 0;double Periods = 0;string PeriodName = null;

Console.WriteLine("This program allows you to calculate the amount of money a ");

Console.WriteLine("customer will owe at the end of the lifetime of a loan\n");

Console.WriteLine("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");Console.WriteLine("Loan Processing\n");Principal = Borrow.GetPrincipal();IntRate = Borrow.GetInterestRate();Period = Borrow.GetPeriod(ref TypeOfPeriod, ref Periods);

296

Page 297: Introduction to C#

AmountPaidAsInterest = Borrow.InterestAmount(Principal, IntRate, Period);

if( TypeOfPeriod == 0 ){

// Since the user made a bad selection, stop the program herereturn 0;

} // Since this "if" condition has a "return 0" line, if the "if"// condition produces true, the "return 0" means the function// would be terminated. If the condition is false, the inside of// this "if" condition would not execute and the function would// continue. This means that, if the condition is false, then// the "else' is implied. Therefore, we don't have to write an// "else" condition: it is automatic.

if( TypeOfPeriod == 1 ){

PeriodName = "days";}else if( TypeOfPeriod == 2 ){

PeriodName = "months";}else if( TypeOfPeriod == 3 ){

PeriodName = "years";}

Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");

Console.WriteLine("==================================");Console.WriteLine("Estimate on loan");Console.WriteLine("----------------------------------");Console.WriteLine("Principal: {0:C}", Principal);Console.WriteLine("Interest: {0:P}", IntRate / 100);Console.WriteLine("Period: {0} {1}", Periods, PeriodName);Console.WriteLine("--------------------------------");Console.WriteLine("Interest paid on Loan: {0:C}", AmountPaidAsInterest);Console.WriteLine("==================================\n");

return 0;}

}}

15. Save the file

16. Switch to the Command Prompt

17. To compile the program, type csc /out:"Loan Evaluation".exe Evaluation.cs Exercise.cs and press Enter

18. To execute it, type "Loan Evaluation" and press Enter. Here is an example: 

This program allows you to calculate the amount of money acustomer will owe at the end of the lifetime of a loan

297

Page 298: Introduction to C#

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Loan Processing

Enter the Principal: $850Enter the Interest Rate (%): 14.55How do you want to enter the length of time?1 - In Days2 - In Months3 - In YearsYour Choice: 1Enter the number of days: 240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

==================================Estimate on loan----------------------------------Principal: $850.00Interest: 14.55 %Period: 240 days--------------------------------Interest paid on Loan: $82.45==================================

19. Return to the Exercise.cs file

 

Delegate Declaration

To declare a delegate, you use the delegate keyword. The basic formula used to create a delegate is:

[attributes] [modifiers] delegate result-type identifier ([formal-parameters]);

The attributes factor can be a normal C# attribute.

The modifier can be one or an appropriate combination of the following keywords: new, public, private, protected, or internal.

The delegate keyword is required.

The ReturnType can be any of the data types we have used so far. It can also be a type void or the name of a class.

The Name must be a valid name for a method.

Because a delegate is some type of a template for a method, you must use parentheses, required for every method. If this method will not take any argument, you can leave the parentheses empty.

Here is an example:

using System;

delegate void dlgSimple();

class Exercise{

298

Page 299: Introduction to C#

static int Main(){

return 0;}

}

After declaring a delegate, remember it only provides a template for a method, not an actual method. In order to use it, you must define a method that would carry an assignment the method is supposed to perform. That method must have the same return type and the same (number of) argument(s), if any. For example, the above declared delegate is of  type void and it does not take any argument. you can define a method as follows:

using System;

delegate void dlgSimple();

class Exercise{

private static void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C# Programming!");}

static int Main(){

return 0;}

}

With such a method implemented, you can associate it to the name of the delegate. To do that, where you want to use the method, first declare a variable of the type of the delegate using the new operator. In the parentheses of the constructor, pass the name of the method. Here is an example

using System;

delegate void dlgSimple();

class Exercise{

private static void Welcome(){Console.WriteLine("Welcome to the Wonderful World of C# Programming!");}

static int Main(){

dlgSimple Announce = new dlgSimple(Welcome);

return 0;}

}

This declaration gives meaning to the declared delegate. To actually use the method, call the name of the delegate as if it were a defined method. Here is an example:

using System;

299

Page 300: Introduction to C#

delegate void dlgSimple();

class Exercise{

private static void Welcome(){Console.WriteLine("Welcome to the Wonderful World of C# Programming!");}

static int Main(){

dlgSimple Announce = new dlgSimple(Welcome);

Announce();return 0;

}}

This would produce:

Welcome to the Wonderful World of C# Programming!

You can also declare a delegate that returns a value. When defining a method that would be associated with the delegate, remember that that method must return the same type of value. Here is an example:

using System;

delegate void dlgSimple();delegate double Addition();

class Exercise{

private static void Welcome(){Console.WriteLine("Welcome to the Wonderful World of C# Programming!");}

private static double Plus(){

double a = 248.66, b = 50.28;

return a + b;}

static int Main(){

dlgSimple Announce = new dlgSimple(Welcome);Addition Add = new Addition(Plus);

Announce();Console.WriteLine("\n248.66 + 50.26 = {0}", Add());return 0;

}}

300

Page 301: Introduction to C#

This would produce:

Welcome to the Wonderful World of C# Programming!

248.66 + 50.26 = 298.94

 

Practical Learning: Using a Delegate

1. To declare and use a delegate, change the Exercise.cs file as follows: 

using System;

namespace LoanProcessing{

delegate double Add2Values(double Value1, double Value2);

class Exercise{

static int Main(){

Evaluation Borrow = new Evaluation();double Principal, IntRate, Period, AmountPaidAsInterest;int TypeOfPeriod = 0;double Periods = 0;string PeriodName = null;

Add2Values Add = new Add2Values(Addition);

Console.WriteLine("This program allows you to calculate the amount of money a ");Console.WriteLine("customer will owe at the end of the lifetime of a loan\n");

Console.WriteLine("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");Console.WriteLine("Loan Processing\n");Principal = Borrow.GetPrincipal();IntRate = Borrow.GetInterestRate();Period = Borrow.GetPeriod(ref TypeOfPeriod, ref Periods);AmountPaidAsInterest = Borrow.InterestAmount(Principal, IntRate, Period);double Amount = Add(Principal, AmountPaidAsInterest);

if( TypeOfPeriod == 0 ){

// Since the user made a bad selection, stop the program herereturn 0;

} // Since this "if" condition has a "return 0" line, if the "if"// condition produces true, the "return 0" means the function// would be terminated. If the condition is false, the inside of// this "if" condition would not execute and the function would// continue. This means that, if the condition is false, then// the "else' is implied. Therefore, we don't have to write an// "else" condition: it is automatic.

if( TypeOfPeriod == 1 )

301

Page 302: Introduction to C#

{PeriodName = "days";

}else if( TypeOfPeriod == 2 ){

PeriodName = "months";}else if( TypeOfPeriod == 3 ){

PeriodName = "years";}

Console.WriteLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");

Console.WriteLine("==================================");Console.WriteLine("Estimate on loan");Console.WriteLine("----------------------------------");Console.WriteLine("Principal: {0:C}", Principal);Console.WriteLine("Interest: {0:P}", IntRate / 100);Console.WriteLine("Period: {0} {1}", Periods, PeriodName);Console.WriteLine("--------------------------------");Console.WriteLine("Total Amount Paid: {0:C}", Amount);Console.WriteLine("Interest paid on Loan: {0:C}", AmountPaidAsInterest);Console.WriteLine("==================================\n");

return 0;}

static double Addition(double Value1, double Value2){

return Value1 + Value2;}

}}

2. Test the program. Here is an example: 

This program allows you to calculate the amount of money acustomer will owe at the end of the lifetime of a loan

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Loan Processing

Enter the Principal: $850Enter the Interest Rate (%): 14.55How do you want to enter the length of time?1 - In Days2 - In Months3 - In YearsYour Choice: 1Enter the number of days: 240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

==================================Estimate on loan

302

Page 303: Introduction to C#

----------------------------------Principal: $850.00Interest: 14.55 %Period: 240 days--------------------------------Total Amount Paid: $932.45Interest paid on Loan: $82.45==================================

3. Return to the Exercise.cs file

Delegates and Classes

In the above introductions, we associated delegates with only method of the main class. Because delegates are usually declared globally, that is outside of a class, they can be associated with a method of any class, provided the method has the same return type (and the same (number of) argument(s)) as the delegate. When we created the methods of the main class, we defined them as static, since all methods of the main class must be declared static.

Methods of any class can also be associated to delegates. Here is an example of two methods associated with a common delegate:

using System;

namespace CSharpLessons{

delegate double Multiplication();

public class Cube{

private double _side;

public double Side{

get { return _side; }

set { _side = value; }}

public Cube(){

_side = 0;}

public Cube(double s){

_side = s;}

public double Area(){

return 6 * Side * Side;}

public double Volume(){

303

Page 304: Introduction to C#

return Side * Side * Side;}

public void CubeCharacteristics(){

Multiplication AreaDefinition = new Multiplication(Area);Multiplication VolDefinition = new Multiplication(Volume);

Console.WriteLine("Cube Characteristics");Console.WriteLine("Side: {0}", Side);Console.WriteLine("Area: {0}", AreaDefinition());Console.WriteLine("Volume: {0}\n", VolDefinition());

}}

class Exercise{

static int Main(){

Cube SmallBox = new Cube(25.58);

SmallBox.CubeCharacteristics();return 0;

}}

}

This would produce:

Cube CharacteristicsSide: 25.58Area: 3926.0184Volume: 16737.925112

Delegates Compositions

One of the characteristics that set delegates apart from C/C++ function pointers is that one delegate can be added to another using the + operation. This is referred to as composition. This is done by adding one delegate variable to another as in a = b + c.

Delegates and Arguments 

A Delegate That Takes One of More Arguments

If you want to associate a method that takes arguments to a delegate, when declaring the delegate, provide the necessary argument(s) in its parentheses. Here is an example of a delegate that takes two arguments (and returns a value):

delegate double Addition(double x, double y);

When defining the associated method, besides returning the same type of value if not void, make sure that the method takes the same number of arguments. Here is an example:

304

Page 305: Introduction to C#

using System;

delegate void dlgSimple();delegate double Addition(double x, double y);

class Exercise{

private static void Welcome(){Console.WriteLine("Welcome to the Wonderful World of C# Programming!");}

private static double Plus(double a, double b){

return a + b;}

static int Main(){

dlgSimple Announce = new dlgSimple(Welcome);

Announce();

return 0;}

}

Once again, to associate the method, declare a variable of the type of delegate and pass the name of the method to the constructor of the delegate. Here is an example:

Addition Add = new Addition(Plus);

Notice that only the name of the method is passed to the delegate. To actually use the delegate, when calling it, in its parentheses, provide a value for the argument(s) conform to the type specified when declaring the delegate. Here is an example:

using System;

delegate void dlgSimple();delegate double Addition(double x, double y);

class Exercise{

private static void Welcome(){Console.WriteLine("Welcome to the Wonderful World of C# Programming!");}

private static double Plus(double a, double b){

return a + b;}

static int Main(){

double Value1 = 248.66, Value2 = 50.26, Result;dlgSimple Announce = new dlgSimple(Welcome);

305

Page 306: Introduction to C#

Addition Add = new Addition(Plus);

Result = Add(Value1, Value2);

Announce();Console.WriteLine("\n{0} + {1} = {2}", Value1, Value2, Result);

return 0;}

}

This would produce:

Welcome to the Wonderful World of C# Programming!

248.66 + 50.26 = 298.92

A Delegate Passed as Argument

Using delegates, one method can be indirectly passed as argument to another method. To proceed, first declare the necessary delegate. Here is a example of such a delegate:

using System;

namespace GeometricFormulas{

public delegate double Squared(double x);

public class Circle{

private double _radius;

public double Radius{

get { return _radius; }

set { _radius = value; }}

}}

A delegate can be passed as argument to a method. Such an argument would be used as if it were a method itself. This means that, when accessed in the body of the method, the name of the delegate must be accompanied by parentheses and if the delegate takes an argument or argument, the argument(s) must be provided in the parentheses of the called delegate. Here is an example:

using System;

namespace GeometricFormulas{

public delegate double Squared(double x);

public class Circle{

private double _radius;

306

Page 307: Introduction to C#

public double Radius{

get { return _radius; }

set { _radius = value; }}

public double Area(Squared sqd){

return sqd(_radius) * Math.PI;}

}}

After declaring a delegate, remember to define a method that implements the needed behavior of that delegate. Here is an example:

using System;

namespace GeometricFormulas{

public delegate double Squared(double x);

public class Circle{

private double _radius;

public static double ValueTimesValue(double Value){

return Value * Value;}

}}

You can also define the associated method in another class, not necessarily in the class where the delegate would be needed. Once the method that implements the delegate is known, you can use the delegate as you see fit. To do that, you can declare a variable of the type of that delegate and pass the implementing method to its constructor. Here is an example:

using System;

namespace GeometricFormulas{

public delegate double Squared(double x);

public class Circle{

private double _radius;

public static double ValueTimesValue(double Value){

return Value * Value;}

public double Area(Squared sqd){

307

Page 308: Introduction to C#

return sqd(_radius) * Math.PI;}

public void CircleCharacteristics(){

Squared Sq = new Squared(ValueTimesValue);}

}

}

This declaration gives life to the delegate and can then be used as we have proceed with delegates so far.

Here is an example:

using System;

namespace GeometricFormulas{

public delegate double Twice(double x);public delegate double Squared(double x);

public class Circle{

private double _radius;

public static double TwoTimes(double Value){

return Value * 2;}

public static double ValueTimesValue(double Value){

return Value * Value;}

public double Radius{

get { return _radius; }

set { _radius = value; }}

public Circle(){

_radius = 0;}

public Circle(double r){

_radius = r;}

public double Diameter(double rad){

return rad * 2;

308

Page 309: Introduction to C#

}

public double Circumference(Twice FDiam){

double Circf;

Circf = FDiam(_radius);return Circf * Math.PI;

}

public double Area(Squared sqd){

return sqd(_radius) * Math.PI;}

public void CircleCharacteristics(){

Squared Sq = new Squared(ValueTimesValue);Twice Tw = new Twice(TwoTimes);

Console.WriteLine("Circle Characteristics");Console.WriteLine("Radius: {0}", Radius);

Console.WriteLine("Diameter: {0}", Diameter(_radius));Console.WriteLine("Circumference: {0}", Circumference(Tw));

Console.WriteLine("Area: {0}\n", Area(Sq));}

}

class Exercise{

static int Main(){

Circle Round = new Circle(25.58);

Round.CircleCharacteristics();return 0;

}}

}

This would produce:

Circle CharacteristicsRadius: 25.58Diameter: 51.16Circumference: 160.723880157654Area: 2055.65842721639

An Array of Delegates

To further refine the call to a group of methods that perform the same kind of task, you can declare an array of delegates. Before creating the array, you must first know or have the methods you would be referring to. These methods must have a similar signature. This means that they must return the same type of value, they must have the same number of arguments and they must  have the  same type(s) of argument(s). Here are examples of such functions:

309

Page 310: Introduction to C#

class Exercise{

const double PI = 3.14159;

static double Diameter(double Radius){

return Radius * 2;}

static double Circumference(double Radius){

return Diameter(Radius) * PI;}

static double Area(double Radius){

return Radius * Radius * PI;}

}

To declare an array of delegates, proceed as you would for a normal C# array as we have done so far. Here is an example:

using System;

class Exercise{

static double Diameter(double Radius){

return Radius * 2;}

static double Circumference(double Radius){

return Diameter(Radius) * PI;}

static double Area(double Radius){

return Radius * Radius * PI;}

static int Main(){

Measure[] Calc = new Measure[3];

return 0;}

}

You can initialize each member using its index and calling the corresponding method. This can be done as follows:

using System;

delegate double Measure(double R);

310

Page 311: Introduction to C#

class Exercise{

const double PI = 3.14159;

static double Diameter(double Radius){

return Radius * 2;}

static double Circumference(double Radius){

return Diameter(Radius) * PI;}

static double Area(double Radius){

return Radius * Radius * PI;}

static int Main(){

double R = 12.55;Measure[] Calc = new Measure[3];

Calc[0] = new Measure(Diameter);double D = Calc[0](R);Calc[1] = new Measure(Circumference); double C = Calc[1](R);Calc[2] = new Measure(Area);double A = Calc[2](R);

Console.WriteLine("Circle Characteristics");Console.WriteLine("Diameter: {0}", D);Console.WriteLine("Circumference: {0}", C);Console.WriteLine("Area: {0}\n", A);

return 0;}

}

This would produce:

Circle CharacteristicsDiameter: 25.1Circumference: 78.8539Area: 494.808

Practical Learning: Using an Array of Delegates

1. Start a new file in Notepad and type the following: 

using System;

namespace MultipleChoiceQuestion{

311

Page 312: Introduction to C#

class Exercise{

enum TMCQuestion { One, Two, Three, Four, Five };delegate char Question();

static char Sequence(){

char Answer;

Console.WriteLine("Which sequence of numbers does not appear ");Console.WriteLine("to follow a recognizable order?");Console.WriteLine("(a) 3 9 27 33");Console.WriteLine("(b) 3 6 9 12");Console.WriteLine("(c) 2 4 6 8");Console.WriteLine("(d) 102 204 408 816");Console.Write("Your Answer? ");Answer = char.Parse(Console.ReadLine());

return Answer;

}

static char Expression(){

char Response;

Console.WriteLine("Select the best expression to complete the empty space");Console.WriteLine("When ... drugs to a business address, traffickers often ");

Console.WriteLine("omit a recipient name");Console.WriteLine("(a) to send");Console.WriteLine("(b) senders");Console.WriteLine("(c) sending");Console.WriteLine("(d) dealing");Console.Write("Your Answer? ");Response = char.Parse(Console.ReadLine());

return Response;

}

static char Sentence(){

char Answer;

Console.WriteLine("Even ... there are 76,000 lawyers in that city, ");Console.WriteLine("it is still a small community");Console.WriteLine("(a) although");Console.WriteLine("(b) though");Console.WriteLine("(c) for");Console.WriteLine("(d) since");Console.Write("Your Answer? ");Answer = char.Parse(Console.ReadLine());

return Answer;

}

static char WrongWord(){

312

Page 313: Introduction to C#

char Wrong;

Console.WriteLine("Select the wrong word that would complete ");Console.WriteLine("the sentence");Console.WriteLine("For this type of business, revenue gains are ...");Console.WriteLine("(a) limited");Console.WriteLine("(b) scarce");Console.WriteLine("(c) limitless");Console.WriteLine("(d) claiming");Console.Write("Your Answer? ");Wrong = char.Parse(Console.ReadLine());

return Wrong;

}

static char Right(){

char Sentence;

Console.WriteLine("Select the right sentence");Console.WriteLine("(a) The company is expecting to reducing inventory,");Console.WriteLine(" control cost, and efficiency improvement.");Console.WriteLine("(b) The company expects to reduce inventory,");Console.WriteLine(" control cost, and improve efficiency.");Console.WriteLine("(c) The company expects to reduce inventory,");Console.WriteLine(" control cost, and improving efficiency.");Console.WriteLine("(d) The company is expecting to reducing inventory,");Console.WriteLine(" controlling cost, and efficiency improvement.");Console.Write("Your Answer? ");Sentence = char.Parse(Console.ReadLine());

return Sentence;

}

static int Main(){

const int NumberOfQuestions = 5;char[] Answer = new char[NumberOfQuestions];

Question[] MCQ = new Question[NumberOfQuestions];

MCQ[0] = new Question(Sequence);MCQ[1] = new Question(Expression);MCQ[2] = new Question(Sentence);MCQ[3] = new Question(WrongWord);MCQ[4] = new Question(Right);

for(int i = 0; i < NumberOfQuestions; i++){

Console.WriteLine("Question {0}", i + 1);Answer[i] = MCQ[i]();ValidateAnswer(i+1, Answer[i]);

}

return 0;}

313

Page 314: Introduction to C#

static void ValidateAnswer(int QstNbr, char Ans){

switch(QstNbr){

case 1:if(Ans == 'a' || Ans == 'A')

Console.WriteLine("Right Answer");else{

Console.WriteLine("Wrong Answer - The right answer was 'a'");Console.WriteLine("(a) Starting at 3, 3*3=9 and 3*9=27");Console.WriteLine(" There is no obvious way to determine 33");

}break;

case 2:if(Ans == 'c' || Ans == 'C')

Console.WriteLine("Right answer");else

Console.WriteLine("Wrong Answer - The right answer was 'c'");break;

case 3:

if(Ans == 'b' || Ans == 'B')Console.WriteLine("Right answer");

elseConsole.WriteLine("Wrong Answer - The right answer was 'b'");

break;

case 4:if(Ans == 'd' || Ans == 'D')

Console.WriteLine("Right answer");else

Console.WriteLine("Wrong Answer - The right answer was 'd'");break;

case 5:if(Ans == 'b' || Ans == 'B')

Console.WriteLine("Right answer");else

Console.WriteLine("Wrong Answer - The right answer was 'b'");break;

default:Console.WriteLine("Invalid Answer");break;

}}

}}

2. Save the file in a new folder named MCQ1

3. Save the file itself as Exercise.cs

4. At the Command Prompt, compile and execute the file

314

Page 315: Introduction to C#

Events 

Introduction

Except for the main class of your program (the class that contains the Main() method), every class is mostly meant to interact with other, either to request values and methods of the other classes or to provide other classes with some values or a behavior they need. When a class A requests a value or service from another class B, class A is referred to as a client of class B. This relationship is important not simply because it establishes a relationship between both classes but also because class B should be ready to provide the value or behavior that a client needs at a certain time.

While a class B is asked to provide some values or methods to another class A, many things would happen. In fact, there is an order that things should follow. For example, during the lifetime of a program, that is, while a program is running, a class may be holding a value it can provide to its client but at another time, that value may not be available anymore, for any reason; nothing strange, this is just the ways it happens. Because different things can happen to a class B while a program is running, and because only class B would be aware of these, it must be able to signal to the other classes when there is a change. This is the basis of events: An event is an action that occurs on an object and affects it in a way that its clients must be made aware of. Events are mostly familiar to those who do graphical (GUI) programming as they are able to "visually" work on Windows controls and as they are able to access the objects on which actions are happening and the objects that must know when these actions occur. Still, because events are dealt with in C#, you should be aware of their functionality.

Events were never part of traditional computer languages such as C/C++ or Pascal. These languages were developed without the operating system in mind. In fact, they were made platform independent so they can be used on any operating system regardless of how that OS functions. Event were highly used at the operating system level. Therefore, they were implemented in operating system libraries like Win32. On the other hands, because C# was developed a long time after its parent the C++, and because it was meant to substitute Win32 knowledge, events were built in the languages. Still, all languages of the .NET Framework are "aware" of the concept of events.

 

Although events are mostly used in Windows controls programming, they can also be implemented in console applications.

 

Event Creation

An event is declared like a pseudo-variable but based on a delegate. Therefore, to declare an event, you must have a delegate that would implement it. Here is an example:

using System;

delegate void dlgSimple();

class Exercise

315

Page 316: Introduction to C#

{public static void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C# Programming!");

}}

To actually declare an event, you use the event keyword with the following formula:

[attributes] [modifiers] event type declarator;[attributes] [modifiers] event type member-name {accessor-declarations};

The attributes factor can be a normal C# attribute.

The modifier can be one or a combination of the following keywords: public, private, protected, internal, abstract, new, override, static, virtual, or extern.

The event keyword is required. It is followed by the name of the delegate that specifies its behavior. If the event is declared in the main class, it should be made static. Like everything in a program, an event must have a name. This would allow the clients to know what (particular) event occurred. Here is an example:

using System;

delegate void dlgSimple();

class Exercise{

public static event dlgSimple Simply;

public static void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C# Programming!");

}}

After declaring the event, you must define a method that calls the event. Here is an example:

using System;

delegate void dlgSimple();

class Exercise{

public static event dlgSimple Simply;

public static void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C# Programming!");

}

public static void SayHello(){

Simply();}

316

Page 317: Introduction to C#

}

When the event occurs, its delegate would be invoked. This specification is also referred to as hooking up an event. As the event occurs (or fires), the method that implements the delegate runs. This provides complete functionality for the event and makes the event ready to be used. Before using an event, you must combine it to the method that implements it. This can be done by passing the name of the method to the appropriate delegate, as we learned when studying delegates. You can then assign this variable to the event's name using the += operator. Once this is done, you can call the event. Here is an example:

using System;

delegate void dlgSimple();

class Exercise{

public static event dlgSimple Simply;

public static void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C# Programming!");

}

public static void SayHello(){

Simply();}

static int Main(){

Simply += new dlgSimple(Welcome);

SayHello();

return 0;}

}

Instead of the += operator used when initializing the event, you can implement add and remove of the event class. Here is an example:

using System;

delegate void dlgSimple();

class Exercise{

public event dlgSimple Simply{

add{

Simply += new dlgSimple(Welcome);}remove{

Simply -= new dlgSimple(Welcome);

317

Page 318: Introduction to C#

}}

public void Welcome(){

Console.WriteLine("Welcome to the Wonderful World of C# Programming!");}

}

 

Previous Copyright © 2004-2007 FunctionX, Inc. <a href="Lesso

 

Exception Handling 

Introduction to Exceptions 

An Overview

During the execution of a program, the computer will face two types of situations: those it is prepared to deal with and those it doesn’t like. Imagine you write a program that requests a number from the user:

using System;

public class Exercise{

static void Main(){

double Number;

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);}

 

318

Page 319: Introduction to C#

}

This is a classic easy program. When it comes up, the user is asked to simply type a number. The number would then be multiplied by 2 and display the result. Imagine that a user types something that is not a valid number, such as the name of a country or somebody’s telephone number. Since this program was expecting a number and it is not prepared to multiply a string to a number, it would not know what to do. The only alternative the compiler would have is to send the problem to the operating system, hoping that the OS would know what to do. What actually happens is that, whenever the compiler is handed a task, it would try to perform the assignment. If it can’t perform the assignment, for any reason it is not prepared for, it would produce an error. As a programmer, if you can anticipate the type(s) of error(s) that could occur in your program, you can deal with it(them) by telling the compiler what to do when this type of error occurs.

Practical Learning: Introducing Exception Handling

1. Start Notepad and type the following: 

using System;

namespace GeorgetownCleaningServices{

struct OrderInformation{

// Basic information about an orderpublic string CustomerName;public string HomePhone;public DateTime OrderDate;public DateTime OrderTime;// Unsigned numbers to represent cleaning itemspublic uint NumberOfShirts;public uint NumberOfPants;public uint NumberOfDresses;

}}

2. Save the file in a new folder named GCS4

3. Save the file itself as OrderInfo.cs

4. To start a new file, on the main menu of Notepad, click File -> New and type the following: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 0.0575M; // 5.75%

OrderInformation Order;

319

Page 320: Introduction to C#

// Each of these sub totals will be used for cleaning itemsprivate decimal SubTotalShirts;private decimal SubTotalPants;private decimal SubTotalDresses;

// Values used to process an orderprivate decimal TotalOrder;private decimal TaxAmount;private decimal SalesTotal;private decimal AmountTended;private decimal Difference;

public void ProcessOrder() {

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");Order.CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");Order.HomePhone = Console.ReadLine();Console.Write("Enter the order date(mm/dd/yyyy): ");Order.OrderDate = DateTime.Parse(Console.ReadLine());Console.Write("Enter the order time(hh:mm AM/PM): ");Order.OrderTime = DateTime.Parse(Console.ReadLine());

// Request the quantity of each category of itemsConsole.Write("Number of Shirts: ");Order.NumberOfShirts = uint.Parse(Console.ReadLine());

Console.Write("Number of Pants: ");Order.NumberOfPants = uint.Parse(Console.ReadLine());

Console.Write("Number of Dresses: ");Order.NumberOfDresses = uint.Parse(Console.ReadLine());

// Perform the necessary calculationsSubTotalShirts = Order.NumberOfShirts * PriceOneShirt;SubTotalPants = Order.NumberOfPants * PriceAPairOfPants;SubTotalDresses = Order.NumberOfDresses * PriceOneDress;// Calculate the "temporary" total of the order

TotalOrder = SubTotalShirts + SubTotalPants + SubTotalDresses;

// Calculate the tax amount using a constant rateTaxAmount = TotalOrder * TaxRate;// Add the tax amount to the total orderSalesTotal = TotalOrder + TaxAmount;

// Communicate the total to the user...Console.WriteLine("\nThe Total order is: {0:C}", SalesTotal);// and request money for the orderConsole.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

// Calculate the difference owed to the customer// or that the customer still owes to the store

320

Page 321: Introduction to C#

Difference = AmountTended - SalesTotal;

ShowReceipt();}

private void ShowReceipt(){

Console.WriteLine();// Display the receiptConsole.WriteLine("====================================");Console.WriteLine("-/- Georgetown Cleaning Services -/-");Console.WriteLine("====================================");Console.WriteLine("Customer: {0}", Order.CustomerName);Console.WriteLine("Home Phone: {0}", Order.HomePhone);Console.WriteLine("Order Date: {0:D}", Order.OrderDate);Console.WriteLine("Order Time: {0:t}", Order.OrderTime);Console.WriteLine("------------------------------------");Console.WriteLine("Item Type Qty Unit/Price Sub-Total");Console.WriteLine("------------------------------------");Console.WriteLine("Shirts {0,3} {1,4} {2,6}",

Order.NumberOfShirts, PriceOneShirt, SubTotalShirts);Console.WriteLine("Pants {0,3} {1,4} {2,6}",

Order.NumberOfPants, PriceAPairOfPants, SubTotalPants);Console.WriteLine("Dresses {0,3} {1,4} {2,6}",

Order.NumberOfDresses, PriceOneDress, SubTotalDresses);Console.WriteLine("------------------------------------");

Console.WriteLine("Total Order: {0,6}", TotalOrder.ToString("C"));Console.WriteLine("Tax Rate: {0,6}", TaxRate.ToString("P"));Console.WriteLine("Tax Amount: {0,6}", TaxAmount.ToString("C"));Console.WriteLine("Net Price: {0,6}", SalesTotal.ToString("C"));

Console.WriteLine("------------------------------------");Console.WriteLine("Amount Tended: {0,6}", AmountTended.ToString("C"));Console.WriteLine("Difference: {0,6}", Difference.ToString("C"));

Console.WriteLine("====================================");}

}}

5. Save the file as OrderProcess.cs in the GCS4 folder

6. To start a new file, on the main menu of Notepad, click File -> New and type the following: 

using System;

namespace GeorgetownCleaningServices{

class Exercise{

static void Main(){

OrderProcessing Order = new OrderProcessing();

Order.ProcessOrder();}

}}

321

Page 322: Introduction to C#

7. Save the file as Exercise.cs in the GCS4 folder

8. Open the Command Prompt and change to the above GCS4 directory

9. To create the application, typecsc /out:"Georgetown Cleaning Services".exe OrderInfo.cs OrderProcess.cs Exercise.cs and press Enter

10. To execute the application, type "Georgetown Cleaning Services" and press Enter. Here is an example: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: PerlEnter Customer Phone: 301-884-0912Enter the order date(mm/dd/yyyy): 08/04/0Enter the order time(hh:mm AM/PM): 10:26Number of Shirts: 12Number of Pants: 2Number of Dresses: 8

The Total order is: $56.79Amount Tended? 70

====================================-/- Georgetown Cleaning Services -/-====================================Customer: PerlHome Phone: 301-884-0912Order Date: Monday, August 04, 2003Order Time: 10:26 AM------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 12 0.95 11.40Pants 2 2.95 5.90Dresses 8 4.55 36.40------------------------------------Total Order: $53.70Tax Rate: 5.75 %Tax Amount: $3.09Net Price: $56.79------------------------------------Amount Tended: $70.00Difference: $13.21====================================

11. Return to Notepad

Exceptional Behaviors

An exception is an unusual situation that could occur in your program. As a programmer, you should anticipate any abnormal behavior that could be caused by the user entering wrong information that could otherwise lead to unpredictable results. The ability to deal with a program’s eventual abnormal behavior is called exception handling. C# provides three keywords to handle an exception.

322

Page 323: Introduction to C#

1. Trying the normal flow: To deal with the expected behavior of a program, use the try keyword as in the following syntax:

try {Behavior}

The try keyword is required. It lets the compiler know that you are attempting a normal flow of your program. The actual behavior that needs to be evaluated is included between an opening curly bracket “{“ and a closing curly bracket “}”. Inside of the brackets, implement the normal flow that the program must follow, at least for this section of the code. Here is an example: 

using System;

public class Exercise{

static void Main(){

double Number;

try {

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);}

}}

2. Catching Errors: During the flow of the program as part of the try section, if an abnormal behavior occurs, instead of letting the program crash or instead of letting the compiler send the error to the operating system, you can transfer the flow of the program to another section that can deal with it. The syntax used by this section is:

catch {WhatToDo}

This section always follows the try section. There must not be any code between the try’s closing bracket and the catch section. The catch keyword is required and follows the try section. Combined with the try block, the syntax of an exception would be: 

try{

// Try the program flow}catch{

// Catch the exception}

323

Page 324: Introduction to C#

A program that includes a catch section would appear as follows: 

using System;

public class Exercise{

static void Main(){

double Number;

try {

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);

}catch{

}}

}

  

Practical Learning: Introducing Vague Exceptions

1. To introduce exceptions, access the OrderProcess.cs file and change it as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

. . . No Change

public void ProcessOrder() {

. . . No Change

// Request the quantity of each category of itemstry {

Console.Write("Number of Shirts: ");Order.NumberOfShirts = uint.Parse(Console.ReadLine());

}catch{

324

Page 325: Introduction to C#

}

try {

Console.Write("Number of Pants: ");Order.NumberOfPants = uint.Parse(Console.ReadLine());

}catch{}

try {

Console.Write("Number of Dresses: ");Order.NumberOfDresses =

uint.Parse(Console.ReadLine());}catch{}

. . . No Change

// and request money for the ordertry {

Console.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

}catch{}

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;

ShowReceipt();}

private void ShowReceipt(){

. . . No Change}

}}

2. Save the file

3. At the Command Prompt, to compile the application, typecsc /out:"Georgetown Cleaning Services".exe OrderInfo.cs OrderProcess.cs Exercise.cs and press Enter

4. To execute the application, type "Georgetown Cleaning Services" and press Enter

5. Return to Notepad

325

Page 326: Introduction to C#

Exceptions and Custom Messages

As mentioned already, if an error occurs when processing the program in the try section, the compiler transfer the processing to the next catch section. You can then use the catch section to deal with the error. At a minimum, you can display a message to inform the user. Here is an example:

using System;

public class Exercise{

static void Main(){

double Number;

try {

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);}catch{

Console.WriteLine("There was a problem with the program");}

}}

Of course, this type of message is not particularly clear but this time, the program will not crash. In the next sections, we will learn better ways of dealing with the errors and the messages.

 

Practical Learning: Displaying Custom Messages

1. To display custom messages to the user, change the OrderProcess.cs file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

. . . No Change

public void ProcessOrder() {

. . . No Change

// Request the quantity of each category of itemstry {

326

Page 327: Introduction to C#

Console.Write("Number of Shirts: ");Order.NumberOfShirts = uint.Parse(Console.ReadLine());

}catch{

Console.WriteLine("The value you typed for the number of " +"shirts is not a valid number");

}

try {

Console.Write("Number of Pants: ");Order.NumberOfPants = uint.Parse(Console.ReadLine());

}catch{

Console.WriteLine("The value you typed for the number of " + "pair or pants is not a valid number");

}

try {

Console.Write("Number of Dresses: ");Order.NumberOfDresses = uint.Parse(Console.ReadLine());

}catch{

Console.WriteLine("The value you typed for the number of " + "dresses is not a valid number");

}

// Perform the necessary calculationsSubTotalShirts = Order.NumberOfShirts * PriceOneShirt;SubTotalPants = Order.NumberOfPants * PriceAPairOfPants;SubTotalDresses = Order.NumberOfDresses * PriceOneDress;// Calculate the "temporary" total of the orderTotalOrder = SubTotalShirts + SubTotalPants + SubTotalDresses;

// Calculate the tax amount using a constant rateTaxAmount = TotalOrder * TaxRate;// Add the tax amount to the total orderSalesTotal = TotalOrder + TaxAmount;

// Communicate the total to the user...Console.WriteLine("\nThe Total order is: {0:C}", SalesTotal);// and request money for the ordertry {

Console.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

}catch{

Console.WriteLine("You were asked to enter an amount of money but...");

}

327

Page 328: Introduction to C#

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;

ShowReceipt();}

private void ShowReceipt(){

. . . No Change}

}}

2. Save the file

3. At the Command Prompt, to compile the application, typecsc /out:"Georgetown Cleaning Services".exe OrderInfo.cs OrderProcess.cs Exercise.cs and press Enter

4. To execute the application, type "Georgetown Cleaning Services" and press Enter. Here is an example: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: AlexandriaEnter Customer Phone: (102) 797-8382Enter the order date(mm/dd/yyyy): 04/02/2001Enter the order time(hh:mm AM/PM): 09:22 AMNumber of Shirts: 6Number of Pants: WThe value you typed for the number of pair or pants is not a valid numberNumber of Dresses: 5

The Total order is: $30.09Amount Tended? _100DYou were asked to enter an amount of money but...

====================================-/- Georgetown Cleaning Services -/-====================================Customer: AlexandriaHome Phone: (102) 797-8382Order Date: Monday, April 02, 2001Order Time: 9:22 AM------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 6 0.95 5.70Pants 0 2.95 0Dresses 5 4.55 22.75------------------------------------Total Order: $28.45Tax Rate: 5.75 %Tax Amount: $1.64

328

Page 329: Introduction to C#

Net Price: $30.09------------------------------------Amount Tended: $0.00Difference: ($30.09)====================================

5. Return to Notepad

Exceptions in the .NET Framework 

The Exception Class

In traditionally-oriented error dealing languages such as C/C++ or Object Pascal, you could create any exception of your choice, including numeric or strings. To customize exception handling, you could also create your own class(es). Most libraries such as Borland's VCL and Microsoft's MFC also shipped with their own classes to handle exceptions. Event the Win32 library provides its type of mechanism to face errors. To support exception handling, the .NET Framework provides a special class called Exception. Once the compiler encounters an error, the Exception class allows you to identify the type of error and take an appropriate action.

Normally, Exception mostly serves as the general class of exceptions. Anticipating various types of problems that can occur in a program, Microsoft derived various classes from Exception to make this issue friendlier. As a result, almost any type of exception you may encounter already has a class created to deal with it. Therefore, when your program faces an exception, you can easily identify the type of error. There are so many exception classes that we cannot study or review them all. The solution we will use is to introduce or review a class when we meet its type of error.

The Exception's Message

In exception handling, errors are dealt with in the catch section. To do this, use catch as if it were a method. This means that, on the right side of catch, opening a parenthesis, declare a variable of the type of exception you want to deal with. By default, an exception is first of type Exception. Based on this, a typical formula to implement exception handling is:

try{

// Process the normal flow of the program here}catch(Exception e){

// Deal with the exception here}

When an exception occurs in the try section, code compilation is transferred to the catch section. If you declare the exception as an Exception type, this class will identify the error. One of the properties of the Exception class is called Message. This property contains a string that describes the type of error that occurred. You can then use this Exception.Message property to display an error message if you want. Here is an example:

using System;

329

Page 330: Introduction to C#

public class Exercise{

static void Main(){

double Number;

try {

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);}catch(Exception ex){

Console.WriteLine(ex.Message);}

}}

Here is an example of running the program:

Type a number: 38G.25Input string was not in a correct format.

 

Custom Error Messages

As you can see, one of the strengths of the Exception.Message property is that it gives you a good indication of the type of problem that occurred. Sometimes, the message provided by the Exception class may not appear explicit enough. In fact, you may not want to show it to the user since, as in this case, the user may not understand what the expression "correct format" in this context means and why it is being used. As an alternative, you can create your own message and display it to the user. Here is an example:

using System;

public class Exercise{

static void Main(){

double Number = 0.00;

try {

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);}catch(Exception ex){Console.WriteLine("The operation could not be carried because " +

"the number you typed is not valid");

330

Page 331: Introduction to C#

}}

}

Here is an example of running the program:

Type a number: 88D.46The operation could not be carried because the number you typed is not valid

You can also combine the Exception.Message message and your own message:

using System;

public class Exercise{

static void Main(){

double Number = 0.00;

try {

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);}catch(Exception ex){

Console.WriteLine(ex.Message + "\nThe operation could not be carried because " +

"the number you typed is not valid");}

}}

Here is an example of running the program:

Type a number: 78m4Input string was not in a correct format.The operation could not be carried because the number you typed is not valid

A Review of .NET Exception Classes 

Introduction

The .NET Framework provides various classes to handle almost any type of exception you can think of. There are so many of these classes that we can only mention the few that we regularly use in our application.

There are two main ways you can use one of the classes of the .NET Framework. If you know for sure that a particular exception will be produced, pass its name to the catch() clause. You don't have to name the argument. Then, in the catch() section, display a custom message. The second option you have consists of using the throw keyword. We will study it later.

From now on, we will try to always indicate the type of exception that could be thrown if

331

Page 332: Introduction to C#

something goes wrong in a program

FormatException

When studying data formatting in Lesson 4, we saw that everything the user types into an application using the keyboard is primarily a string and that you must convert it to the appropriate type before using it. When you request a specific type of value from the user, after the user has typed it and you decide to convert it to the appropriate type, if your conversion fails, the program produces (in the next lessons, we will use he word "throw") an error. The error is of from the FormatException class.

Here is a program that deals with a FormatException exception:

using System;

public class Exercise{

static void Main(){

double Number = 0.00;

try {

Console.Write("Type a number: ");Number = double.Parse(Console.ReadLine());

Console.WriteLine("\n{0} * 2 = {1}", Number, Number * 2);}catch(FormatException){

Console.WriteLine("You typed an invalid number");}

}}

Here is an example of running the program:

Type a number: 39W.68gYou typed an invalid number

 

Practical Learning: Using the FormatException Class

1. Change the OrderProcess.cs file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

. . . No Change

332

Page 333: Introduction to C#

public void ProcessOrder() {

. . . No Change

// Request the quantity of each category of itemstry {

Console.Write("Number of Shirts: ");Order.NumberOfShirts = uint.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("The value you typed for the number of " +"shirts is not a valid number");

}

try {

Console.Write("Number of Pants: ");Order.NumberOfPants = uint.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("The value you typed for the number of " + "pair or pants is not a valid number");

}

try {

Console.Write("Number of Dresses: ");Order.NumberOfDresses = uint.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("The value you typed for the number of " + "dresses is not a valid number");

}

. . . No Change

// Communicate the total to the user...Console.WriteLine("\nThe Total order is: {0:C}", SalesTotal);// and request money for the ordertry {

Console.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

}catch(FormatException){Console.WriteLine("You were asked to enter an amount of money but...");}

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;

333

Page 334: Introduction to C#

ShowReceipt();}

private void ShowReceipt(){

. . . No Change}

}}

2. Save the file

3. At the Command Prompt, to compile the application, typecsc /out:"Georgetown Cleaning Services".exe OrderInfo.cs OrderProcess.cs Exercise.cs and press Enter

4. To execute the application, type "Georgetown Cleaning Services" and press Enter

5. Return to Notepad

The OverflowException Exception

A computer application receives, processes, and produces values on a regular basis as the program is running. To better manage these values, as we saw when studying variables and data types in Lesson 2, the compiler uses appropriate amounts of space to store its values. It is not unusual that either you the programmer or a user of your application provides an value that is beyond the allowed range based on the data type. For example, we saw that a byte uses 8 bits to store a value and a combination of 8 bits can store a number no more than 255. If you provide a value higher than 255 to be stored in a byte, you get an error. Consider the following program:

using System;

// An Exercise classclass Exercise{

static void Main(){

byte NumberOfPages;

Console.Write("Enter the number of pages of the newspaper: ");NumberOfPages = byte.Parse(Console.ReadLine());

Console.WriteLine("Number of Pages of the Newspaper: {0}\n", NumberOfPages);

}}

When a value beyond the allowable range is asked to be stored in memory, the compiler produces (the verb is "throws" as we will learn soon) an error of the OverflowException class. Here is an example of running the program:

Enter the number of pages of the newspaper: 824

Unhandled Exception: System.OverflowException: Value was either too large or too small for an unsigned byte.

334

Page 335: Introduction to C#

at System.Byte.Parse(String s, NumberStyles style, IFormatProvider provider) at System.Byte.Parse(String s) at Exercise.Main() in c:\programs\msvcs .net 2003\project17\exercise.cs:line11

As with the other errors, when this exception is thrown, you should take appropriate action.

ArgumentOutOfRangeException

Once again, when studying the techniques of converting or formatting values in Lesson 4, we saw that a value is passed to the Parse() method of its data type for analysis. For a primitive data type, the Parse() method scans the string and if the string cannot be converted into a valid character or number, the compiler usually throws a FormatException exception as we saw above. Other classes such as DateTime also use a Parse() method to scan the value submitted to it. For example, if you request a date value from the user, the DateTime.Parse() method scans the string to validate it. In US English, Parse() expects the user to type a string in the form m/d/yy or mm/dd/yy or mm/dd/yyyy. Consider the following program:

using System;

// An Exercise classclass Exercise{

static void Main(){

DateTime DateHired;

Console.Write("Enter Date Hired: ");DateHired = DateTime.Parse(Console.ReadLine());

Console.WriteLine("Date Hired: {0:d}\n", DateHired);}

}

If the user types a value that cannot be converted into a valid date, the compiler throws an ArgumentOutOfRangeException exception. Here is an example of running the above program:

Enter Date Hired: 1244/04/258

Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Lex(Int32 dps, __DTString str, DateTimeToken dtok, DateTimeRawInfo raw, DateTimeResult result, DateTimeFormatInfo& dtfi) at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider) at System.DateTime.Parse(String s) at Exercise.Main() in c:\programs\msvcs .net 2003\project17\exercise.cs:line 11

One way you can avoid this is to guide the user but still take appropriate actions, just in case this error is thrown.

 

335

Page 336: Introduction to C#

Practical Learning: Using an ArgumentOutOfRangeException Exception

1. Change the OrderProcess.cs file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

. . . No Change

public void ProcessOrder() {

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");Order.CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");Order.HomePhone = Console.ReadLine();try {

Console.WriteLine("Enter the date this order was placed");Console.Write("Enter Today's Day: ");int day = int.Parse(Console.ReadLine());Console.Write("Enter Current Month: ");int month = int.Parse(Console.ReadLine());Console.Write("Enter This Year: ");int year = int.Parse(Console.ReadLine());Order.OrderDate = new DateTime(year, month, day);

}catch(ArgumentOutOfRangeException){

Console.WriteLine("The date you entered is not valid - We will ignore it");

}

try {

Console.WriteLine("Enter the time this order was placed");Console.Write("Enter the hour: ");int hour = int.Parse(Console.ReadLine());Console.Write("Enter the minutes: ");int minute = int.Parse(Console.ReadLine());Order.OrderDate = new DateTime(0, 0, 0, hour, minute, 0);

}catch(ArgumentOutOfRangeException){

Console.WriteLine("The time value you entered is not valid - We will ignore it");

}

// Request the quantity of each category of items

336

Page 337: Introduction to C#

try {

Console.Write("Number of Shirts: ");Order.NumberOfShirts = uint.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("The value you typed for the number of " + "shirts is not a valid number");

}

try {

Console.Write("Number of Pants: ");Order.NumberOfPants = uint.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("The value you typed for the number of " + "pair or pants is not a valid number");

}

try {

Console.Write("Number of Dresses: ");Order.NumberOfDresses = uint.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("The value you typed for the number of " + "dresses is not a valid number");

}

. . . No Change

// Communicate the total to the user...Console.WriteLine("\nThe Total order is: {0:C}", SalesTotal);// and request money for the ordertry {

Console.Write("Amount Tended? ");AmountTended = decimal.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You were asked to enter an amount of money but...");}

// Calculate the difference owed to the customer// or that the customer still owes to the storeDifference = AmountTended - SalesTotal;

ShowReceipt();}

private void ShowReceipt()

337

Page 338: Introduction to C#

{. . . No Change

}}

}

2. Save the file

3. At the Command Prompt, to compile the application, typecsc /out:"Georgetown Cleaning Services".exe OrderInfo.cs OrderProcess.cs Exercise.cs and press Enter

4. To execute the application, type "Georgetown Cleaning Services" and press Enter. Here is an example: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: Gertrude NganaEnter Customer Phone: 208-923-8263Enter the date this order was placedEnter Today's Day: 48Enter Current Month: 6Enter This Year: 2002The date you entered is not valid - We will ignore itEnter the time this order was placedEnter the hour: 62Enter the minutes: 380The time value you entered is not valid - We will ignore itNumber of Shirts: wThe value you typed for the number of shirts is not a valid numberNumber of Pants: 8Number of Dresses: 2

The Total order is: $34.58Amount Tended? q42You were asked to enter an amount of money but...

====================================-/- Georgetown Cleaning Services -/-====================================Customer: Gertrude NganaHome Phone: 208-923-8263Order Date: Monday, January 01, 0001Order Time: 12:00 AM------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 0 0.95 0Pants 8 2.95 23.60Dresses 2 4.55 9.10------------------------------------Total Order: $32.70Tax Rate: 5.75 %Tax Amount: $1.88Net Price: $34.58------------------------------------Amount Tended: $0.00

338

Page 339: Introduction to C#

Difference: ($34.58)====================================

5. Return to Notepad

The DivideByZeroException Exception

Division by zero is an operation to always avoid. It is so important that it is one of the most fundamental exceptions of the computer. It is addressed at the core level even by the Intel and AMD processors. It is also addressed by the operating systems at their level. It is also addressed by most, if not all, compilers. It is also addressed by most, if not, all libraries. This means that this exception is never welcomed anywhere. The .NET Framework also provides it own class to face this operation.

If an attempt to divide a value by 0, the compiler throws a DivideByZeroException exception. We will see an example later.

Techniques of Using Exceptions 

Throwing an Exception

As mentioned above, the Exception class is equipped with a Message property that carried a message for the error that occurred. We also mentioned that the message of this property may not be particularly useful to a user. Fortunately, you can create your own message and pass it to the Exception. To be able to receive custom messages, the Exception class provides the following constructor:

public Exception(string message);

To use it, in the section that you are anticipating the error, type the throw keyword followed by a new instance of the Exception class using the constructor that takes a string. Here is an example:

using System;

class Exercise{

static void Main(){

double Operand1, Operand2;double Result = 0.00;char Operator;

Console.WriteLine("This program allows you to perform an operation on two numbers");

try {

Console.WriteLine("To proceed, enter a number, an operator, and a number:");

Operand1 = double.Parse(Console.ReadLine());Operator = char.Parse(Console.ReadLine());

339

Page 340: Introduction to C#

Operand2 = double.Parse(Console.ReadLine());

if( Operator != '+' && Operator != '-' && Operator != '*' && Operator != '/')

throw new Exception(Operator.ToString());

switch(Operator){

case '+':Result = Operand1 + Operand2;break;

case '-':Result = Operand1 - Operand2;break;

case '*':Result = Operand1 * Operand2;break;

case '/':Result = Operand1 / Operand2;break;

default:Console.WriteLine("Bad Operation");break;

}Console.WriteLine("\n{0} {1} {2} = {3}", Operand1, Operator,

Operand2, Result);}catch(Exception ex){

Console.WriteLine("\nOperation Error: {0} is not a valid operator", ex.Message);

}}

}

Here is an example of running the program:

This program allows you to perform an operation on two numbersTo proceed, enter a number, an operator, and a number:38.44#124.52

Operation Error: # is not a valid operator

 

Catching Various Exceptions

In the examples above, when we anticipated some type of problem, we instructed the compiler to use our default catch section. We left it up to the compiler to find out when there was a problem and we provided a catch section to deal with it. A method with numerous or complex operations and requests can also produce different types of errors. With such a type of

340

Page 341: Introduction to C#

program, you should be able to face different problems and deal with them individually, each by its own kind. To do this, you can create different catch sections, each made for a particular error. The formula used would be:

try {// Code to Try

}catch(Arg1){

// One Exception}catch(Arg2){

// Another Exception}

The compiler would proceed in a top-down:

1. Following the normal flow of the program, the compiler enters the try block

2. If no exception occurs in the try block, the rest of the try block is executedIf an exception occurs in the try block, the compiler registers the type of error that occurred. If there is a throw line, the compiler registers it also:

a. The compiler gets out of the try section

b. The compiler examines the first catch. If the first catch matches the thrown error, that catch executes and the exception handling routine may seize. If the first catch doesn’t match the thrown error, the compiler proceeds with the next catch

c. The compiler checks the next match, if any and proceeds as in the first match. This continues until the compiler finds a catch that matches the thrown error

d. If one of the catches matches the thrown error, its body executes. If no catch matches the thrown error, the compiler calls the Exception class and uses the default message

Multiple catches are written if or when a try block is expected to throw different types of errors. Once again, consider the following program:

using System;

class Exercise{

static void Main(){

double Operand1, Operand2;double Result = 0.00;char Operator;

Console.WriteLine("This program allows you to perform an operation on two numbers");

try {

Console.WriteLine("To proceed, enter a number, an operator, and a number:");

Operand1 = double.Parse(Console.ReadLine());Operator = char.Parse(Console.ReadLine());

341

Page 342: Introduction to C#

Operand2 = double.Parse(Console.ReadLine());

switch(Operator){

case '+':Result = Operand1 + Operand2;break;

case '-':Result = Operand1 - Operand2;break;

case '*':Result = Operand1 * Operand2;break;

case '/':Result = Operand1 / Operand2;break;

default:Console.WriteLine("Bad Operation");break;

}Console.WriteLine("\n{0} {1} {2} = {3}", Operand1, Operator,

Operand2, Result);}catch(Exception ex){

Console.WriteLine("\nOperation Error: {0} is not a valid operator", ex.Message);

}}

}

This program works fine as long as the user types a valid sequence of values made of a number, a valid arithmetic operator, and a number. Anything else, such an invalid number, an unexpected operator, or a wrong sequence (such as a number then another number instead of an operator), would cause an error to be thrown. Obviously various bad things could happen when this program is running. To handle the exceptions that this program could produce, you can start with the most likely problem that would occur. Trusting that a user is able to provide the two numbers that are requested, it is possible that a user would type an invalid operator. For example, for this program we will perform only the addition (+), the subtraction(-), the multiplication(*), and the division(/). Therefore, we will first validate the operator. This can be done as follows:

using System;

class Exercise{

static void Main(){

double Operand1, Operand2;double Result = 0.00;char Operator;

342

Page 343: Introduction to C#

Console.WriteLine("This program allows you to perform an operation on two numbers");

try {

Console.WriteLine("To proceed, enter a number, an operator, and a number:");

Operand1 = double.Parse(Console.ReadLine());Operator = char.Parse(Console.ReadLine());Operand2 = double.Parse(Console.ReadLine());

if( Operator != '+' && Operator != '-' && Operator != '*' && Operator != '/')

throw new Exception(Operator.ToString());

switch(Operator){

case '+':Result = Operand1 + Operand2;break;

case '-':Result = Operand1 - Operand2;break;

case '*':Result = Operand1 * Operand2;break;

case '/':Result = Operand1 / Operand2;break;

default:Console.WriteLine("Bad Operation");break;

}Console.WriteLine("\n{0} {1} {2} = {3}", Operand1, Operator,

Operand2, Result);}catch(Exception ex){

Console.WriteLine("\nOperation Error: {0} is not a valid operator", ex.Message);

}}

}

When this program runs, if the user provides a valid number followed by a wrong operator, we call the Exception(string message) constructor and pass it a string converted from the character that was typed. Then, when the compiler gets out of the try block, it looks for and finds a catch clause that receives a character value. Therefore, this catch is executed.

Imagine that the user wants to perform a division. You need to tell the compiler what to do if the user enters the denominator as 0 (or 0.00). If this happens, the best option, and probably the only one you should consider is to display a message and get out. Fortunately, the .NET Framework provides the DivideByZeroException class to deal with an exception caused by

343

Page 344: Introduction to C#

division by zero. As done with the message passed to the Exception class, you can compose your own message and pass it to the DivideByZeroException(string message) constructor.

Exception is the parent of all exception classes. This corresponds to the three periods of a C++' catch(...) block. Therefore, if you write various catch blocks, the one that takes the Exception as argument must be the last.

Here is an example that catches two types of exceptions:

using System;

class Exercise{

static void Main(){

double Operand1, Operand2;double Result = 0.00;char Operator;

Console.WriteLine("This program allows you to perform an operation on two numbers");

try {

Console.WriteLine("To proceed, enter a number, an operator, and a number:");

Operand1 = double.Parse(Console.ReadLine());Operator = char.Parse(Console.ReadLine());Operand2 = double.Parse(Console.ReadLine());

if( Operator != '+' && Operator != '-' && Operator != '*' && Operator != '/')

throw new Exception(Operator.ToString());

if( Operator == '/' )if( Operand2 == 0 )

throw new DivideByZeroException("Division by zero is not allowed");

switch(Operator){

case '+':Result = Operand1 + Operand2;break;

case '-':Result = Operand1 - Operand2;break;

case '*':Result = Operand1 * Operand2;break;

case '/':Result = Operand1 / Operand2;break;

344

Page 345: Introduction to C#

default:Console.WriteLine("Bad Operation");break;

}Console.WriteLine("\n{0} {1} {2} = {3}", Operand1, Operator,

Operand2, Result);}catch(DivideByZeroException ex){

Console.WriteLine(ex.Message);}catch(Exception ex){

Console.WriteLine("\nOperation Error: {0} is not a valid operator", ex.Message);

}}

When running this program, if the user types a wrong operator, the compiler gets out the try block and looks for a catch that takes an Exception as argument. It finds the second and executes it. If the user enters the right values (a number, an operator, and another number), then the compiler finds out if the operator entered was a forward slash “/” used to perform a division. If the user wants to perform a division, the compiler finds out if the second operand, the denominator, is 0. If it is, we create a DivideByZeroException instance and pass our own message to it. Based on this exception, the compiler gets out of the try block and starts looking for a catch block that takes a DivideByZeroException argument. It finds it in the first catch. Therefore, the compiler executes it.

 

Practical Learning: Identifying the Thrown Exception

1. To catch various exceptions, change the OrderProcess.cs file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing{

. . . No Change

public void ProcessOrder() {

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");Order.CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");Order.HomePhone = Console.ReadLine();

// To make it easy for the user, we will request different values for the date

345

Page 346: Introduction to C#

try {

Console.WriteLine("Enter the date this order was placed");Console.Write("Enter Today's Day: ");int day = int.Parse(Console.ReadLine());Console.Write("Enter Current Month: ");int month = int.Parse(Console.ReadLine());Console.Write("Enter This Year: ");int year = int.Parse(Console.ReadLine());Order.OrderDate = new DateTime(year, month, day);

}// This exception is thrown if the user types a value that cannot// be converted into a valid numbercatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message +"\nThe value you entered " +"is not a valid number");

}// This exception is thrown if the values that user had typed cannot// produce a valid date valuecatch(ArgumentOutOfRangeException ex){

Console.WriteLine("The date you entered is not valid - " + "We will ignore it and use 1/1/0001 instead");

}

try {Console.Write("Enter the time the order was placed(hh:mm AM/PM): ");

Order.OrderTime = DateTime.Parse(Console.ReadLine());}// This exception is thrown if the user typed values that cannot// produce a valid (date or) time valuecatch(FormatException){

Console.WriteLine("The time you entered is not valid and it will be refused");}

// Request the quantity of each category of itemstry {

Console.Write("Number of Shirts: ");Order.NumberOfShirts = uint.Parse(Console.ReadLine());

}// This exception is thrown if the user typed a value that cannot// be recognized as a valid numbercatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message + "\nThe value you typed for the number of " +

"shirts is not valid");}// This exception is thrown if the user types a negative valuecatch(OverflowException){

346

Page 347: Introduction to C#

Console.WriteLine("The number you typed is negative but " + "we cannot accept a negative number of shirts");

}

try {

Console.Write("Number of Pants: ");Order.NumberOfPants = uint.Parse(Console.ReadLine());

}// This exception is thrown if the user typed a value that cannot// be recognized as a valid numbercatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message + "\nThe value you typed for the number of " + "pair or pants is not valid");

}// This exception is thrown if the user types a negative valuecatch(OverflowException){

Console.WriteLine("The number you typed is negative and " + "we cannot accept it");

}

try {

Console.Write("Number of Dresses: ");Order.NumberOfDresses = uint.Parse(Console.ReadLine());

}// This exception is thrown if the user typed a value that cannot// be recognized as a valid numbercatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message + "\nThe value you typed for the number of " + "dresses is not valid");

}// This exception is thrown if the user types a negative valuecatch(OverflowException){

Console.WriteLine("The number you typed is lower than the " +"acceptable number. It will be ignored");

}

// Perform the necessary calculations

. . . No Change

ShowReceipt();}

private void ShowReceipt(){

. . . No Change}

}

347

Page 348: Introduction to C#

}

2. Save the file

3. At the Command Prompt, to compile the application, typecsc /out:"Georgetown Cleaning Services".exe OrderInfo.cs OrderProcess.cs Exercise.cs and press Enter

4. To execute the application, type "Georgetown Cleaning Services" and press Enter. Here is an example: 

-/- Georgetown Cleaning Services -/-Enter Customer Name: Andrew PalermoEnter Customer Phone: (240) 493-8935Enter the date this order was placedEnter Today's Day: 12Enter Current Month: 08Enter This Year: 2002Enter the time the order was placed(hh:mm AM/PM): 12:98The value you entered is not a valid numberNumber of Shirts: 9Number of Pants: -5The number you typed is negative and we cannot accept itNumber of Dresses: -12The number you typed is lower than the acceptable number. It will be ignored

The Total order is: $9.04Amount Tended? W10.00FError: Input string was not in a correct format.You were asked to enter an amount of money but you typed an invalid currency value

====================================-/- Georgetown Cleaning Services -/-====================================Customer: Andrew PalermoHome Phone: (240) 493-8935Order Date: Monday, August 12, 2002Order Time: 12:00 AM------------------------------------Item Type Qty Unit/Price Sub-Total------------------------------------Shirts 9 0.95 8.55Pants 0 2.95 0Dresses 0 4.55 0------------------------------------Total Order: $8.55Tax Rate: 5.75 %Tax Amount: $0.49Net Price: $9.04------------------------------------Amount Tended: $0.00Difference: ($9.04)====================================

348

Page 349: Introduction to C#

5. Return to Notepad

Exceptions Nesting

The calculator simulator we have studied so far performs a division as one of its assignments. We learned that, in order to perform any operation, the compiler must first make sure that the user has entered a valid operator. Provided the operator is one of those we are expecting, we also must make sure that the user typed valid numbers. Even if these two criteria are met, it was possible that the user enter 0 for the denominator. The block that is used to check for a non-zero denominator depends on the exception that validates the operators. The exception that could result from a zero denominator depends on the user first entering a valid number for the denominator.

You can create an exception inside of another. This is referred to as nesting an exception. This is done by applying the same techniques we used to nest conditional statements. This means that you can write an exception that depends on, and is subject to, another exception. To nest an exception, write a try block in the body of the parent exception. The nested try block must be followed by its own catch(es) clause. To effectively handle the exception, make sure you include an appropriate throw in the try block. Here is an example:

using System;

class Exercise{

static void Main(){

double Operand1, Operand2;double Result = 0.00;char Operator;

Console.WriteLine("This program allows you to perform an operation on two numbers");

try {

Console.WriteLine("To proceed, enter a number, an operator, and a number:");

Operand1 = double.Parse(Console.ReadLine());Operator = char.Parse(Console.ReadLine());Operand2 = double.Parse(Console.ReadLine());

if( Operator != '+' && Operator != '-' && Operator != '*' && Operator != '/')

throw new Exception(Operator.ToString());

switch(Operator){

case '+':Result = Operand1 + Operand2;Console.WriteLine("\n{0} + {1} = {2}", Operand1,

Operand2, Result);break;

case '-':Result = Operand1 - Operand2;

349

Page 350: Introduction to C#

Console.WriteLine("\n{0} - {1} = {2}", Operand1, Operand2, Result);

break;

case '*':Result = Operand1 * Operand2;Console.WriteLine("\n{0} * {1} = {2}", Operand1,

Operand2, Result);break;

case '/':// The following exception is nested in the

previous trytry {

if(Operand2 == 0)throw new

DivideByZeroException("Division by zero is not allowed");

Result = Operand1 / Operand2;Console.WriteLine("\n{0} / {1} = {2}",

Operand1, Operand2, Result);}catch(DivideByZeroException ex){

Console.WriteLine(ex.Message);}break;

}}catch(Exception ex){

Console.WriteLine("\nOperation Error: {0} is not a valid operator", ex.Message);

}}

}

Here is an example of running the program:

This program allows you to perform an operation on two numbersTo proceed, enter a number, an operator, and a number:245.55/0Division by zero is not allowed

Practical Learning: Nesting Exceptions

1. To nest exceptions, change the OrderProcess.cs file as follows: 

using System;

namespace GeorgetownCleaningServices{

class OrderProcessing

350

Page 351: Introduction to C#

{// Price of itemsconst decimal PriceOneShirt = 0.95M;const decimal PriceAPairOfPants = 2.95M;const decimal PriceOneDress = 4.55M;const decimal TaxRate = 0.0575M; // 5.75%

OrderInformation Order;

// Each of these sub totals will be used for cleaning itemsprivate decimal SubTotalShirts;private decimal SubTotalPants;private decimal SubTotalDresses;

// Values used to process an orderprivate decimal TotalOrder;private decimal TaxAmount;private decimal SalesTotal;private decimal AmountTended;private decimal Difference;

public void ProcessOrder() {

Console.WriteLine("-/- Georgetown Cleaning Services -/-");// Request order information from the userConsole.Write("Enter Customer Name: ");Order.CustomerName = Console.ReadLine();Console.Write("Enter Customer Phone: ");Order.HomePhone = Console.ReadLine();

// To make it easy for the user, we will request different values for the datetry {

int month = DateTime.Today.Month;int day = DateTime.Today.Day;int year = DateTime.Today.Year;

Console.WriteLine("Enter the date this order was placed");

try {

Console.Write("Enter the year this order was placed: ");year = int.Parse(Console.ReadLine());if( year < 1998 )

throw new ArgumentOutOfRangeException("This business started in 1998. " +"Any year prior to that value is invalid");

if( year > DateTime.Today.Year )throw new ArgumentOutOfRangeException("Common... " +

"We can't process an order that will occur in the future.");}// Exception to deal with if the user typed a non-digit in the valuecatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message +"\nYou entered an invalid value for the year");

}

351

Page 352: Introduction to C#

// Exception to deal with if the user typed a negative numbercatch(ArgumentOutOfRangeException ex){

Console.WriteLine(ex.Message);}

// Evaluate the value of the monthtry {

Console.Write("Enter the month this order was placed: ");month = int.Parse(Console.ReadLine());if( month < 1 )

throw new ArgumentOutOfRangeException("A negative month cannot be accepted");if( month > 12 )

throw new ArgumentOutOfRangeException("Valid numbers of month must be from 1 to 12");

}// Exception to deal with if the user typed a non-digit in the valuecatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message +"\nYou entered an invalid value for the month");

}// Exception to deal with if the user typed a negative number

catch(ArgumentOutOfRangeException ex){

Console.WriteLine(ex.Message);}

// Evaluate the value of the daytry {

Console.Write("Enter the day this order was placed: ");day = int.Parse(Console.ReadLine());if( day < 0 )

throw new ArgumentOutOfRangeException("A negative day cannot be accepted");}// Exception to deal with if the user typed a non-digit in the valuecatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message +"\nYou entered an invalid value");

}// Exception to deal with if the user typed a negative numbercatch(ArgumentOutOfRangeException ex){

Console.WriteLine(ex.Message);}

Order.OrderDate = new DateTime(year, month, day);}// This exception is thrown if the user types a value that cannot// be converted into a valid numbercatch(FormatException ex){

Console.WriteLine("Error: " + ex.Message +

352

Page 353: Introduction to C#

"\nThe value you entered " +"is not a valid number");

}

. . . No Change

ShowReceipt();}

private void ShowReceipt(){

. . . No Change}

}}

2. At the Command Prompt, to compile the application, typecsc /out:"Georgetown Cleaning Services".exe OrderInfo.cs OrderProcess.cs Exercise.cs and press Enter

3. To execute the application, type "Georgetown Cleaning Services" and press Enter

Exceptions and Methods

One of the most effective techniques used to deal with code is to isolate assignments. We have learned this when studying methods of classes. For example, the switch statement that was performing the operations in the “normal” version of our program can be written as follows:

using System;

class Exercise{

static void Main(){

double Number1, Number2;double Result = 0.00;char Operator;

Console.WriteLine("This program allows you to perform an operation on two numbers");

try {

Console.WriteLine("To proceed, enter");

Console.Write("First Number: ");Number1 = double.Parse(Console.ReadLine());

Console.Write("An Operator (+, -, * or /): ");Operator = char.Parse(Console.ReadLine());if( Operator != '+' && Operator != '-' &&

Operator != '*' && Operator != '/' )throw new Exception(Operator.ToString());

Console.Write("Second Number: ");

353

Page 354: Introduction to C#

Number2 = double.Parse(Console.ReadLine());

if( Operator == '/' )if( Number2 == 0 )

throw new DivideByZeroException("Division by zero is not allowed");

Result = Calculator(Number1, Number2, Operator);Console.WriteLine("\n{0} {1} {2} = {3}", Number1, Operator,

Number2, Result);}catch(FormatException){

Console.WriteLine("The number you typed is not valid");}catch(DivideByZeroException ex){

Console.WriteLine(ex.Message);}catch(Exception ex){

Console.WriteLine("\nOperation Error: {0} is not a valid operator", ex.Message);

}}

static double Calculator(double Value1, double Value2, char Symbol){

double Result = 0.00;

switch(Symbol){

case '+':Result = Value1 + Value2;break;

case '-':Result = Value1 - Value2;break;

case '*':Result = Value1 * Value2;break;

case '/':Result = Value1 / Value2;break;

}

return Result;}

}

You can still use regular methods along with methods that handle exceptions. As done in the Main() function, any method of a program can take care of its own exceptions that would occur in its body. Here is an example of an exception handled in a function:

354

Page 355: Introduction to C#

using System;

class Exercise{

static void Main(){

double Number1, Number2;double Result = 0.00;char Operator;

Console.WriteLine("This program allows you to perform an operation on two numbers");

try {

Console.WriteLine("To proceed, enter");

Console.Write("First Number: ");Number1 = double.Parse(Console.ReadLine());

Console.Write("An Operator (+, -, * or /): ");Operator = char.Parse(Console.ReadLine());if( Operator != '+' && Operator != '-' &&

Operator != '*' && Operator != '/' )throw new Exception(Operator.ToString());

Console.Write("Second Number: ");Number2 = double.Parse(Console.ReadLine());

Result = Calculator(Number1, Number2, Operator);}catch(FormatException){

Console.WriteLine("The number you typed is not valid");}catch(Exception ex){

Console.WriteLine("\nOperation Error: {0} is not a valid operator", ex.Message);

}}

static double Calculator(double Value1, double Value2, char Symbol){

double Result = 0.00;

switch(Symbol){

case '+':Result = Value1 + Value2;Console.WriteLine("\n{0} + {1} = {2}", Value1, Value2,

Result);break;

case '-':Result = Value1 - Value2;

355

Page 356: Introduction to C#

Console.WriteLine("\n{0} - {1} = {2}", Value1, Value2, Result);

break;

case '*':Result = Value1 * Value2;Console.WriteLine("\n{0} * {1} = {2}", Value1, Value2,

Result);break;

case '/':// The following exception is nested in the previous trytry {

if(Value2 == 0)throw new DivideByZeroException("Division

by zero is not allowed");

Result = Value1 / Value2;Console.WriteLine("\n{0} / {1} = {2}", Value1,

Value2, Result);}catch(DivideByZeroException ex){

Console.WriteLine(ex.Message);}break;

}

return Result;}

}

Isolating assignments and handing them to functions is a complete and important matter in the area of application programming. Consider a program that handles a simple exception such as this one:

using System;

class Exercise{

static void Main(){

double Number1, Number2;double Result = 0.00;char Operator = '/';

Console.WriteLine("This program allows you to perform a division of two numbers");

try {

Console.WriteLine("To proceed, enter two numbers:");Number1 = double.Parse(Console.ReadLine());Number2 = double.Parse(Console.ReadLine());;

if( Number2 == 0 )

356

Page 357: Introduction to C#

throw new DivideByZeroException("Division by zero is not allowed");

Result = Number1 / Number2;Console.WriteLine("\n{0} / {1} = {2}", Number1, Number2,

Result);}catch(DivideByZeroException ex){

Console.WriteLine(ex.Message);}

}}

One of the ways you can use methods in exception routines is to have a central method that receives variables, and sends them to an external method. The external method tests the value of a variable. If an exception occurs, the external method displays or sends a throw. This throw can be picked up by the method that sent the error. Observe the following example that implements this scenario:

using System;

class Exercise{

static void Main(){

double Number1, Number2;

Console.WriteLine("This program allows you to perform a division of two numbers");

try {

Console.WriteLine("To proceed, enter two numbers:");Number1 = double.Parse(Console.ReadLine());Number2 = double.Parse(Console.ReadLine());;

Division(Number1, Number2);}catch(DivideByZeroException ex){

Console.WriteLine(ex.Message);}

}

static void Division(double a, double b){

double Result;

// If an exception occurred,if( b == 0 ) // then throw a string to the function caller

throw new DivideByZeroException("Division by zero is not allowed");

Result = a / b;Console.WriteLine("\n{0} / {1} = {2}", a, b, Result);

}

357

Page 358: Introduction to C#

}

In this program, the Division method receives two values that it is asked to perform a division with. The Division method analyzes the second argument that represents the denominator. If this argument is zero, an exception is found and the Division method throws a DivideByZeroException exception.

A method can also be called to perform more than one test to eventually throw more than one exception. Such a method can (and should) be programmed to throw different types of exceptions.

 

Previous Copyright © 2004-2007 FunctionX, Inc. Next

Introduction to Arrays 

A Series of Similar Items 

Introduction

Imagine you want to create a program that would use a series of numbers. In math, we represent such a series as follows: X1, X2, X3, X4, X5. You can also represent a list of items as follows:

Alex

Gaston

Hermine

Jerry

In C#, so far, to use a series of items, we were declaring a variable for each of them. If the list was made of numbers, we would declare variables for such numbers as follows:

358

Page 359: Introduction to C#

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double number1 = 12.44, number2 = 525.38,

number3 = 6.28,number4 = 2448.32,

number5 = 632.04;}

}}

Instead of using individual variables that share the same characteristics, you can group them in an entity like a regular variable but called an array. Therefore, an array is a series of items of the same kind. It could be a group of numbers, a group of cars, a group of words, etc but all items of the array must be of the same type.

 

Practical Learning: Introducing Arrays1. Start Notepad

2. In the empty file, type the following: 

using System;

class IceCream{

public const decimal BasePrice = 1.55M;

public void ProcessAnOrder(){

int dFlv = 0,dCont = 0,dIngrd = 0;

int Scoops;decimal PriceIngredient, TotalPrice;

Console.WriteLine("Ice Cream Vendor Machine");

do {

try {

Console.WriteLine("What type of flavor do you want?");Console.WriteLine("1 - ");Console.WriteLine("2 - ");Console.WriteLine("3 - ");Console.WriteLine("4 - ");

359

Page 360: Introduction to C#

Console.WriteLine("5 - ");Console.WriteLine("6 - ");Console.Write("Your Choice? " );dFlv = int.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You must enter a valid number and no other character!");

}

if( dFlv < 1 || dFlv > 6 )Console.WriteLine("Invalid Choice - Try Again!\n");

} while( dFlv < 1 || dFlv > 6 );

do {

try {

Console.WriteLine("What type of container do you want?");Console.WriteLine("1 - ");Console.WriteLine("2 - ");Console.WriteLine("3 - ");Console.Write("Your Choice? ");dCont = int.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You must enter a valid number and no other character!");

}

if( dCont < 1 || dCont > 3 )Console.WriteLine("Invalid Choice - Try Again!");

} while( dCont < 1 || dCont > 3 );

do {

try {

Console.WriteLine("Do you want an ingredient or not");Console.WriteLine("1 - ");Console.WriteLine("2 - ");Console.WriteLine("3 - ");Console.WriteLine("4 -

1.

Console.WriteLine("4 - ");Console.Write("Your Choice? ");dIngrd = int.Parse(Console.ReadLine());

}catch(FormatException)

360

Page 361: Introduction to C#

{Console.WriteLine("You must enter a valid number and no other

character!");}

if( dIngrd < 1 || dIngrd > 4 )Console.WriteLine("Invalid Choice - Try Again!");

} while( dIngrd < 1 || dIngrd > 4 );

do{

try {

Console.Write("How many scoops(1, 2, or 3)? ");Scoops = int.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You must enter a valid number and no other character!");

}

if( Scoops < 1 || Scoops > 3 )Console.WriteLine("Invalid Choice - Try Again!");

} while( Scoops < 1 || Scoops > 3 );

if( dIngrd == 2 || dIngrd == 3 || dIngrd == 4 )PriceIngredient = 0.65M;

elsePriceIngredient = 0.00M;

TotalPrice = (BasePrice * Scoops) + PriceIngredient;

DisplayReceipt(dFlv, dCont, dIngrd, Scoops, TotalPrice);}

public void DisplayReceipt(int Flv, int Cont, int Ingrd, int spoons, decimal TotalPrice)

{Console.WriteLine("\nIce Cream Order");

Console.WriteLine("Scoops: {0}", spoons);Console.WriteLine("Total Price: {0:C}\n", TotalPrice);

}}

2. Save the file in a new folder named IceCream2

3. Save the file itself as Order.cs in the IceSream2 folder

4. Start another instance of Notepad and type the following: 

using System;

class Exercise{ static void Main()

361

Page 362: Introduction to C#

{ }}

5. Save the file as Exercise.cs in the IceCream2 folder

Array Creation

Before creating an array, you must first decide the type its items will be made of. Is it a group of numbers, a group of chairs, a group of buttons on a remote controls? This information allows the compiler to know how much space each item of the group will require. This is because each item of the group will occupy its own memory space, just like any of the variables we have used so far.

After deciding about the type of data of the items that make up the series, you must use a common name to identify them. The name is simply the same type you would use for a variable as we have used so far. The name allows you and the compiler to identify the area in memory where the items are located.

Thirdly, you must specify the number of items that will constitute the group. For the compiler to be able to allocate an adequate amount of space for the items of the list, once it knows how much space each item will require, it needs to know the number of items so an appropriate and large enough amount of space can be reserved. The number of items of an array is included in square brackets, as in [5].

In C# (unlike some other languages like C/C++ or Pascal), an array is considered a reference type. Therefore, an array requests its memory using the new operator (like a pointer in C/C++). Based on this, the basic formula to declare an array is:

DataType[] VariableName = new DataType[Number];

In this formula, the DataType factor can be one of the types we have used so far. It can also be the name of a class. The square brackets on the left of the assignment operator are used to let the compiler know that you are declaring an array instead of a regular variable. The new operator allows the compiler to reserve memory in the heap. The Number factor is used to specify the number of items of the list.

Based on the above formula, here is an example of an array variable:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double[] number = new double[5];

Console.WriteLine();}

}}

 

Practical Learning: Creating an Array

 

362

Page 363: Introduction to C#

1. To create arrays, access the Order.cs file and change it as follows: 

using System;

class IceCream{

public const decimal BasePrice = 1.55M;

public void ProcessAnOrder(){

int dFlv = 0, dCont = 0, dIngrd = 0;int Scoops = 0;decimal PriceIngredient, TotalPrice;

string[] Flavor = new string[6]; string[] Container = new string[3];string[] Ingredient = new string[4];

Console.WriteLine("Ice Cream Vendor Machine");

. . . No Change

}

. . . No Change}

2. Save the file

Operations on Arrays 

Array Initialization

When creating an array, you can specify the number of items that make up its list. Each item of the series is referred to as a member of the array. Once the array has been created, each one of its members is initialized with a 0 value. Most, if not all, of the time, you will need to change the value of each member to a value of your choice. This is referred to as initializing the array.

An array is primarily a variable; it is simply meant to carry more than one value. Like every other variable, an array can be initialized. There are two main techniques you can use to initialize an array. If you have declared an array as done above, to initialize it, you can access each one of its members and assign it a desired but appropriate value.

In math, if you create a series of values as X1, X2, X3, X4, and X5, each member of this series can be identified by its subscript number. In this case the subscripts are 1, 2, 3, 4, and 5. This subscript number is also called an index. In the case of an array also, each member of an array can be referred to by an incremental number called an index. A C# (like a C/C++) array is zero-based. This means that the first member of the series has an index of 0, the second has an index of 1. In math, the series would be represented as X0, X1, X2, X3, and X4. In C#, the index of a member of an array is written in its own square brackets. This is the notation you would use to

363

Page 364: Introduction to C#

locate each member. One of the actions you can take would consist of assigning it a value. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double[] number = new double[5];

number[0] = 12.44; number[1] = 525.38;

number[2] = 6.28; number[3] = 2448.32; number[4] = 632.04;

Console.WriteLine();}

}}

Besides this technique, you can also initialize the array as a whole when declaring it. To do this, on the right side of the declaration, before the closing semi-colon, type the values of the array members between curly brackets and separated by a comma. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double[] number = new double[5]{ 12.44, 525.38, 6.28, 2448.32, 632.04 };

Console.WriteLine();}

}}

If you use this second technique, you don't have to specify the number of items in the series. If you can leave all square brackets empty:

using System;

namespace CSharpLessons{

class Exercise{

static void Main(){

double[] number = new double[]{ 12.44, 525.38, 6.28, 2448.32, 632.04 };

364

Page 365: Introduction to C#

Console.WriteLine();}

}}

If you leave the square brackets empty, the compiler will figure out the number of items.

Practical Learning: Using the Members of an Array

1. To initialize arrays and use their members, change the Order.cs file as follows: 

using System;

class IceCream{

public const decimal BasePrice = 1.55M;

public void ProcessAnOrder(){

int dFlv = 0, dCont = 0, dIngrd = 0;int Scoops = 0;decimal PriceIngredient, TotalPrice;

string[] Flavor = new string[6]{ "Vanilla", "Cream of Cocoa",

"Organic Strawberry", "Butter Pecan", "Cherry Coke", "Chocolate Brownies" };

string[] Container = new string[3];Container[0] = "Cone";Container[1] = "Cup";Container[2] = "Bowl";

string[] Ingredient = new string[]{"No Ingredient", "Peanuts",

"M & M", "Cookies" };

Console.WriteLine("Ice Cream Vendor Machine");

do {

try {

Console.WriteLine("What type of flavor do you want?");Console.WriteLine("1 - {0}", Flavor[0]);Console.WriteLine("2 - {0}", Flavor[1]);Console.WriteLine("3 - {0}", Flavor[2]);Console.WriteLine("4 - {0}", Flavor[3]);Console.WriteLine("5 - {0}", Flavor[4]);Console.WriteLine("6 - {0}", Flavor[5]);

365

Page 366: Introduction to C#

Console.Write("Your Choice? " );dFlv = int.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You must enter a valid number and no other character!");

}

if( dFlv < 1 || dFlv > 6 )Console.WriteLine("Invalid Choice - Try Again!\n");

} while( dFlv < 1 || dFlv > 6 );

do {

try {

Console.WriteLine("What type of container do you want?");Console.WriteLine("1 - {0}", Container[0]);Console.WriteLine("2 - {0}", Container[1]);Console.WriteLine("3 - {0}", Container[2]);Console.Write("Your Choice? ");dCont = int.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You must enter a valid number and no other character!");

}

if( dCont < 1 || dCont > 3 )Console.WriteLine("Invalid Choice - Try Again!");

} while( dCont < 1 || dCont > 3 );

do {

try {

Console.WriteLine("Do you want an ingredient or not");Console.WriteLine("1 - {0}", Ingredient[0]);Console.WriteLine("2 - {0}", Ingredient[1]);Console.WriteLine("3 - {0}", Ingredient[2]);Console.WriteLine("4 - {0}", Ingredient[3]);Console.Write("Your Choice? ");dIngrd = int.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You must enter a valid number and no other character!");

}

if( dIngrd < 1 || dIngrd > 4 )Console.WriteLine("Invalid Choice - Try Again!");

} while( dIngrd < 1 || dIngrd > 4 );

366

Page 367: Introduction to C#

do{

try {

Console.Write("How many scoops(1, 2, or 3)? ");Scoops = int.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("You must enter a valid number and no other character!");}

if( Scoops < 1 || Scoops > 3 )Console.WriteLine("Invalid Choice - Try Again!");

} while( Scoops < 1 || Scoops > 3 );

if( dIngrd == 2 || dIngrd == 3 || dIngrd == 4 )PriceIngredient = 0.65M;

elsePriceIngredient = 0.00M;

TotalPrice = (BasePrice * Scoops) + PriceIngredient;

DisplayReceipt(dFlv, dCont, dIngrd, Scoops, TotalPrice);}

public void DisplayReceipt(int Flv, int Cont, int Ingrd,int spoons, decimal TotalPrice)

{Console.WriteLine("\nIce Cream Order");switch(Flv){

case 2:Console.WriteLine("Flavor: Cream of Cocoa");break;

case 3:Console.WriteLine("Flavor: Organic Strawberry");break;

case 4:Console.WriteLine("Flavor: Butter Pecan");break;

case 5:Console.WriteLine("Flavor: Cherry Coke");break;

case 6:Console.WriteLine("Flavor: Chocolate Brownies");break;

default:Console.WriteLine("Flavor: Vavilla");break;

}

switch(Cont){

case 2:Console.WriteLine("Container: Cup");

367

Page 368: Introduction to C#

break;case 3:

Console.WriteLine("Container: Bowl");break;

default:Console.WriteLine("Container: Cone");break;

}

switch(Ingrd){

case 2:Console.WriteLine("Ingredient: Peanuts");break;

case 3:Console.WriteLine("Ingredient: M & M");break;

case 4:Console.WriteLine("Ingredient: Cookies");break;

default:Console.WriteLine("Ingredient: None");break;

}

Console.WriteLine("Scoops: {0}", spoons);Console.WriteLine("Total Price: {0:C}\n", TotalPrice);

}}

2. Save the Order.cs file

3. To test the IceCream class, access the Exercise.cs file and change it as follows: 

using System;

class Exercise{

static void Main(){

IceCream IS = new IceCream();

IS.ProcessAnOrder();}

}

4. Save the Exercise.cs file

5. To test the application, open the Command Prompt and change to the folder in which you created the C# file

6. Type csc Order.cs Exercise.cs and press Enter

7. After compiling, type Exercise to execute and press Enter. Here is an example: 

Ice Cream Vendor MachineWhat type of flavor do you want?

368

Page 369: Introduction to C#

1 - Vanilla2 - Cream of Cocoa3 - Organic Strawberry4 - Butter Pecan5 - Cherry Coke6 - Chocolate BrowniesYour Choice? 8Invalid Choice - Try Again!

What type of flavor do you want?1 - Vanilla2 - Cream of Cocoa3 - Organic Strawb

369

Page 370: Introduction to C#

Techniques of Using Arrays 

Main()'s Argument 

Introduction

When a program starts, it looks for an entry point as the area of entrance. This is the role of the Main() function. In fact, a program, that is an executable program, starts by, and stops with, the Main() function. The way this works is that, at the beginning, the compiler enters he Main() function in a top-down approach, starting just after the opening curly bracket. If it finds a problem and judges that it is not worth continuing, it stops and lets you know. If, or as long as, it doesn't find a problem, it continues line after line, with the option to even call or execute a method in the same file or in another file. This process continues to the closing curly bracket "}". Once the compiler finds the closing bracket, the whole program has ended and stops.

If you want the user to provide additional information when executing your program, you can take care of this in the Main() function as this is the entry point of your program.

Practical Learning: Introducing the Command Line

1. In Notepad, start a new empty file and type the following: 

using System;

namespace CSharpLessons{ class Exercise {

static int Main(){ string FirstName = "James";

string LastName = "Weinberg"; double WeeklyHours = 36.50;

double HourlySalary = 12.58; string FullName = LastName + ", " + FirstName; double WeeklySalary = WeeklyHours * HourlySalary;

Console.WriteLine("Employee Payroll"); Console.WriteLine("Full Name: {0}", FullName);

Console.WriteLine("WeeklySalary: {0}", WeeklySalary.ToString("C"));

return 0;

 

370

Page 371: Introduction to C#

} }}

2. Save the file in a new folder named CommandLine1

3. Save the file itself as Exercise.cs in the CommandLine1 folder

4. To test the application, open the Command Prompt and change to the folder in which you created the C# file

5. Type csc Exercise.cs and press Enter

6. To execute the program, type the name Exercise and press Enter. This would produce: 

C:\CSharp Lessons\CommandLine1>csc Exercise.csMicrosoft (R) Visual C# .NET Compiler version 7.10.3052.4for Microsoft (R) .NET Framework version 1.1.4322Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

C:\CSharp Lessons\CommandLine1>ExerciseEmployee PayrollFull Name: Weinberg, JamesWeeklySalary: $459.17

C:\CSharp Lessons\CommandLine1>

7. Return to your text editor

 

Command Request from Main()

So far, to compile a program, we simply typed the csc command at the command prompt. Then, to execute a program, we typed its name at the prompt. If we distributed one of these programs, we would tell the user to type the name of the program at the command prompt. In some cases, you may want the user to type additional information besides the name of the program. To request additional information from the user, pass a string argument to the Main() function. The argument should be passed as an array and make sure you provide a name for the argument. Here is an example:

using System;

class ObjectName{ static int Main(string[] args) { return 0; }}

The reason you pass the argument as an array is so you can use as many values as you judge necessary. To provide values at the command prompt, the user types the name of the program followed by each necessary value. Here is an example:

371

Page 372: Introduction to C#

The values the user would provide are stored in the zero-based argument array without considering the name of the program. The first value (that is, after the name of the program) is stored at index 0, the second at index 1, etc. Based on this, the first argument is represented by args[0], the second is represented by args[1], etc.

Since the array argument (like all C# arrays) is based on the Array class of the System namespace, if you want to find out how many values the user supplied, you can call the Array.Length property.

Each of the values the user types is a string. If any one of them is not a string, you should/must convert its string first to the appropriate value.

 

Practical Learning: Passing an Argument to Main()

1. To pass an argument to Main(), change the file as follows: 

using System;

namespace CSharpLessons{ class Exercise {

static int Main(string[] Argument){ string FirstName;

string LastName; Double WeeklyHours;

Double HourlySalary; FirstName = Argument[0];

LastName = Argument[1]; WeeklyHours = Double.Parse(Argument[2]);

372

Page 373: Introduction to C#

HourlySalary = Double.Parse(Argument[3]);

string FullName = LastName + ", " + FirstName; Double WeeklySalary = WeeklyHours * HourlySalary;

Console.WriteLine("Employee Payroll"); Console.WriteLine("Full Name: {0}", FullName);

Console.WriteLine("WeeklySalary: {0}", WeeklySalary.ToString("C"));

return 0;}

}}

2. Save the file and switch to the command prompt

3. To compile the application, type csc Exercise.cs and press Enter

4. To execute the program, type Exercise followed by a first name, a last name, and two decimal values. An example would be Exercise Catherine Engolo 42.50 20.48 

5. Return to your text editor

Two-Dimensional Arrays 

Introduction

The arrays we used so far were made of a uniform series, where all members constituted a simple list, like a column of names on a piece of paper. Also, all items fit in one list. In some cases, you may want to divide the list in delimited sections. For example, if you create a list of names, you may want part of the list to include family members and another part of the list to include friends. Instead of creating a second list, you can add a second dimension to the list. In other words, you would like to create a list of a list, or one list inside of another list, although the list is still made of items with common characteristics.

A multi-dimensional array is a series of lists so that each list contains its own list. For example, if you create two lists of names, you would have an array of two lists. Each array or list would have its own list or array.

373

Page 374: Introduction to C#

Practical Learning: Introducing Multidimensional Arrays

1. Start a new file in Notepad and type the following: 

using System;

class DepartmentStore{

static int Main(){

long ItemID = 0;string Description = "Unknown";decimal Price = 0.00M;

Console.WriteLine("Receipt");Console.WriteLine("Item Number: {0}", ItemID);

Console.WriteLine("Description: {0}", Description);Console.WriteLine("Unit Price: {0:C}\n", Price);

return 0;}

}

2. Save the file in a new folder named DeptStore4

3. Save the file as Exercise.cs

4. Open the Command Prompt and change to the above DeptStore4

5. Compile the exercise with csc Exercise.cs and execute it with ExerciseThis would produce: 

ReceiptItem Number: 0Description: UnknownUnit Price: $0.00

6. Return to Notepad

Creation of a Two-Dimensional Array

The most basic multi-dimensional array is an array of an array, also referred to as two-dimensional. To create a two-dimensional array, declare the array variable as we did earlier but add a comma in the square brackets. The formula you would use is:

DataType[,] VariableName;

The pair of brackets is empty but must contain a comma. There are two ways you can use a two-dimensional array. If you are declaring the array variable but are not ready to initialize it, use the following formula:

DataType[,] VariableName = new DataType[Number1,Number2];

Based on this, type two integers separated by a comma in the right square brackets. Here is an example:

374

Page 375: Introduction to C#

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ string[,] Members = new string[2,4];}

}

In this declaration, the Members variable contains two lists. Each of the two lists contains 4 items. This means that the first list contains 4 items, the second list contains 4 items also. Therefore, the whole list is made of 8  items (2*4=8). Because the variable is declared as a string, each of the 8 items is a string.

You can initialize an array variable when declaring it. To do this, on the right side of the declaration, before the closing semi-colon, type an opening and a closing curly brackets. Inside of the brackets, include a pair of an opening and a closing curly brackets for each internal list of the array. Then, inside of a pair of curly brackets, provide a list of the values of the internal array, just as you would do for a one-dimensional array. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ String[,] members = new String[2,4]{

{"Celeste", "Mathurin", "Alex", "Germain"}, {"Jeremy", "Mathew", "Anselme", "Frederique"}

};

Console.WriteLine();}

}

If you use this technique to initialize an array, you can omit specifying the dimension of the array.

 

Practical Learning: Creating a Two-Dimensional Array

1. To create and use a two-dimensional array, change the file as follows: 

using System;

class DepartmentStore{

static int Main(){

375

Page 376: Introduction to C#

// The first list contains women's items// The other contains non-women items

long[,] ItemNumber = new long[2,5]{ { 947783, 934687, 973947, 987598, 974937 }, { 739579, 367583, 743937, 437657, 467945 } };

string[,] ItemName = new string[2,5]{ { "Women Double-faced wool coat","Women Floral Silk Tank Blouse","Women Push Up Bra","Women Chiffon Blouse","Women Bow Belt Skirtsuit" },

{ "Men Cotton Polo Shirt", "Children Cable-knit Sweater ",

"Children Bear Coverall Cotton", "Baby three-piece Set ",

"Girls Jeans with Heart Belt " } };decimal[,] UnitPrice = new decimal[2,5]{ { 275.25M, 180.05M, 50.00M, 265.35M, 245.55M },

{ 45.55M, 25.65M, 28.25M, 48.55M, 19.95M } };

long ItemID = 0;string Description = "Unknown";decimal Price = 0.00M;

Console.WriteLine("Receipt");Console.WriteLine("Item Number: {0}", ItemID);Console.WriteLine("Description: {0}", Description);Console.WriteLine("Unit Price: {0:C}\n", Price);

return 0;}

}

2. Save the file

Access to Members of a Two-Dimensional Array

To use the members of a two-dimensional array, you can access each item individually. For example, to initialize a two-dimensional array, you can access each member of the array and assign it a value. The external list is zero-based. In other words, the first list has an index of 0, the second list has an index of 1. Internally, each list is zero-based and behaves exactly like a one-dimensional array. To access a member of the list, type the name of the variable followed by its square brackets. In the brackets, type the index of the list, a comma, and the internal index of the member whose access you need. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ String[,] members = new String[2,4];

members[0,0] = "Celeste"; members[0,1] = "Mathurin";

members[0,2] = "Alex"; members[0,3] = "Germain";

376

Page 377: Introduction to C#

members[1,0] = "Jeremy"; members[1,1] = "Mathew";

members[1,2] = "Anselme"; members[1,3] = "Frederique";

Console.WriteLine();}

}

Practical Learning: Accessing Members of a Two-Dimensional Array

1. To retrieve the values of a two-dimensional array, change the file as follows: 

using System;

class DepartmentStore{

static int Main(){

// The first list contains women's items// The other contains non-women items

long[,] ItemNumber = new long[2,5]{ { 947783, 934687, 973947, 987598, 974937 }, { 739579, 367583, 743937, 437657, 467945 } };

string[,] ItemName = new string[2,5]{ { "Women Double-faced wool coat ","Women Floral Silk Tank Blouse","Women Push Up Bra","Women Chiffon Blouse","Women Bow Belt Skirtsuit" },

{ "Men Cotton Polo Shirt","Children Cable-knit Sweater","Children Bear Coverall Cotton","Baby three-piece Set","Girls Jeans with Heart Belt" } };

decimal[,] UnitPrice = new decimal[2,5]{ { 275.25M, 180.05M, 50.00M, 265.35M, 245.55M },{ 45.55M, 25.65M, 28.25M, 48.55M, 19.95M } };

long ItemID = 0;string Description = "Unknown";decimal Price = 0.00M;

// Order Processingtry {

Console.Write("Enter Item Number: ");ItemID = long.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("Invalid Number - The program will terminate\n");}

for(int i = 0; i < 2; i++){

377

Page 378: Introduction to C#

for(int j = 0; j < 5; j++){

if( ItemID == ItemNumber[i,j] ){

Description = ItemName[i,j];Price = UnitPrice[i,j];

}}

}

Console.WriteLine("Receipt");Console.WriteLine("Item Number: {0}", ItemID);Console.WriteLine("Description: {0}", Description);Console.WriteLine("Unit Price: {0:C}\n", Price);

return 0;}

}

2. Save, compile, and execute the application. Here is an example: 

Enter Item Number: 397494ReceiptItem Number: 397494Description: UnknownUnit Price: $0.00

3. Here is another example of executing the application: 

Enter Item Number: 367583ReceiptItem Number: 367583Description: Children Cable-knit SweaterUnit Price: $25.65

4. Return to Notepad

Multidimensional Arrays 

Introduction

Beyond two dimensions, you can create a variable that represents various lists and each list contains various internal lists. This is referred to as a multidimensional array.

 

Creation of a Multidimensional Array

To create a mutidimensional array, add as many commas in the square brackets as you judge them necessary. Here is an example of a three-dimensional array initialized:

using System;

378

Page 379: Introduction to C#

namespace CSharpLessons{ class Exercise {

static void Main(){ double[,,] number = new double[2,3,5]{ { { 12.44, 525.38, -6.28,

2448.32, 632.04}, {-378.05, 48.14, 634.18, 762.48, 83.02},

{ 64.92, -7.44, 86.74, -534.60, 386.73} },

{ { 48.02, 120.44, 38.62, 526.82,1704.62},

{ 56.85, 105.48, 363.31, 172.62, 128.48},

{ 906.68, 47.12,-166.07, 4444.26, 408.62} },

};

Console.WriteLine("Number of items {0}", number.Length); Console.WriteLine("Number of items {0}", number.Rank);

Console.WriteLine();}

}

In this example, we are creating 2 groups of items. Each of the two groups is made of three lists. Each list contains 5 numbers. This would produce:

Number of items 30Number of items 3

Access to Members of a Multidimensional Array

To locate each member of a multidimensional array, type the name of the array followed by the opening square bracket, followed by the 0-based first dimension, followed by a comma. Continue with each dimension and end with the closing square bracket. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double[,,] number = new double[2,3,5]{ { { 12.44, 525.38, -6.28,

2448.32, 632.04}, {-378.05, 48.14, 634.18, 762.48, 83.02},

{ 64.92, -7.44, 86.74, -534.60, 386.73} },

{ { 48.02, 120.44, 38.62, 526.82,1704.62},

379

Page 380: Introduction to C#

{ 56.85, 105.48, 363.31, 172.62, 128.48},

{ 906.68, 47.12,-166.07, 4444.26, 408.62} },

};

Console.WriteLine("Number[0][0][0] = {0}", number[0,0,0]); Console.WriteLine("Number[0][0][1] = {0}", number[0,0,1]); Console.WriteLine("Number[0][0][2] = {0}", number[0,0,2]); Console.WriteLine("Number[0][0][3] = {0}", number[0,0,3]); Console.WriteLine("Number[0][0][4] = {0}\n", number[0,0,4]);

Console.WriteLine("Number[0][1][0] = {0}", number[0,1,0]); Console.WriteLine("Number[0][1][1] = {0}", number[0,1,1]); Console.WriteLine("Number[0][1][2] = {0}", number[0,1,2]); Console.WriteLine("Number[0][1][3] = {0}", number[0,1,3]);

Console.WriteLine("Number[0][1][4] = {0}\n", number[0,1,4]);

Console.WriteLine("Number[0][2][0] = {0}", number[0,2,0]); Console.WriteLine("Number[0][2][1] = {0}", number[0,2,1]); Console.WriteLine("Number[0][2][2] = {0}", number[0,2,2]); Console.WriteLine("Number[0][2][3] = {0}", number[0,2,3]);

Console.WriteLine("Number[0][2][4] = {0}\n", number[0,2,4]);

Console.WriteLine("Number[1][0][0] = {0}", number[1,0,0]); Console.WriteLine("Number[1][0][1] = {0}", number[1,0,1]); Console.WriteLine("Number[1][0][2] = {0}", number[1,0,2]); Console.WriteLine("Number[1][0][3] = {0}", number[1,0,3]); Console.WriteLine("Number[1][0][4] = {0}\n", number[1,0,4]);

Console.WriteLine("Number[1][1][0] = {0}", number[1,1,0]); Console.WriteLine("Number[1][1][1] = {0}", number[1,1,1]);

Console.WriteLine("Number[1][1][2] = {0}", number[1,1,2]); Console.WriteLine("Number[1][1][3] = {0}", number[1,1,3]);

Console.WriteLine("Number[1][1][4] = {0}\n", number[1,1,4]);

Console.WriteLine("Number[1][2][0] = {0}", number[1,2,0]); Console.WriteLine("Number[1][2][1] = {0}", number[1,2,1]); Console.WriteLine("Number[1][2][2] = {0}", number[1,2,2]); Console.WriteLine("Number[1][2][3] = {0}", number[1,2,3]);

Console.WriteLine("Number[1][2][4] = {0}\n", number[1,2,4]);

Console.WriteLine("Number of items {0}", number.Length); Console.WriteLine("Number of items {0}", number.Rank);

Console.WriteLine();}

}

This would produce:

Number[0][0][0] = 12.44Number[0][0][1] = 525.38Number[0][0][2] = -6.28Number[0][0][3] = 2448.32Number[0][0][4] = 632.04

380

Page 381: Introduction to C#

Number[0][1][0] = -378.05Number[0][1][1] = 48.14Number[0][1][2] = 634.18Number[0][1][3] = 762.48Number[0][1][4] = 83.02

Number[0][2][0] = 64.92Number[0][2][1] = -7.44Number[0][2][2] = 86.74Number[0][2][3] = -534.6Number[0][2][4] = 386.73

Number[1][0][0] = 48.02Number[1][0][1] = 120.44Number[1][0][2] = 38.62Number[1][0][3] = 526.82Number[1][0][4] = 1704.62

Number[1][1][0] = 56.85Number[1][1][1] = 105.48Number[1][1][2] = 363.31Number[1][1][3] = 172.62Number[1][1][4] = 128.48

Number[1][2][0] = 906.68Number[1][2][1] = 47.12Number[1][2][2] = -166.07Number[1][2][3] = 4444.26Number[1][2][4] = 408.62

Number of items 30Number of items 3

Jagged Arrays 

Introduction

A jagged array is just an array of arrays, or an array of arrays of arrays, etc. To create a jagged array, use a combination of square brackets for each dimension. The formula used is:

DataType[][] VariableName;

Each of the square brackets is used in any of the ways we have introduced arrays so far. This means that the first square bracket can be used as its own one-dimensional array or as a multi-dimensional array. Here is an example:

long[2][5] Distances;

This declares a variable that represents two arrays and each array internally contains 5 arrays. The first square bracket can also be used as its own multidimensional array. Here is an example:

long[2,4][5] Distances;

In the same way, the second square bracket can be used as a single or a multidimensional array. Here is an example:

381

Page 382: Introduction to C#

long[2,4][5,12,8] Distances;

Practical Learning: Introducing Jagged Arrays

1. Start a new file in Notepad and type the following: 

using System;

class DepartmentStore{

static int Main(){

long ItemID = 0;string Description = "Unknown";decimal Price = 0.00M;

Console.WriteLine("Receipt");Console.WriteLine("Item Number: {0}", ItemID);

Console.WriteLine("Description: {0}", Description);Console.WriteLine("Unit Price: {0:C}\n", Price);

return 0;}

}

2. Save the file in a new folder named DeptStore5

3. Save the file as Exercise.cs

4. Open the Command Prompt and change to the above DeptStore5

5. Compile the exercise with csc Exercise.cs and execute it with ExerciseThis would produce: 

ReceiptItem Number: 0Description: UnknownUnit Price: $0.00

6. Return to Notepad

Initialization of a Jagged Array

When declaring a jagged array, you can allocate memory for it using the new operator followed by the data type of the array and the same combination of square brackets used to the left of the assignment operator. The first square of the square brackets on the right side of the assignment operator must contain the external dimension of the array. The second pair of square brackets must be left empty. Here is an example:

using System;

namespace CSharpLessons{

class Exercise{

382

Page 383: Introduction to C#

static void Main(){

string[][] Members = new string[2][];

}}

}

To initialize a jagged array, when declaring the variable, on the right side of the second pair of square brackets, provide an opening and a closing curly brackets, then create each list in its own pair of curly brackets. At the beginning of each list, you must allocate memory for the list with the new operator. Here is an example:

using System;

namespace CSharpLessons{

class Exercise{

static void Main(){

string[][] Members = new string[2][]{new string[]{"Celeste", "Mathurin", "Alex",

"Germain"},new string[]{"Jeremy", "Mathew", "Anselme",

"Frederique"} };

Console.WriteLine();}

}}

If you initialize the array this way, you can omit specifying the dimension of the external array. With a jagged array, you can also initialize its internal array individually. To do this, access each internal array by its zero-based index. Here is an example:

namespace CSharpLessons{

class Exercise{

static void Main(){

string[][] Members = new string[2][];

Members[0] = new string[]{"Celeste", "Mathurin", "Alex", "Germain"};

Members[1] = new string[]{"Jeremy", "Mathew", "Anselme", "Frederique"};

Console.WriteLine();}

}}

Practical Learning: Initializing a Jagged Array

383

Page 384: Introduction to C#

1. To declare and initialize jagged arrays, change the file as follows: 

using System;

class DepartmentStore{

static int Main(){

// Each of the following variable arrays is structured as [2][2][4].// Each variable represents:// A/ Two major lists: The first major list represents women items,// the second major list represents men

items,// B/ Two minor lists.// Each of the major lists contains two minor lists:// a/ The first minor list of the first major list contains adult women

items// The second minor list of the first major list contains girls

items// b/ The first minor list of the second major list contains adult men

items// The second minor list of the second major list contains boys

items// C/ Each minor list contains four items

long[][][] ItemNumber = new long[][][]{

new long[][]{new long[]{947783, 934687, 973947, 987598, 974937},new long[]{743765, 747635, 765473, 754026, 730302}},

new long[][]{new long[]{209579, 267583, 248937, 276057, 267945},new long[]{ 409579, 467583, 448937, 476057, 467945}} };

string[][][] ItemName =new string[][][]{

new string[][]{new string[]{ "Double-faced wool coat", "Floral Silk Tank Blouse",

"Push Up Bra", "Chiffon Blouse", "Bow Belt Skirtsuit" },

new string[]{ "Cable-knit Sweater", "Jeans with Heart Belt",

"Fashionable mini skirt", "Double Dry Pants",

"Romantic Flower Dress" } },new string[][]{

new string[]{ "Cotton Polo Shirt", "Pure Wool Cap",

"Striped Cotton Shirt", "Two-Toned Ribbed Crewneck", "Chestnut Italian Shoes" },

new string[]{ "Under Collar and Placket Jacket", "Country Coat Rugged Wear",

"Carpenter Jeans", "Double-Cushion Tennis Shoes",

384

Page 385: Introduction to C#

"Stitched Center-Bar Belt" }} };decimal[][][] UnitPrice =

new decimal[2][][]{new decimal[][]{

new decimal[]{ 275.25M, 180.00M, 50.00M, 265.00M, 245.55M },

new decimal[]{ 45.55M, 25.65M, 34.55M, 28.55M, 24.95M }},

new decimal[][]{new decimal[]{ 45.75M, 25.00M, 65.55M, 9.75M,

165.75M },new decimal[]{ 265.15M, 35.55M, 24.95M, 48.75M,

32.50M }} };

long ItemID = 0;string Description = "Unknown";decimal Price = 0.00M;

Console.WriteLine("Receipt");Console.WriteLine("Item Number: {0}", ItemID);Console.WriteLine("Description: {0}", Description);Console.WriteLine("Unit Price: {0:C}\n", Price);

return 0;}

}

2. Save the file

Access to Members of a Jagged Array

As done for a multidimensional array, each member of a jagged array can be access with a multiple index, depending on how the array was created. Both the external and the internal lists are zero-based. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ string[][] Members = new string[2][];

Members[0] = new string[]{"Celeste", "Mathurin", "Alex", "Germain"}; Members[1] = new string[]{"Jeremy", "Mathew", "Anselme", "Frederique"};

Console.WriteLine("Member 1: {0}", Members[0][0]); Console.WriteLine("Member 2: {0}", Members[0][1]); Console.WriteLine("Member 3: {0}", Members[0][2]); Console.WriteLine("Member 4: {0}", Members[0][3]); Console.WriteLine("Member 5: {0}", Members[1][0]); Console.WriteLine("Member 6: {0}", Members[1][1]); Console.WriteLine("Member 7: {0}", Members[1][2]); Console.WriteLine("Member 8: {0}\n", Members[1][3]);

385

Page 386: Introduction to C#

} }}

This would produce:

Member 1: CelesteMember 2: MathurinMember 3: AlexMember 4: GermainMember 5: JeremyMember 6: MathewMember 7: AnselmeMember 8: Frederique

Press any key to continue . . .

Practical Learning: Using a Jagged Array

1. To process the members of a jagged array, make the following changes to the file: 

using System;

class DepartmentStore{

static int Main(){

// Each of the following variable arrays is structured as [2][2][4].// Each variable represents:// A/ Two major lists: The first major list represents women items,// the second major list represents men items,// B/ Two minor lists.// Each of the major lists contains two minor lists:// a/ The first minor list of the first major list contains adult women

items// The second minor list of the first major list contains girls items// b/ The first minor list of the second major list contains adult men

items// The second minor list of the second major list contains boys items// C/ Each minor list contains four items

long[][][] ItemNumber = new long[][][]{

new long[][]{new long[]{947783, 934687, 973947, 987598, 974937},new long[]{743765, 747635, 765473, 754026, 730302}},

new long[][]{new long[]{209579, 267583, 248937, 276057, 267945},new long[]{ 409579, 467583, 448937, 476057, 467945}} };

string[][][] ItemName =new string[][][]{

new string[][]{new string[]{ "Double-faced wool coat", "Floral Silk Tank Blouse",

"Push Up Bra", "Chiffon Blouse",

386

Page 387: Introduction to C#

"Bow Belt Skirtsuit" },new string[]{ "Cable-knit Sweater", "Jeans with Heart Belt",

"Fashionable mini skirt", "Double Dry Pants",

"Romantic Flower Dress" } },new string[][]{

new string[]{ "Cotton Polo Shirt", "Pure Wool Cap",

"Striped Cotton Shirt", "Two-Toned Ribbed Crewneck", "Chestnut Italian Shoes" },

new string[]{ "Under Collar and Placket Jacket", "Country Coat Rugged Wear",

"Carpenter Jeans", "Double-Cushion Tennis Shoes",

"Stitched Center-Bar Belt" }} };decimal[][][] UnitPrice =

new decimal[2][][]{new decimal[][]{

new decimal[]{ 275.25M, 180.00M, 50.00M, 265.00M, 245.55M },

new decimal[]{ 45.55M, 25.65M, 34.55M, 28.55M, 24.95M }},

new decimal[][]{new decimal[]{ 45.75M, 25.00M, 65.55M, 9.75M,

165.75M },new decimal[]{ 265.15M, 35.55M, 24.95M, 48.75M,

32.50M }} };

long ItemID = 0;string Description = "Unknown";decimal Price = 0.00M;string Category = "Category";

// Order Processingtry {

Console.Write("Enter Item Number: ");ItemID = long.Parse(Console.ReadLine());

}catch(FormatException){

Console.WriteLine("Invalid Number - The program will terminate\n");}

for(int i = 0; i < 2; i++){

for(int j = 0; j < 2; j++){

for(int k = 0; k < 5; k++){

if( ItemID == ItemNumber[i][j][k] ){

Description = ItemName[i][j][k];

387

Page 388: Introduction to C#

Price = UnitPrice[i][j][k];

if( ItemID >= 900000 )Category = "Women";

else if( ItemID >= 700000 )Category = "Girls";

else if( ItemID >= 400000 )Category = "Boys";

elseCategory = "Men";

}}

}}

Console.WriteLine("Receipt");Console.WriteLine("Item Number: {0}", ItemID);Console.WriteLine("Description: {0} - {1}", Category, Description);Console.WriteLine("Unit Price: {0:C}\n", Price);

return 0;}

}

2. Save, compile, and execute the file

The .NET Framework Support of Arrays 

Introduction

The techniques we have used to create arrays so far are part of the traditional routines used in many languages. The Microsoft .NET Framework goes further than that. Every time you declare an array, it inherits from a class called Array. The Array class provides support for any array using various properties and methods. After creating an array as we have done so far, you may want to rearrange the list based on one of the items. You may want to look for a particular item to find if it exists; then, if it exists, where it is located. The Array class provides many other routines to perform such operations.

 

Item Addition

As done so far, to add an item to a list, you specify its index and assign the desired value. Here is an example:

class Exercise{ static void Main() {

string[] EmployeeName = new string[4];

EmployeeName[0] = "Joan Fuller";EmployeeName[1] = "Barbara Boxen";EmployeeName[2] = "Paul Kumar";

388

Page 389: Introduction to C#

EmployeeName[3] = "Bertrand Entire"; }}

Alternatively, you can use the SetValue() method to add a new item to the list. This method has 8 versions. The first argument is the value that needs to be added to the list, the other argument(s) is(are) used for the index. Here is an example:

class Exercise{ static void Main() {

string[] EmployeeName = new string[4];

EmployeeName.SetValue("Joan Fuller", 0);EmployeeName.SetValue("Barbara Boxen", 1);EmployeeName.SetValue("Paul Kumar", 2);EmployeeName.SetValue("Bertrand Entire", 3);

}}

 

Item Retrieval

To locate an item of an array, we were using its index. Here is an example:

class Exercise{ static void Main() {

string[] EmployeeName = new string[4];

EmployeeName[0] = "Joan Fuller";EmployeeName[1] = "Barbara Boxen";EmployeeName[2] = "Paul Kouma";EmployeeName[3] = "Bertand Entire";

Console.WriteLine("Employees Records");Console.WriteLine("Employee 1: {0}", EmployeeName[0]);Console.WriteLine("Employee 2: {0}", EmployeeName[1]);Console.WriteLine("Employee 3: {0}", EmployeeName[2]);Console.WriteLine("Employee 4: {0}", EmployeeName[3]);

}}

Alternatively, the Array class provides the GetValue() method, also loaded with 8 versions. Here is an example:

class Exercise{ static void Main() {

string[] EmployeeName = new string[4];

EmployeeName.SetValue("Joan Fuller", 0);EmployeeName.SetValue("Barbara Boxen", 1);EmployeeName.SetValue("Paul Kumar", 2);

389

Page 390: Introduction to C#

EmployeeName.SetValue("Bertrand Entire", 3);

Console.WriteLine("Employees Records");Console.WriteLine("Employee 1: {0}", EmployeeName.GetValue(0));Console.WriteLine("Employee 2: {0}", EmployeeName.GetValue(1));Console.WriteLine("Employee 3: {0}", EmployeeName.GetValue(2));Console.WriteLine("Employee 4: {0}", EmployeeName.GetValue(3));

}}

 

The Length of an Array

Once an array has been created, you can use it as you see fit. We saw that, when creating an array, you can specify the number of its members, and you must do this if you are not initializing the array. As we saw already, if you are initializing an array when creating it, you don't have to specify its dimension. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double[] number = new double[]{ 12.44, 525.38, 6.28, 2448.32, 632.04,

-378, 48, 6348, 762, 83, 64, -7, 86, 534, -6, 386, 73, 5, -284, 3654, 671, 34,

693};

Console.WriteLine();}

}

Some time in your program, you may want to know how many members are in this type of array. You could get dizzy trying to count them and you may make a mistake with the count. All of the arrays used in C# derive from a class called Array. This class provides a property called Length. To find out how many members an array has, you can access its Length property. Here is an example:

using System;

namespace CSharpLessons{ class Exercise {

static void Main(){ double[] number = new double[]{ 12.44, 525.38, 6.28, 2448.32, 632.04,

-378, 48, 6348, 762, 83, 64, -7, 86, 534, -6, 386, 73, 5, -284, 3654, 671, 34,

693};

Console.WriteLine("Number of items {0}", number.Length);

390

Page 391: Introduction to C#

Console.WriteLine();}

}

This would produce:

Number of items 23

The Dimensions of an Array

We have seen that the square brackets are used to specify to the compiler that you are declaring an array. If you are creating a one-dimensional array, we saw that you can type a number in the square bracket. If you are creating a two-dimensional array, you type two numbers separated by a comma in the second pair of square brackets. Each number, whether it is one, two, or more is a place holder for what is referred to a dimension. In other words, a one dimensional array has a dimension of one. A two-dimensional array has a dimension of 2.

To find out the dimension of an array, the Array class, which is the parent of all arrays, provides the Rank property. Therefore, to know the dimension of an existing array, you can access its Rank.

 

The Bounds of an Array

To better manage an array, the compiler must always be able to locate its highest and its lowest members. This is particularly important because an array must have a fixed size (by the way, using some gymnastic, an array can grow or shrink, a feature that is not available in C/C++ but can be managed in C# using the .NET Framework).

The lowest member of an array can be located using the Array.GetLowerBound() method. Its syntax is:

public int GetLowerBound(int dimension);

The highest member of an array can be located using the Array.GetUpperBound() method. Its syntax is:

public int GetUpperBound(int dimension);

In both cases, the dimension argument is the rank of the array. For a single-dimensional array, as those we have always used so far, this parameter must have the value of 0;

 

Previous Copyright © 2004-2006 FunctionX, Inc. Next

391

Page 392: Introduction to C#

 

Arrays and Classes 

Primitive Arrays as Fields 

Introduction

As we have used so far, an array is primarily a variable. As such, it can be declared as a field. To create a field as an array, you can declare it like a normal array in the body of the class. Here is an example:

using System;

namespace Arithmetic{

class NumberCrunching{

double[] number;}

}

Practical Learning: Introducing Primitive Arrays as Fields of a Class

1. Start Notepad and, in the empty file, type the following: 

using System;

namespace VendingMachine{ // This class is used to create an ice cream // And to process an order sealed class IceCream { }}

2. Save the file in a new folder named IceCream3

3. Save the file itself as IceCream.cs in the IceCream3 folder

4. Start a new instance of Notepad and type the following:

 

392

Page 393: Introduction to C#

 

using System;

// This is the main class used to manage the applicationclass Exercise{ static int Main() { return 0; }}

5. Save the file as Exercise.cs in a the above IceCream3 folder

6. To declare fields as arrays, access the IceCream.cs file and change it as follows: 

using System;

namespace VendingMachine{ // This class is used to create an ice cream // And to process an order sealed class IceCream { // These arrays are used to build the components of various ice creams private string[] Flavor; private string[] Container; private string[] Ingredient; }}

7. Save the file

A Primitive Array as a Field

Like any field, when an array has been declared as a member variable, it is made available to all the other members of the same class. You can use this feature to initialize the array in one method and let other methods use the initialized variable. This also means that you don't have to pass the array as argument nor do you have to explicitly return it from a method.

After or when declaring an array, you must make sure you allocate memory for it prior to using. Unlike C++, you can allocate memory for an array when declaring it. Here is an example:

using System;

namespace Arithmetic{

class NumberCrunching{

double[] number = new double[5];}

}

393

Page 394: Introduction to C#

You can also allocate memory for an array field in a constructor of the class. Here is an example:

using System;

namespace Arithmetic{

class NumberCrunching{

double[] Number;

public NumberCrunching(){

Number = new double[5];}

}}

If you plan to use the array as soon as the program is running, you can initialize it using a constructor or a method that you know would be before the array can be used. Here is an example:

using System;

namespace Arithmetic{

class NumberCrunching{

double[] Number = new double[5];

public NumberCrunching(){

Number[0] = 12.44;Number[1] = 525.38;Number[2] = 6.28;Number[3] = 2448.32;Number[4] = 632.04;

}}

}

  

Practical Learning: Creating Fields as Primitive Arrays in a Class

1. To initialize the arrays of the class, change the IceCream.cs file as follows: 

using System;

namespace VendingMachine{ // This class is used to create an ice cream // And to process an order

394

Page 395: Introduction to C#

sealed class IceCream { // These arrays are used to build the components of various ice creams // In C#, we can allocate an array's memory in the body of the class private string[] Flavor = new string[10]; private string[] Container; private string[] Ingredient;

// This default constructor is the best place for us to initialize the array public IceCream() { Flavor[0] = "Vanilla";

Flavor[1] = "Cream of Cocoa"; Flavor[2] = "Chocolate Chip"; Flavor[3] = "Organic Strawberry";

Flavor[4] = "Butter Pecan"; Flavor[5] = "Cherry Coke"; Flavor[6] = "Chocolate Brownies"; Flavor[7] = "Caramel Au Lait"; Flavor[8] = "Chunky Butter"; Flavor[9] = "Chocolate Cookie";

Ingredient = new string[4]; Ingredient[0] = "No Ingredient"; Ingredient[1] = "Peanuts"; Ingredient[2] = "M & M"; Ingredient[3] = "Cookies";

Container = new string[3]; Container[0] = "Cone"; Container[1] = "Cup"; Container[2] = "Bowl";

} }}

2. Save the file

Use of a Primitive Array as a Field

Remember that, after an array is made a field, it can be used by any other member of the same class. Based on this, you can use a member of the same class to request values that would initialize it. You can also use another method to explore the array. Here is an example:

using System;

namespace Arithmetic{

class NumberCrunching{

double[] Number = new double[5];

public NumberCrunching()

395

Page 396: Introduction to C#

{Number[0] = 12.44;Number[1] = 525.38;Number[2] = 6.28;Number[3] = 2448.32;Number[4] = 632.04;

}

public void DisplayNumber(){

for (int i = 0; i < 5; i++)Console.WriteLine("Number {0}: {1}", i, Number[i]);

}}

class Program{

static void Main(){

NumberCrunching lstNumber = new NumberCrunching();

lstNumber.DisplayNumber();}

}}

Practical Learning: Using Primitive Arrays in a Class

1. To use the arrays throughout the class, change the IceCream.cs file as follows: 

using System;

namespace VendingMachine{ // This class is used to create and manage an ice cream // and to process an order sealed class IceCream { // This is the base price of an ice cream

// Optional values may be added to itpublic const decimal BasePrice = 1.55M;

// These arrays are used to build the components of various ice creams // In C#, we can allocate an array's memory in the body of the class private string[] Flavor = new string[10]; private string[] Container; private string[] Ingredient;

// Additional factor used to process an ice cream orderprivate int Scoops;private decimal TotalPrice;

// This default constructor is the best place for us to initialize the array public IceCream() {

396

Page 397: Introduction to C#

Flavor[0] = "Vanilla"; Flavor[1] = "Cream of Cocoa"; Flavor[2] = "Chocolate Chip"; Flavor[3] = "Organic Strawberry"; Flavor[4] = "Butter Pecan"; Flavor[5] = "Cherry Coke"; Flavor[6] = "Chocolate Brownies"; Flavor[7] = "Caramel Au Lait"; Flavor[8] = "Chunky Butter"; Flavor[9] = "Chocolate Cookie";

Ingredient = new string[4]; Ingredient[0] = "No Ingredient"; Ingredient[1] = "Peanuts"; Ingredient[2] = "M & M"; Ingredient[3] = "Cookies";

Container = new string[3]; Container[0] = "Cone";

Container[1] = "Cup"; Container[2] = "Bowl";

}

// This method requests a flavor from the user and returns the choicepublic int ChooseFlavor(){ int Choice = 0;

// Make sure the user selects a valid number that represents a flavor... do

{// In case the user types a symbol that is not a numbertry {

Console.WriteLine("What type of flavor do you want?"); for(int i = 0; i < Flavor.Length; i++)

Console.WriteLine("{0} - {1}", i+1, Flavor[i]); Console.Write("Your Choice? " );

Choice = int.Parse(Console.ReadLine());}catch(FormatException) // display an appropriate message{ Console.WriteLine("You must enter a valid number and no other character!");}

// If the user typed an invalid number out of the allowed range// let him or her know and provide another chanceif( Choice < 1 || Choice > Flavor.Length ) Console.WriteLine("Invalid Choice - Try Again!\n");

} while( Choice < 1 || Choice > Flavor.Length );

// Return the numeric choice that the user made return Choice;

}

// This method allows the user to select a container

397

Page 398: Introduction to C#

public int ChooseContainer(){

int Choice = 0;

// Make sure the user selects a valid number that represents a container do

{// If the user types a symbol that is not a numbertry {

Console.WriteLine("What type of container do you want?"); for(int i = 0; i < Container.Length; i++) Console.WriteLine("{0} - {1}", i+1, Container[i]); Console.Write("Your Choice? "); Choice = int.Parse(Console.ReadLine());}catch(FormatException) // display an appropriate message{ Console.WriteLine("You must enter a valid number and no other character!");}

// If the user typed an invalid number out of the allowed range// let him or her know and provide another chanceif( Choice < 1 || Choice > Container.Length )

Console.WriteLine("Invalid Choice - Try Again!"); } while( Choice < 1 || Choice > Container.Length );

// Return the numeric choice that the user made return Choice;

}

public int ChooseIngredient(){

int Choice = 0;

do {

try {

Console.WriteLine("Do you want an ingredient or not"); for(int i = 0; i < Ingredient.Length; i++)

Console.WriteLine("{0} - {1}", i+1, Ingredient[i]); Console.Write("Your Choice? "); Choice = int.Parse(Console.ReadLine());}catch(FormatException){ Console.WriteLine("You must enter a valid number and no other character!");}

if( Choice < 1 || Choice > Ingredient.Length ) Console.WriteLine("Invalid Choice - Try Again!");

} while( Choice < 1 || Choice > Ingredient.Length );

return Choice;}

398

Page 399: Introduction to C#

public void SpecifyNumberOfScoops(){

do {

try {

Console.Write("How many scoops(1, 2, or 3)? "); Scoops = int.Parse(Console.ReadLine());}catch(FormatException){ Console.WriteLine("You must enter a valid number and no other character!");}

if( Scoops < 1 || Scoops > 3 ) Console.WriteLine("Invalid Choice - Try Again!");

} while( Scoops < 1 || Scoops > 3 );}

// This method is used to process a customer order// It uses the values of the above methodspublic void ProcessAnOrder(){

int ChoiceFlavor; int ChoiceContainer; int ChoiceIngredient;

decimal PriceIngredient, PriceScoop;

// Let the user know that this is a vending machine Console.WriteLine("Ice Cream Vending Machine");

// Let the user select the components of the ice cream ChoiceFlavor = ChooseFlavor();

ChoiceContainer = ChooseContainer(); ChoiceIngredient = ChooseIngredient(); SpecifyNumberOfScoops();

// If the user selects an ingredient instead of "No Ingredient", // add $0.50 to the order

if( ChoiceIngredient == 2 || ChoiceIngredient == 3 || ChoiceIngredient == 4 )PriceIngredient = 0.50M;

elsePriceIngredient = 0.00M;

// Instead of multiplying a number scoops to a value, // We will use an incremental value depending on the number of scoops

if( Scoops == 1 )PriceScoop = 0.65M;

else if( Scoops == 2 )PriceScoop = 1.05M;

elsePriceScoop = 1.55M;

// Calculate the total price of the ice cream TotalPrice = BasePrice + PriceScoop + PriceIngredient;

399

Page 400: Introduction to C#

// Create the ice cream...

// And display a receipt to the user DisplayReceipt(ref ChoiceFlavor, ref ChoiceContainer, ref ChoiceIngredient);

}

// This method is used to display a receipt to the userpublic void DisplayReceipt(ref int Flv, ref int Cnt, ref int Igr){

Console.WriteLine("\nIce Cream Order"); Console.WriteLine("Flavor: {0}", Flavor[Flv-1]); Console.WriteLine("Container: {0}", Container[Cnt-1]);

Console.WriteLine("Ingredient: {0}", Ingredient[Igr-1]); Console.WriteLine("Scoops: {0}", Scoops); Console.WriteLine("Total Price: {0:C}\n", TotalPrice);}

}}

2. Save the IceCream.cs file

3. Access the Exercise.cs file and change it as follows: 

using System;

namespace VendingMachine{ // This is the main class used to manage the application class Exercise { static int Main() {

VendingMachine.IceCream IS = new VendingMachine.IceCream();

IS.ProcessAnOrder(); return 0;

} }}

4. Save the Exercise.cs file

5. Open the Command Prompt and change to the directory that contains the above Exercise.cs file

6. To compile it, type csc /out:"Ice Cream Vending Machine".exe IceCream.cs Exercise.cs and press Enter

7. To execute it, type "Ice Cream Vending Machine" and press Enter. Here is an example: 

C:\>CD CSharp Lessons\IceCream3

C:\CSharp Lessons\IceCream3>csc /out:"Ice Cream Vending Machine".exe IceCream.cs Exercise.csMicrosoft (R) Visual C# .NET Compiler version 7.10.3052.4

400

Page 401: Introduction to C#

for Microsoft (R) .NET Framework version 1.1.4322Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

C:\CSharp Lessons\IceCream3>"Ice Cream Vending Machine"Ice Cream Vending MachineWhat type of flavor do you want?1 - Vanilla2 - Cream of Cocoa3 - Chocolate Chip4 - Organic Strawberry5 - Butter Pecan6 - Cherry Coke7 - Chocolate Brownies8 - Caramel Au Lait9 - Chunky Butter10 - Chocolate CookieYour Choice? 8What type of container do you want?1 - Cone2 - Cup3 - BowlYour Choice? 2Do you want an ingredient or not1 - No Ingredient2 - Peanuts3 - M & M4 - CookiesYour Choice? 6Invalid Choice - Try Again!Do you want an ingredient or not1 - No Ingredient2 - Peanuts3 - M & M4 - CookiesYour Choice? gYou must enter a valid number and no other character!Invalid Choice - Try Again!Do you want an ingredient or not1 - No Ingredient2 - Peanuts3 - M & M4 - CookiesYour Choice? 2How many scoops(1, 2, or 3)? 2

Ice Cream OrderFlavor: Caramel Au LaitContainer: CupIngredient: PeanutsScoops: 2Total Price: $3.10

C:\CSharp Lessons\IceCream3>

8. Return to Notepad

401

Page 402: Introduction to C#

foreach Item in the Series

In the previous lessons, we used the traditional techniques of locating each item in an array. Here is an example:

class Exercise{ static int Main() {

string[] EmployeeName = new string[4];

EmployeeName[0] = "Joan Fuller";EmployeeName[1] = "Barbara Boxen";EmployeeName[2] = "Paul Kumar";EmployeeName[3] = "Bertrand Entire";

Console.WriteLine("Employees Records");Console.WriteLine("Employee 1: {0}", EmployeeName[0]);Console.WriteLine("Employee 2: {0}", EmployeeName[1]);Console.WriteLine("Employee 3: {0}", EmployeeName[2]);Console.WriteLine("Employee 4: {0}", EmployeeName[3]);

return 0; }}

To scan a list, the C# language provides two keyword, foreach and in, with the following syntax:

foreach (type identifier in expression) statement<p c

402

Page 403: Introduction to C#

ms+=

403


Recommended