Introduction To C#
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 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:
class Car { }
                                                                                                         2
 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
 as "static".
 The most regularly used method of a C# program is called Main. Therefore, the minimum you
                                                                                                     3
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.
 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.
 As its name indicates, the Command Line is a blank line with a blinking caret that expects you to
 type a command:
                                                                                                        4
 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
                                                                                                       5
 "compiler" to refer to the program you use to "translate" your English-based instructions into a
 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.
  6. Click OK
  7. To create a source file for the project, on the main menu, click Project -> Add New Item...
                                                                                  6
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
   class OrderProcessing
   {
           static void Main()
           {
           }
   }
12. To save the file, on the main menu, click File -> Save
                                                                                                                   7
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:
                                                                                                                 8
namespace Jason
{
        class Airport
        {
        }
}
      namespace GeorgetownCleaners
      {
              class OrderProcessing
              {
                      static void Main()
                      {
                      }
              }
      }
   2. Save the file
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
 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
                                                                                                              9
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.Payroll
                Personnel.Benefits.MedicalInsurance
                Personnel.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:
Accounting.Payroll;
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.
      namespace GeorgetownCleaners
      {
              class OrderProcessing
              {
                      static void Main()
                      {
                              System.Console.WriteLine("Georgetown Cleaning
      Services");
                      }
              }
      }
With the using keyword, you can include as many external namespaces as necessary.
using System;
      namespace GeorgetownCleaners
      {
              class OrderProcessing
              {
                      static void Main()
                      {
                              Console.WriteLine("Georgetown Cleaning
      Services");
                      }
              }
      }
                                                                                                                12
  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
 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:
                                                                                                                13
 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.
using System;
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.
  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.
                                                                                                     16
                                              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:
 Once you avoid these words, there are rules you must follow when naming your objects. On this
                                                                                                               17
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.
 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.
 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.
 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.
 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
Another way you can represent this is by using the following table:
 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).
 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.
There is also the octal system but we will not use it anywhere in our applications.
 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.
Data Types
 In order to use a variable in your program, the compiler must be aware of it. Once the compiler
                                                                                                        20
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 =
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 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.
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
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
                                                                                                    23
 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:
 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:
 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.
A Byte
Introduction
                                                                                                               24
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.
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:
                                                                                                                  25
    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()
        {
                char Gender = 'M';
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;
    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.
using System;
         namespace GeorgetownCleaners
         {
                 class OrderProcessing
                 {
                         static void Main()
                         {
                                 byte NumberOfShirts;
                                 byte NumberOfPants;
                                    NumberOfShirts = 4;
                                    NumberOfPants = 1;
         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:
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;
A Word
Introduction
                                                                                                         28
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 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
= 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:
= 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;
    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.
using System;
class NumericRepresentation
{
        static void Main()
        {
                // These variables must hold only positive integers
                ushort NumberOfTracks;
                                                                                  30
ushort MusicCategory;
                      NumberOfTracks = 16;
                      MusicCategory = 2;
using System;
         class GeorgetownCleaningServices
         {
                 static void Main()
                 {
                         ushort NumberOfShirts;
                         ushort NumberOfPants;
                         ushort NumberOfDresses;
                            NumberOfShirts = 4;
                            NumberOfPants   = 0;
                            NumberOfDresses = 3;
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.
 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:
                                                                                                    32
    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
= 4,286,578,708
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
                                                                                                      33
{
          static void Main()
          {
                  int CoordX;
                  int CoordY;
                   CoordX = 12;
                   CoordY = -8;
using System;
class NumericRepresentation
{
        static void Main()
        {
                uint DayOfBirth;
                uint MonthOfBirth;
                uint YearOfBirth;
                   DayOfBirth   = 8;
                   MonthOfBirth = 11;
                   YearOfBirth = 1996;
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;
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.
In C++, the long data type is 32 bits while in C#, the long data type is 64 bits.
using System;
class NumericRepresentation
{
        static void Main()
        {
                long CountryArea;
CountryArea = 5638648;
    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
                                                                                                          36
    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;
Therefore, keep in mind that an int, a uint, a short, or a ushort can fit in a long variable.
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:
                                                                                                   37
 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
 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
                                                                                                     38
    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;
using System;
         namespace GeorgetownCleaners
         {
                 class OrderProcessing
                 {
                         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;
                                                                                                           39
Decimal
    The decimal data type can be used to declare a variable that would hold significantly large values that
    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;
Hourly Salary = 24
using System;
class NumericRepresentation
{
        static void Main()
        {
                float Distance;
Distance = 248.38F;
                    Console.Write("Distance = ");
                    Console.Write(Distance);
                    Console.WriteLine("km\n");
           }
}
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.
    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.
using System;
         namespace GeorgetownCleaners
         {
                 class OrderProcessing
                 {
                         static void Main()
                         {
                                 ushort NumberOfShirts;
                                 ushort NumberOfPants;
                                 ushort NumberOfDresses;
                                     decimal PriceOneShirt;
                                     decimal PriceAPairOfPants;
                                                                             41
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%
      ========================
      Order Date: 7/15/2002
      ------------------------
      Item Type Qty Sub-Total
      ------------------------
      Shirts      1     0.95
      Pants       1     2.95
      Dresses     1     4.55
      ------------------------
      Monday Discount: 0.25%
      ========================
   3. Return to Visual C#
 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;
        }
                                                                                                       43
    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!");
        }
}
    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:
    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()
                                                                                    44
         {
                  string Msg = "Welcome to the World of C# Programming! ";
                  Console.WriteLine(Msg);
         }
}
using System;
       namespace GeorgetownCleaners
       {
               class OrderProcessing
               {
                       static void Main()
                       {
                               string CustomerName;
                               string CustomerHomePhone;
                                   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%
3. Return to Visual C#
 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
 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
 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;
                                                                                                        48
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.
      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;
                                                                                                 49
      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
{
        static void Main()
        {
        }
}
 Parentheses can also be used to isolate an operation or an expression with regard to another
 operation or expression.
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;
        }
                                                                                                          50
    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.
using System;
         namespace GeorgetownCleaningServices
         {
                 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;
    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;
    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 operator
                        decimal Salary = 12.55M;
    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;
                                                                                                         52
                               System.Console.Write("Value 1 = ");
                               System.Console.WriteLine(Value1);
                               System.Console.Write("Value 2 = ");
                               System.Console.WriteLine(Value2);
                               System.Console.WriteLine();
                      }
             }
}
Value 1 = 224.58
Value 2 = 1548.26
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()
        {
                                                                                                     53
                    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:
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#!!!");
        }
}
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,
                                                                                            54
                                        PriceOneDress     = 4.55M;
                            uint OrderMonth = 3, OrderDay = 15, OrderYear = 2002;
                            double MondayDiscount = 0.25; // 25%;
2. To execute the program, on the main menu, click Debug -> Start Without Debugging. This
   would produce:
                                                                                                      55
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 +
    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
                                                                                                    56
    -∞          -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 number
                Console.Write("Number = ");
                Console.WriteLine(+802);
        }
}
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
                Console.Write("First Number ");
                Console.WriteLine(+802);
                                                                                                    57
    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 type
                Console.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");
                Console.Write("byte            ");
                Console.Write(sizeof(byte));
                Console.WriteLine(" Bytes");
                                                                                                58
                   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();
           }
}
Algebraic Operators
    In algebra, operations are performed on numeric values. Algebraic operators are represented with the
    following symbols:
The Addition
                                                                                                   59
 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
namespace Operations
{
        class Exercise
        {
                static void Main()
                {
                        System.Console.Write("244 + 835 = ");
                        System.Console.WriteLine(244 + 835);
                }
        }
}
 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);
                }
        }
}
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;
                                 Console.WriteLine("========================");
                                 Console.WriteLine();
                        }
               }
      }
  2. Execute the program. This would produce:
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
                        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();
                   }
                                                                                            62
          }
}
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;
                               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:
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
                                                                                                         64
{
           class Exercise
           {
                   static void Main()
                   {
                           // Values used in this program
                           double Value1 = 224.58, Value2 = 1548.26;
                           double Result = Value1 - Value2;
                             Console.Write(Value1);
                             Console.Write(" - ");
                             Console.Write(Value2);
                             Console.Write(" = ");
                             Console.WriteLine(Result);
                    }
           }
}
    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 associative
                        Console.WriteLine(" =+= Addition =+=");
                        Console.Write("128 + 42 +   5 = ");
                        Console.WriteLine(128 + 42 + 5);
                        Console.Write(" 5 + 42 + 128 = ");
                        Console.WriteLine(5 + 42 + 128);
Console.WriteLine();
                             Console.WriteLine();
                    }
           }
}
128 + 42 +   5 = 175
  5 + 42 + 128 = 175
 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.
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%
                            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:
         Number of Items: 10
         Total Order:     24.30
         Discount Rate:   12.500%
         Discount Amount: 3.03750
         Net 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();
                    }
           }
}
    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.
using System;
         namespace GeorgetownCleaningServices
         {
                                                                                  68
       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%
            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
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:
class Exercise
{
    static void Main()
    {
        int Players = 26;
           Console.Write(26 % 11);
           Console.WriteLine(" players will have to wait when the game starts.\n");
      }
}
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:
class Exercise
{
        static void Main()
        {
                int Value = 12;
Value = Value + 1;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
           }
}
    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:
class Exercise
                                                                                                      72
{
           static void Main()
           {
                   int Value = 12;
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:
class Exercise
{
        static void Main()
        {
                int Value = 12;
                    Value++;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
                    Value++;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
                    Value++;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
           }
}
    When using the ++ operator, the position of the operator with regard to the variable it is
                                                                                                         73
    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:
class Exercise
{
        static void Main()
        {
                int Value = 12;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
                    Console.Write("Value = ");
                    Console.WriteLine(++Value);
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
           }
}
    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:
class Exercise
{
        static void Main()
        {
                int Value = 12;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
                    Console.Write("Value = ");
                    Console.WriteLine(Value++);
                    Console.Write("Value = ");
                                                                                                       74
                    Console.WriteLine(Value);
           }
}
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:
class Exercise
{
        static void Main()
        {
                int Value = 12;
Value = Value - 1;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
           }
}
    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:
class Exercise
{
        static void Main()
        {
                                                                                                        75
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:
class Exercise
{
        static void Main()
        {
                int Value = 12;
                    Console.Write("Value = ");
                    Console.WriteLine(--Value);
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
           }
}
    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:
class Exercise
{
        static void Main()
        {
                                                                                                         76
                    Console.Write("Value = ");
                    Console.WriteLine(Value--);
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
           }
}
class Exercise
{
        static void Main()
        {
                double Value = 12.75;
                double NewValue;
                    Console.Write("Value = ");
                    Console.WriteLine(NewValue);
           }
}
    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.
                                                                                                      77
    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:
class Exercise
{
        static void Main()
        {
                double Value = 12.75;
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:
class Exercise
{
        static void Main()
        {
                double Value = 12.75;
Value -= 2.42;
                    Console.Write("Value = ");
                    Console.WriteLine(Value);
           }
                                                                                                        78
    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);
    As you can see, using the parentheses controls how the whole operation would proceed. This
                                                                                                   79
 difference can be even more accentuated if your operation includes 3 or more operators and 4
 or more operands.
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.
using System;
       namespace GeorgetownCleaningServices
       {
               class OrderProcessing
               {
                       static void Main()
                       {
                               Console.WriteLine();
                       }
               }
       }
   12. To execute the application, on the main menu, click Debug -> Start Without Debugging
                                                                                                     81
string FirstName;
Console.Write("Enter First Name: ");
FirstName = Console.ReadLine();
using System;
      namespace GeorgetownCleaningServices
      {
              class OrderProcessing
              {
                      static void Main()
                      {
                              string CustomerName, HomePhone;
                                 Console.WriteLine();
                                 // Display the receipt
                                 Console.WriteLine("====================================");
                                 Console.WriteLine("-/- Georgetown Cleaning Services -/-");
                                 Console.WriteLine("====================================");
                                 Console.Write("Customer:   ");
                                 Console.WriteLine(CustomerName);
                                 Console.Write("Home Phone: ");
                                 Console.WriteLine(HomePhone);
                                 Console.WriteLine("====================================\n");
                        }
               }
      }
   2. To execute the program, on the main menu, click Debug -> Start Without Debugging. This
      would produce:
         ====================================
         -/- Georgetown Cleaning Services -/-
         ====================================
         Customer:   James Watson
         Home Phone: (410) 493-2005
         ====================================
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()
                {
                        int Number;
                        string strNumber;
                            strNumber = Console.ReadLine();
                            Number = int.Parse(strNumber);
                   }
          }
}
using System;
namespace GeorgetownCleaningServices
{
        class OrderProcessing
        {
                static void Main()
                {
                        int Number;
                             Number = int.Parse(Console.ReadLine());
                    }
           }
}
using System;
         namespace GeorgetownCleaningServices
         {
                 class OrderProcessing
                 {
                         static void Main()
                         {
                                 // Price of items
                                 const decimal PriceOneShirt             =   0.95M;
                                 const decimal PriceAPairOfPants         =   2.95M;
                                 const decimal PriceOneDress             =   4.55M;
                                 const decimal TaxRate                   =   0.0575M;   // 5.75%
                            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 -/-
   ====================================
   Customer:   Genevieve Alton
   Home Phone: (202) 974-8244
   ------------------------------------
                                                                                                    86
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
    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.
using System;
         namespace GeorgetownCleaningServices
         {
                                                                                   87
       class OrderProcessing
       {
               static void Main()
               {
                       // Price of items
                       const decimal PriceOneShirt       =   0.95M;
                       const decimal PriceAPairOfPants   =   2.95M;
                       const decimal PriceOneDress       =   4.55M;
                       const decimal TaxRate             =   0.0575M;   // 5.75%
                              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 -/-
     ====================================
     Customer:      Alexander Pappas
     Home Phone: (301) 397-9764
     Date & Time: 6/22/1998 8:26:00 AM
     ------------------------------------
     Item Type Qty Unit/Price Sub-Total
     ------------------------------------
     Shirts       2      0.95     1.90
     Pants        6      2.95     17.70
     Dresses      0      4.55     0
     ------------------------------------
     Total Order:       19.60
     Tax Rate:          5.7500%
     Tax Amount:        1.127000
     Net Price:         20.727000
     ------------------------------------
     Amount Tended:     50
     Difference:        29.273000
     ====================================
  6. Return to Notepad
    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
using System;
// An Exercise class
class Exercise
{
    static void Main()
    {
        String FullName = "Anselme Bogos";
         int Age = 15;
         double HSalary = 22.74;
             Console.WriteLine();
      }
}
    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
                                                                                                   91
using System;
    namespace GeorgetownCleaningServices
    {
            class OrderProcessing
            {
                    static void Main()
                    {
                            // Price of items
                            const decimal PriceOneShirt                  =   0.95M;
                            const decimal PriceAPairOfPants              =   2.95M;
                            const decimal PriceOneDress                  =   4.55M;
                            const decimal TaxRate                        =   0.0575M;   // 5.75%
Conversion To String
 We mentioned earlier that everything the user types using the keyboard is primarily a string
                                                                                                       93
    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 class
class Exercise
{
        static void Main()
        {
                String FullName = "Anselme Bogos";
                int Age = 15;
                double HSalary = 22.74;
                    Console.WriteLine();
           }
}
using System;
         namespace GeorgetownCleaningServices
         {
                 class OrderProcessing
                 {
                         static void Main()
                         {
                                 // Price of items
                                 const 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());
                                                                                                     94
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
 ToString(), you can include a specific letter or combination inside of double-quotes. The
 letters and their meanings are:
using System;
                                                                                                       95
// An Exercise class
class 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: 2.483878E+002
Age: 15
Color: D96
Weekly 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.
using System;
          namespace GeorgetownCleaningServices
          {
                  class OrderProcessing
                  {
                          static void Main()
                          {
                                  // Price of items
                                  const decimal PriceOneShirt             =   0.95M;
                                  const decimal PriceAPairOfPants         =   2.95M;
                                  const decimal PriceOneDress             =   4.55M;
                                  const decimal TaxRate                   =   5.75M;   // 5.75%
                             Console.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:
   ====================================
   -/- Georgetown Cleaning Services -/-
   ====================================
   Customer:    Gretchen McCormack
   Home Phone: (410) 739-2884
   Date & Time: 4/9/2001 10:25:00 AM
   ------------------------------------
   Item Type Qty Unit/Price Sub-Total
                                                                                                           98
         ------------------------------------
         Shirts       5    $0.95     $4.75
         Pants        12    $2.95     $35.40
         Dresses      8    $4.55     $36.40
         ------------------------------------
         Total Order:     $76.55
         Tax Rate:        5.75 %
         Tax Amount:      $4.40
         Net Price:       80.95
         ------------------------------------
         Amount Tended:   $100.00
         Difference:      $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 class
class Exercise
{
        static void Main()
        {
                String FullName = "Anselme Bogos";
                int Age = 15;
                double HSalary = 22.74;
                    Console.WriteLine();
           }
}
    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
                                                                                                     99
using System;
      namespace GeorgetownCleaningServices
      {
              class OrderProcessing
              {
                      static void Main()
                      {
                              // Price of items
                              const decimal PriceOneShirt               =   0.95M;
                              const decimal PriceAPairOfPants           =   2.95M;
                              const decimal PriceOneDress               =   4.55M;
                              const decimal TaxRate                     =   0.0575M;   // 5.75%
   ====================================
   -/- Georgetown Cleaning Services -/-
   ====================================
   Customer:     Antoinette Calhoun
   Home Phone: (703) 797-1135
   Order Date: Friday, April 12, 2002
   Order Time: 2:12 PM
   ------------------------------------
   Item Type Qty Unit/Price Sub-Total
   ------------------------------------
   Shirts      5   $0.95       $4.75
   Pants       2   $2.95        $5.90
   Dresses     1   $4.55        $4.55
   ------------------------------------
   Total Order:    $15.20
   Tax Rate:       5.75 %
   Tax Amount:     $0.87
   Net Price:      $16.07
   ------------------------------------
   Amount Tended: $20.00
   Difference:     $3.93
   ====================================
3. Type Exit and close Notepad
                                                                                                     102
                         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
                                                                                                          103
 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
      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
                                                                                                      104
    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
{
        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);
           }
}
Value 1 = 15
Value 2 = 24
Comparison 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
                                                                                                          105
!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()
        {
                bool HasAirCondition = true;
                bool DoesIt;
                    Console.Write("HasAirCondition = ");
                    Console.WriteLine(HasAirCondition);
                    DoesIt = !HasAirCondition;
                    Console.Write("DoesIt          = ");
                    Console.WriteLine(DoesIt);
             }
}
HasAirCondition = True
DoesIt          = 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.
    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.
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();
           }
}
To find out whether one value is lower than another, use the < operator. Its syntax is:
    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.
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();
           }
}
Value 1 = 15
Value 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
                                                                                                         108
    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:
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();
           }
}
Value 1 = 15
Value 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:
 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.
 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.
                                                                                                    110
                              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()
        {
                                                                                                          111
string Answer;
                      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;
                      return 0;
           }
}
This time, the applicant knows that the expected answers are simply y for Yes or n for No.
         using System;
                                                                                                        112
      namespace MotorVehicleDivision
      {
              class Exercise
              {
                      static void Main()
                      {
                              string FirstName, LastName;
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: ");
                                                                                                        113
                      Console.WriteLine(TheStudentIsHungry);
             }
}
    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:
using System;
         namespace MotorVehicleDivision
         {
                 class Exercise
                 {
                         static void Main()
                         {
                                 string FirstName, LastName;
                                 bool WillingToBeAnOrganDonor;
                                    return 0;
                            }
                  }
         }
     2. Execute the application. Here is an example:
                                                                                                  114
 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:
                                                                                                       115
    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;
        // 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 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;
           }
}
    You can also write the statement on its own line if the statement is too long to fit on the same
                                                                                                   116
    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;
    If you omit the brackets, only the statement that immediately follows the condition would be
    executed.
using System;
         namespace MotorVehicleDivision
         {
                 class Exercise
                 {
                         static void Main()
                         {
                                 string FirstName, LastName;
                                 string OrganDonorAnswer;
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;
individual(y=Yes/n=No)? ");
               Ans = Console.ReadLine();
               Answer = char.Parse(Ans);
    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, 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;
using System;
class NewProject
                                                                                         119
{
         static void Main()
         {
                 char Answer;
                 string Ans;
using System;
       namespace MotorVehicleDivision
       {
               class Exercise
               {
                       static void Main()
                       {
                               string FirstName, LastName;
                               string OrganDonorAnswer;
                               char Gender;
 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;
                    Number1 = int.Parse(Num1);
                    Number2 = int.Parse(Num2);
                   Console.WriteLine();
          }
}
                             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;
                                                                                                     122
 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.
 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;
 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:
                                                       123
using System;
class NewProject
{
        static void Main()
        {
                int Number = 0;
                  Console.WriteLine();
          }
}
Number   0
Number   1
Number   2
Number   3
Number   4
Number   5
Number   6
Number   7
Number   8
Number   9
                                                                                                        124
Number 10
Number 11
Number 12
using System;
      enum TypeOfApplication
      {
          NewDriversLicense = 1,
          UpgradeFromIDCard,
          TransferFromAnotherState
      };
      class Exercise
      {
          static void Main()
          {
              TypeOfApplication AppType;
                      string FirstName, LastName;
              string OrganDonorAnswer;
              char Sex;
              string Gender;
              char DLClass;
       Console.Write("Sex(F=Female/M=Male): ");
       Sex = char.Parse(Console.ReadLine());
       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;
                                                                                              126
            default:
                Console.WriteLine("Not Specified");
                break;
            }
            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;
            }
            return 0;
       }
   }
12. To execute the application, on the main menu, click Debug -> Start Without Debugging. Here is
    an example:
 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.
                                                                                                  128
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:
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);
                   } while( !(SittingDown == 'y') );
                   Console.WriteLine();
            }
}
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;
            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("\nSex(F=Female/M=Male): ");
            Sex = char.Parse(Console.ReadLine());
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;
}
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");
                                                                                  131
                break;
            case 'm':
            case 'M':
                Console.WriteLine("Class:            M");
                break;
            default:
                Console.WriteLine("Class:            Unknown");
                break;
            }
            return 0;
       }
   }
2. Save, compile, and test it. Here is an example:
Sex(F=Female/M=Male): M
         M - Motorcycles
         Your Choice: C
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;
    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.
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:
                                                                                                     133
using System;
class NewProject
{
        static void Main()
        {
                    Console.WriteLine();
           }
}
Techniques of Writing Conditional Statements
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.
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);
           Console.WriteLine();
      }
}
    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
{
    static void Main()
    {
        char SittingDown;
        string SitDown;
           do {
                    Console.Write("Are you sitting down now(y/n)? ");
                    SitDown = Console.ReadLine();
                    SittingDown = char.Parse(SitDown);
char WantToContinue;
           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
                                                                                                     135
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);
char WantToContinue;
                    if(WantToContinue == '1')
                    {
                            char LayOnBack;
                              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");
using System;
      enum TypeOfApplication
      {
          NewDriversLicense = 1,
          UpgradeFromIDCard,
          TransferFromAnotherState
      };
      class Exercise
      {
          static void Main()
          {
              . . . No Change
               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());
. . . No Change
               return 0;
           }
      }
   2. Save, compile, and test the program
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
                                                                                                     137
    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;
          }
}
d
e
f
g
h
i
j
The break statement can also be used in a do…while or a for loop the same way.
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:
                                                                                                     138
using System;
class Exercise
{
        static void Main()
        {
                // Declare necessary variables
                double Number, Sum = 0;
                string Nbr;
    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
{
                                                                                                     139
                             if( Count == 5 )
                                     goto MamaMia;
                    }
                    MamaMia:
                            Console.WriteLine("\nStopped at 5");
                    Console.WriteLine();
             }
}
Count    0
Count    1
Count    2
Count    3
Count    4
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.
using System;
class SportCamp
{
        static void Main()
        {
                string FullName;
                int Gender;
                int Age;
                    Console.WriteLine();
           }
}
    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.
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
using System;
class SportCamp
                                                                                                       141
{
           static void Main()
           {
                   string FullName;
                   int Gender;
                   int Age;
                    if( Gender == 1 )
                    {
                            // If the applicant is a man, then we can continue
                            Console.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();
           }
}
    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;
                    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 continue
                            Console.Write("Age: ");
                            Age = int.Parse(Console.ReadLine());
                    Console.WriteLine();
           }
}
                                 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;
                                                                                                  143
                    int Gender;
                    int Age;
                    if( Gender == 1 )
                    {
                            // If the applicant is a man, then we can continue
                            Console.Write("Age: ");
                            Age = int.Parse(Console.ReadLine());
                    Console.WriteLine();
           }
}
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:
    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.Write("Age: ");
                    Age = int.Parse(Console.ReadLine());
                    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
{
        static void Main()
        {
                string FullName;
                int Gender;
                int Age;
                    Console.Write("Age: ");
                    Age = int.Parse(Console.ReadLine());
                    Console.WriteLine();
           }
}
false.
                                 A               B           A && B
                               False           False          False
                                 A              B            A && B
                               False           True           False
                                A                B           A && B
                               True            False          False
                               A              B           A && B
                              True           True          True
                                  A             B           A && B
                                False         False          False
                                False         True           False
                                True          False          False
                                True          True           True
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;
               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
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());
               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') );
               do
               {
                        Console.Write("Are you willing to be an Organ Donor(y=Yes/n=No)? ");
                        OrganDonorAnswer = Console.ReadLine();
                        if( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") &&
                                                                148
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;
}
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;
}
                                                                                 149
                    return 0;
           }
   }
2. Save, compile, and test the program. Here is an example:
   Invalid Answer
   Are you willing to be an Organ Donor(1=Yes/0=No)? 1
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:
using System;
class JobApplication
{
        static void Main()
        {
                string FullName;
                int MCAD;
                int Degree;
                    Console.WriteLine();
           }
}
When a new applicant applies for a job, his or her information is evaluated:
    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
using System;
class JobApplication
{
        static void Main()
        {
                string FullName;
                int MCAD;
                int Degree;
                    if( MCAD == 1 )
                            Console.WriteLine("Since you are an MCAD, you qualify");
                    Console.WriteLine();
           }
}
    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
using System;
class JobApplication
{
        static void Main()
        {
                string FullName;
                int MCAD;
                int Degree;
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();
           }
}
    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
    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
                                                                                                     153
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();
                   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).
Since you are an MCAD OR you have the necessary degree, you qualify
                                                                                        154
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 = null;
                      char DLClass;
                        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();
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') );
Console.WriteLine();
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;
                                                                                               156
                              default:
                                      Console.WriteLine("Not Specified");
                                      break;
                     }
                     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.WriteLine();
                     return 0;
            }
   }
2. Save the file. Compile and test it. Here is an example:
  state
  Your Choice: 0
  Invalid Answer
  Are you willing to be an Organ Donor(y=Yes/n=No)? p
  Invalid Answer
  Are 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 License
  Full Name:   Jocelyne Aladiere
  Sex:         Female
  Class:       M
  Organ Donor? Yes
  =======================================
3. Close
                                                                                                      158
          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;
                                                                                                        159
class Exercise
{
        Welcome() { }
    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
    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()
        {
        }
In the exercises of this lesson, we will perform calculations for a cylinder with circular base:
                                            Base Area = R * R * Pi
                                            Lateral Area = 2 * Pi *
                                            R * H
                                                                               PiR2H
                                            Total Area = 2PiRH +
                                            2PiR2
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()
        {
        }
           }
}
    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()
        {
                Console.WriteLine("Welcome to the Wonderful World of C#");
        }
    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#");
        }
                    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");
                 }
                                                                                                      162
                        Console.WriteLine();
                }
      }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging
  3. Return to Visual 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.
using System;
      namespace GeometricFormulas
      {
              class Cylinder
              {
                      static void ProcessCylinder()
                      {
                              double Radius, Height;
                              double BaseArea, LateralArea, TotalArea;
                              double Volume;
                                 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);
                                                                                                      163
                                    Console.WriteLine();
                             }
                   }
         }
     2. Execute the application. Here is an example:
         Cylinder      Characteristics
         Radius:       38.64
         Height:       22.48
         Base:         4690.55
         Lateral:      5457.75
         Total:        14838.85
         Volume:       105443.65
     3. Return to Visual C#
using System;
class Exercise
{
        static double Operation()
        {
        }
    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
{
                                                                                                  164
    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;
        }
    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;
        }
In the same way, a method returns a value can be assigned to a variable of the same type.
using System;
         namespace GeometricFormulas
                                                                            165
{
          class Cylinder
          {
                  static double GetRadius()
                  {
                          double rad;
                         Console.Write("Radius: ");
                         rad = double.Parse(Console.ReadLine());
                         return rad;
                 }
                         Console.Write("Height: ");
                         h = double.Parse(Console.ReadLine());
                         return h;
                 }
                         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);
                 }
                                     Console.WriteLine();
                             }
                   }
         }
     2. Save, compile, and test the file. Here is an example of running the program:
         Cylinder      Characteristics
         Radius:       52.08
         Height:       36.44
         Base:         8521.02
         Lateral:      11924.20
         Total:        28966.25
         Volume:       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;
                       Console.WriteLine();
             }
}
After declaring such a variable, you can access from any method that belongs to the same class.
using System;
namespace GeometricFormulas
{
        class Cylinder
        {
                static double   Radius;
                static double   Height;
                static double   BaseArea;
                static double   LateralArea;
                static double   TotalArea;
                static double   Volume;
                       Console.Write("Radius: ");
                       rad = double.Parse(Console.ReadLine());
                       return rad;
                }
                       Console.Write("Height: ");
                       h = double.Parse(Console.ReadLine());
                       return h;
                }
                                 Console.WriteLine("Lateral: {0:F}",
      LateralArea);
                                 Console.WriteLine("Total:        {0:F}", TotalArea);
                                 Console.WriteLine("Volume:       {0:F}", Volume);
                          }
                                 Console.WriteLine();
                          }
                }
      }
  2. Save, compile, and test the file. Here is an example of running the program:
      Cylinder      Characteristics
      Radius:       38.24
      Height:       32.58
      Base:         4593.94
      Lateral:      7827.96
      Total:        17015.85
      Volume:       149670.68
  3. Return to Notepad
 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;
                   do {
                            Console.Write("Are you sitting down now(y/n)? ");
                            Pos = Console.ReadLine();;
                            Position = char.Parse(Pos);
                   return Position;
           }
Position = GetPosition();
    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.
using System;
        namespace GeometricFormulas
        {
                class Cylinder
                {
                                                                                   170
                     do
                     {
                               Console.Write("Radius: ");
                               rad = double.Parse(Console.ReadLine());
                               if( rad < 0 )
                                       Console.WriteLine("Please enter a positive
number");
                     } while( rad < 0 );
                     return rad;
            }
                     do
                     {
                               Console.Write("Height: ");
                               h = double.Parse(Console.ReadLine());
                               if( h < 0 )
                                       Console.WriteLine("Please enter a positive
number");
                     } while( h < 0 );
                     return h;
            }
                                 Console.WriteLine();
                          }
                }
      }
   2. Save, compile, and test the file. Here is an example of running the program:
      Cylinder      Characteristics
      Radius:       24.55
      Height:       18.75
      Base:         1893.45
      Lateral:      2892.23
      Total:        6679.12
      Volume:       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.
using System;
                    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;
string Answer;
                    Answer = GetPosition();
                    Console.Write("\nAnswer = ");
                    Console.WriteLine(Answer);
           }
}
    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.
using System;
   namespace GeometricFormulas
   {
           class Cylinder
           {
                   static double        Radius;
                   static double        Height;
                   static double        BaseArea;
                   static double        LateralArea;
                   static double        TotalArea;
                   static double        Volume;
                                do
                                {
                                         Console.Write("Radius: ");
                                         rad = double.Parse(Console.ReadLine());
                      if( rad < 0 )
                              Console.WriteLine("Please enter a positive number");
                              } while( rad < 0 );
                                return rad;
                      }
                                do
                                {
                                         Console.Write("Height: ");
                                         h = double.Parse(Console.ReadLine());
                      if( h < 0 )
                              Console.WriteLine("Please enter a positive number");
                                                                                                175
} while( h < 0 );
                                return h;
                       }
                                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
 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
                                                                                                      176
have to supply the length, you can define such a method as follows:
using System;
    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;
    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;
                    Console.WriteLine();
           }
}
    As mentioned already, a method that takes an argument can also declared its own variable. Such
                                                                                       177
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.
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;
              }
                        do
                        {
                                  Console.Write("Height: ");
                                  h = double.Parse(Console.ReadLine());
                                  if( h < 0 )
                                          Console.WriteLine("Please enter a positive
     number");
                        } while( h < 0 );
                        return h;
              }
                     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);
           }
                     Console.WriteLine();
           }
   }
   Cylinder    Characteristics
   Radius:     35.96
   Height:     30.28
   Base:       4062.46
                                                                                               179
     Lateral: 6841.56
     Total:   14966.49
     Volume: 123011.33
  3. Return to Visual C#
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;
              }
                       do
                       {
                               Console.Write("Height: ");
                               h = double.Parse(Console.ReadLine());
                               if( h < 0 )
                                       Console.WriteLine("Please enter a positive
      number");
                       } while( h < 0 );
                       return h;
              }
                                                                            180
               BaseArea      =   CalculateBaseArea(Radius);
               LateralArea   =   CalculateLateralArea(Radius, Height);
               TotalArea     =   CalculateTotalArea(Radius, Height);
               Volume        =   CalculateVolume(Radius, Height);
                          Console.WriteLine();
                }
     }
  2. Execute the application and test it. Here is an example:
     Cylinder       Characteristics
     Radius:        38.26
     Height:        28.48
     Base:          4598.75
     Lateral:       6846.44
     Total:         16043.94
     Volume:        130972.40
  3. Return to Visual C#
using System;
                    Rate = 15.58;
                    Hours = 26.00;
Earnings(Hours, Rate);
                    Console.Write("\n");
           }
}
                                 Introduction to Classes
A Re-Introduction to Classes
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 Classes
using System;
         class DepartmentStore
         {
         }
         class Exercise
         {
             static void Main()
             {
             }
         }
using System;
class Book
{
}
class NewProject
{
    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
                                                                                                              184
    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
{
You can also use the new operator directly when declaring the variable as follows:
    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.
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();
             }
         }
Sharing Classes
                                                                                                        185
    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;
                    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"
    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.
         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:
                                                                                                         186
 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:
 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.
using System;
    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:
    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;
        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
                                                                                                 189
  1. 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;
                        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);
              }
      }
using System;
                    do
                    {
                             Console.Write("Enter the value in meters:           ");
                             Meter = double.Parse(Console.ReadLine());
     1. To start a new project, on the main menu, click File -> New -> Project
     2. In the Templates section, click Empty Project
                                                                                                     191
using System;
        class IceCream
        {
                public const decimal BasePrice    = 1.05M;
                public const string DefaultFlavor = "Diet Mint Fudge High
        Carb";
        class Exercise
        {
                static void Main()
                {
                }
        }
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 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:
 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
{
                                                                                                            192
           class Circle
           {
                   public double Radius;
           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;
           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);
                                                                                                             193
                              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;
           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;
                     }
           }
}
    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
        {
                                                                                                           194
           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;
                    }
           }
}
Circle Characteristics
Radius:        24.72
Diameter:      49.44
Circumference: 155.3202096
Area:          1919.757790656
    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
                                                                                                     195
necessary:
using System;
 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;
              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);
                                                                                                          196
                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);
           }
}
Book Characteristics
Title: Psychology and Human Evolution
Author: Jeannot Lamm
Year:   1996
Pages: 872
Cover: H
Book Characteristics
Title: C# First Step
Author: Alexandra Nyango
Year:   2004
Pages: 604
Cover: 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
    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;
                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);
                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
    accessed from the Program class without using an instance of the Book class:
using System;
               Console.WriteLine("Book Characteristics");
               Console.WriteLine("Title: {0}", Book.Title);
               Console.WriteLine("Author: {0}\n", Book.Author);
               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;
                    Length = 22.55;
                    Width = 20.25;
                    Console.WriteLine("\nRectangle 1");
                    Console.Write("Length: ");
                    Console.WriteLine(Length);
                    Console.Write("Width: ");
                    Console.WriteLine(Width);
                    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 Fields
using System;
class IceCream
{
        public const decimal BasePrice    = 1.05M;
        public const string DefaultFlavor = "Diet Mint Fudge High Carb";
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());
                          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;
                             case '2':
                                     Console.WriteLine("Ingredient:            M & M");
                                     break;
                             case '3':
                                     Console.WriteLine("Ingredient:            Cookies");
                                     break;
                             default:
                                     Console.WriteLine("Ingredient:            None");
                                     break;
                     }
2. Save the file, compile and test it at the Command Prompt. Here is an example:
                                                                                                   201
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;
Book Characteristics
Title: Psychology and Human Evolution
Author: Jeannot Lamm
Pages: 472
Price: $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.
                                                                                      202
The documentation can guide you to know what method of what class is static or not.
using System;
    class IceCream
    {
            public const decimal BasePrice    = 1.05M;
            public const string DefaultFlavor = "Diet Mint Fudge High Carb";
               {
                       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;
                       case '2':
                               Console.WriteLine("Ingredient:   M & M");
                               break;
                       case '3':
                               Console.WriteLine("Ingredient:   Cookies");
                               break;
                       default:
                               Console.WriteLine("Ingredient:   None");
                               break;
                                                                                                     204
         class Exercise
         {
                 static void Main()
                 {
                         IceCream.ProcessAnOrder();
                         IceCream.DisplayReceipt();
                             Console.WriteLine();
                   }
         }
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()
             {
                     double Area;
    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
                                                                                                       205
{
           double Base;
           double Height;
                    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();
        }
}
Triangle Characteristics
Base:   24.55
Height: 20.75
Area:   254.70625
     •   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();
                 }
         }
                                                                                                     206
•   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;
                       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();
            }
    }
1. 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;
      Department Store
      Item #:      348649
      Description: Men 3-Piece Jacket
      Unit Price: $275.95
6. Return to Notepad
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
                                                                                                    208
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.
using System;
                }
      }
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
                                                                                                       209
{
}
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.
using System;
                         item.ItemNumber = 348649;
                         item.ItemName   = "Men 3-Piece Jacket";
                         item.UnitPrice = 275.95;
                         item.DisplayAnItem();
                }
      }
 A constructor holds the same name as its class and doesn't return any value, not even void.
 Here is an example:
 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
 memory using the new operator. You can notice that we always included the parentheses when
 declaring such a variable. Here is an example:
 In this case, the parentheses indicate that we are calling the default constructor to instantiate
                                                                                                           211
the class.
        C++ Note
        In C++, if you declare a class variable without having
        to initialize it, you can omit the parentheses.
    Consider the following program:
using System;
      public Exercise()
      {
          Console.WriteLine("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.
1. Start a new file in Notepad and, in the empty file, type the following:
using System;
               public Applicant()
               {
                       this.FullName = "Unknown";
                       this.Sex          = "Ungenerated";
               }
      }
                         return 0;
               }
      }
6. Return to Notepad
    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:
           }
}
    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;
           }
}
    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;
           }
}
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.
using System;
              public Applicant(string n)
              {
                      this.FullName = n;
              }
     }
                        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());
                                 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') );
                                                                                                       215
                         return person;
               }
                         return 0;
               }
      }
3. Return to Notepad
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.
                                                                                                        216
    To make a constructor static, when creating it, type the static keyword to its left. Here is an
    example:
using System;
             }
}
    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.
using System;
                 do
                 {
                           Console.Write("Sex(F=Female/M=Male): ");
                           sex = char.Parse(Console.ReadLine());
                           if( (sex != 'f') && (sex != 'F') && (sex != 'm') &&
(sex != 'M') )
                                                                                    218
                       return person;
             }
                       App = RegisterPersonalInformation();
                       Show(App);
                       return 0;
             }
   }
         Sex:           Male
         Date of Birth: 06/10/1972
3. Return to Notepad
using System;
class SampleClass
{
        // Constructor
        public SampleClass()
        {
                Console.WriteLine("SampleClass - Constructor");
        }
           ~SampleClass()
           {
                   Console.WriteLine("Destructor of SampleClass");
           }
}
                    Console.WriteLine();
           }
}
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
    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.
                                                                                                       220
 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 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.
using System;
namespace Geometry
{
    public class Square
    {
        private double _side;
    }
           }
      }
}
    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;
using System;
          namespace LamackEnterprises
          {
              public class DepartmentStore
                                                                                           222
        {
            private   string   itemNo;
            private   string   cat;
            private   string   name;
            private   string   size;
            private   double   price;
        }
            }
        }
   }
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;
                    }
                }
          }
     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;
    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;
    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;
           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;
namespace Geometry
{
    public class Square
    {
        private double _side;
                }
           }
           public Square(double s)
           {
               _side = s;
           }
      }
      class Applied
      {
          static void Main()
          {
              Square sq = new Square(-25.55);
           }
      }
}
    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;
           public Square(double s)
           {
                                                                                           226
                _side = s;
           }
      }
      class Applied
      {
          static void Main()
          {
              Square sq1 = new Square(-12.48);
              Square sq2 = new Square(25.55);
Practical L
                                  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:
                                                                                                         227
    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:
    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 Outside()
           {
                   Console.WriteLine(" =- Outside -=");
           }
}
class Exercise
{
        static void Main()
                                                                                                      228
          {
                   Outside Recto = new Outside();
                   Outside.Inside Ins = new Outside.Inside();
          }
}
=- 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 Inside()
                   {
                           Console.WriteLine(" -= Insider =-");
                           InMessage = "Sitting inside while it's raining";
                   }
          public Outside()
          {
                  Console.WriteLine(" =- The Parent -=");
          }
class Exercise
{
        static void Main()
        {
                Outside Recto = new Outside();
                Outside.Inside Ins = new Outside.Inside();
                   Recto.Display();
                                                                                                       229
          }
}
    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 Inside()
                   {
                           Console.WriteLine(" -= Insider =-");
                           InMessage = "Sitting inside while it's raining";
                   }
          public Outside()
          {
                  Console.WriteLine(" =- The Parent -=");
                  OutMessage = "Standing outside! It's cold and raining!!";
          }
class Exercise
{
        static void Main()
        {
                Outside Recto = new Outside();
                Outside.Inside Ins = new Outside.Inside();
                   Recto.Display();
                   Console.WriteLine();
                                                                                                         230
                    Ins.FieldFromOutside();
           }
}
=- The Parent -=
 -= Insider =-
Sitting inside while it's raining
Show me the wonderful world of C# Programming
    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;
           {
                    Console.WriteLine(insider.InMessage);
           }
class Exercise
{
        static void Main()
        {
                Outside Recto = new Outside();
                Outside.Inside Ins = new Outside.Inside();
                    Ins.Show();
                    Recto.Display();
           }
}
-= 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:
    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;
{
      MVADate MDate;
    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.
using System;
                  return result;
             }
         }
MVA.birthdate.SetDate(d, m, y);
            Console.WriteLine();
                    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 exercise
   Here is an example:
          C:\CSharp Lessons\MVA2>exercise
          To process a registration, enter the information
          Full Name:      Ernestine Mvouama
          Day of Birth:   8
          Month of Birth: 14
          Year of Birth: 1984
          Is the application an organ donor (0=No/1=Yes)? 1
using System;
           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.
                                                                     235
using System;
            return result;
        }
   }
            date.SetDate(d, m, y);
            return date;
        }
MVA.birthdate = RequestDateOfBirth();
                Console.WriteLine();
                 return 0;
            }
      }
   2. Save it and switch to the Command Prompt
   3. Compile and execute the application
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);
    }
}
    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);
    }
}
using System;
        namespace ConsoleApplication1
        {
                public class MVADate
                {
                        private int dayOfBirth;
                        private int monthOfBirth;
                        private int yearOfBirth;
                                   return result;
                          }
                 }
                 class Car
                 {
                         public string Make;
                         public string Model;
                         public int      CarYear;
                 }
                 class CarRegistration
                                                                               238
         {
                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);
               }
                         date.SetDate(d, m, y);
                         return date;
                }
                         Console.Write("Make: ");
                         c.Make = Console.ReadLine();
                         Console.Write("Model: ");
                         c.Model = Console.ReadLine();
                         Console.Write("Year: ");
                         c.CarYear = int.Parse(Console.ReadLine());
                                                                                       239
                             return c;
                     }
                             regist.DisplayDriversLicense(MVA);
                             regist.DisplayCarInformation(MVA.NewCar);
                             Console.WriteLine();
                     }
           }
   }
2. Save it and switch to the Command Prompt
3. Compile and execute the application. Here is an example:
                                         Inheritance
Introduction to Inheritance
Definition
 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.
using System;
            public Applicant()
            {
            }
            public Applicant(string n)
            {
                    this.name = n;
            }
                     do
                                                                                     242
                      {
                               Console.Write("Sex(F=Female/M=Male): ");
                               s = char.Parse(Console.ReadLine());
                      Console.WriteLine();
                      return 0;
             }
   }
Sex(F=Female/M=Male): F
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;
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;
           }
}
Circle Characteristics
Side:     25.55
Diameter: 51.1
Circumference: 160.535249
Area:     2050.837805975
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:
    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:
    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;
                                                                           245
class Circle
{
        private double _radius;
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);
                                                                                                          246
                    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;
           }
}
Circle Characteristics
Side:     25.55
Diameter: 51.1
Circumference: 160.535249
Area:     2050.837805975
Sphere Characteristics
Side:     25.55
Diameter: 51.1
Circumference: 160.535249
Area:     2050.837805975
Press 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 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;
               c.ShowCharacteristics();
                   s.Radius = 25.55;
                   s.ShowCharacteristics();
                   return 0;
          }
}
using System;
                 public Applicant()
                 {
                 }
                 public Applicant(string n)
                 {
                         this.name = n;
                 }
               do
               {
                       Console.Write("Sex(F=Female/M=Male): ");
                       s = char.Parse(Console.ReadLine());
this.Registration();
            switch((TypeOfIDCardApplication)ApplicationReason)
            {
                    case TypeOfIDCardApplication.NewCard:
                            break;
                   case TypeOfIDCardApplication.Replacement:
                           do
                           {
                                   Console.WriteLine("What is the reason you want a replacemen
                                   Console.WriteLine("1 - I lost my ID Card");
                                   Console.WriteLine("2 - My ID Card was stolen");
                                   Console.WriteLine("3 - My ID Card is damaged");
                                   Console.WriteLine("4 - The court/a judge ordered that I rep
                                   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 );
                   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
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 );
                   case TypeOfIDCardApplication.Renewal:
                           break;
                                                                               251
                       case TypeOfIDCardApplication.Transfer:
                               Console.WriteLine("What US state or Canadian province are you trans
from?");
                               TransferFrom = Console.ReadLine();
                               break;
               }
       }
               Console.WriteLine("Applicant's Information");
               this.Show();
               Console.WriteLine("Administration Notes or Comments");
               Console.WriteLine(NotesOrComments);
       }
}
               do
               {
                       Console.WriteLine("So what brought you to the Motor Vehicle Department toda
                       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 );
                                                                                         252
                        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;
               }
      }
   2. Save, compile, and test the file. Here is an example:
namespace People
{
        public class Person
        {
                private string _name;
                private string _gdr;
                   public Person()
                   {
                           this._name = "Not Available";
                           this._gdr = "Unknown";
                                                                                                     253
                    }
                    public Person(string name, string gender)
                    {
                            this._name = name;
                            this._gdr = gender;
                    }
    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:
namespace HighSchool
{
        public class Teacher : People.Person
        {
                private string _pos;
                    public Teacher()
                    {
                            this._pos = "Staff Member";
                    }
    If you need to call the class that was defined in a different namespace, remember to qualify its name
                                                                                               254
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:
class Exercise
{
        static void Main()
        {
                Person man = new Person("Hermine Sandt", "Male");
                Teacher staff = new Teacher("Vice Principal");
                   Console.WriteLine();
           }
}
using System;
         namespace FlatShapes
         {
                 class Square
                 {
                         private double _side;
                          public Square()
                          {
                                  _side = 0.00;
                          }
                          public Square(double s)
                                                                                                    255
                        {
                                 _side = s;
                        }
               }
               class Rectangle
               {
                       double _length;
                       double _height;
                        public Rectangle()
                        {
                                _length = 0.00;
                                _height = 0.00;
                        }
using System;
      class Exercise
      {
              static void Main()
              {
                      FlatShapes.Square Sq = new FlatShapes.Square(24.55);
              }
      }
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.
                                                                                               256
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;
                   }
    You can access protected members only in derived classes. Therefore, if you instantiate a class
    outside, you can call only public members:
    class Exercise
    {
            static void Main()
            {
                    People.Person man = new People.Person("Hermine Sandt",
    "Male");
                                                                                                      257
                   Console.WriteLine("Staff Member");
                   man.Show();
                   Console.WriteLine();
          }
 }
Staff Member
Full Name: Hermine Sandt
Gender:      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
 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:
    In the same way, when implementing an abstract method of a class, type the override keyword to its
    left.
         class ShapeDescription
         {
                 public virtual string Description()
                 {
                         string Msg = "A quadrilateral is a geometric figure that has " +
                                     "four sides and four angles.";
                         return Msg;
                 }
         }
using System;
         namespace FlatShapes
         {
                 class Square : ShapeDescription
                 {
                         private double _side;
                          public Square()
                          {
                                  _side = 0.00;
                          }
                          public Square(double s)
                          {
                                  _side = s;
                          }
                               return Introduction;
                     }
            }
                     public Rectangle()
                     {
                             _length = 0.00;
                             _height = 0.00;
                     }
using System;
   class Exercise
   {
           static void DisplaySquare(FlatShapes.Square S)
           {
                   Console.WriteLine("Square Characteristics");
                   Console.WriteLine("Description: {0}", S.Description());
           }
               {
                        Console.WriteLine("Rectangle Characteristics");
                        Console.WriteLine("Description: {0}", R.Description());
               }
               Console.WriteLine("========================================");
                       DisplaySquare(Sq);
               Console.WriteLine("========================================");
                       DisplayRectangle(Rect);
Console.WriteLine("========================================");
                        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 Characteristics
      Description: A quadrilateral is a geometric figure that has four
      sides and four
      angles.A square is a quadrilateral that has four equal sides and four
      right angles
      ========================================
      Rectangle Characteristics
      Description: A quadrilateral is a geometric figure that has four
      sides and four
      angles.
      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
                                                                                                        261
example:
    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:
    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.
using System;
   namespace FlatShapes
   {
           class Square : ShapeDescription
           {
                   private double _side;
                      public Square()
                      {
                              _side = 0.00;
                      }
                      public Square(double s)
                      {
                              _side = s;
                      }
                               return Introduction;
                      }
             }
                      public Rectangle()
                      {
                              _length = 0.00;
                                                                                                    263
                               _height = 0.00;
                      }
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());
           }
                        Console.WriteLine("========================================");
                        DisplaySquare(Sq);
                        Console.WriteLine("========================================");
                        DisplayRectangle(Rect);
                        Console.WriteLine("========================================");
                        Console.WriteLine();
               }
      }
   6. Save the Exercise.cs
   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 Characteristics
      Name:        Square
      Description: A quadrilateral is a geometric figure that has four
      sides and four
      angles.A square is a quadrilateral that has four equal sides and four
      right angl
      es
      ========================================
      Rectangle Characteristics
      Name:        Rectangle
      Description: A quadrilateral is a geometric figure that has four
      sides and four
      angles.
      A rectangle is a quadrilateral that has adjacent perpendicular sides.
      This impli
      es 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:
 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
{
}
  Practical Learning: Introducing Interfaces
   1. Access the Quadrilaterals.cs file. To create an interface, change the file as follows:
       interface IQuadrilateral
       {
       }
• You can indicate that it would be read-only by adding it an empty getter. Here is an example:
   •   You can indicate that it would be used to write values to it and to read values from it. Here is an
                                                                                                      266
example:
    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:
          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; }
         }
 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:
 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:
 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:
 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:
      interface IQuadrilateral
      {
              double Area { get; }
      }
using System;
      namespace FlatShapes
      {
              class Square : ShapeDescription, IRightAngles
              {
                      . . . No Change
              }
{
           int NumberOfPlayers
           {
                   get;
                   set;
           }
           string NameOfSport
           {
                   get;
           }
           void SportCharacteristics();
}
    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:
           {
                    get      { return sport; }
           }
Once the class is ready, you can then use it as you see fit. Here is an example:
class Exercise
{
        static void Main()
        {
                SportBall volley = new SportBall(6, SportCategory.Collective, "Volley Ball");
                volley.Length = 18;
                volley.Width = 9;
                volley.SportCharacteristics();
Console.WriteLine();
                    Console.WriteLine();
           }
}
Sport Characteristics
Name of Sport: Volley Ball
Type of Sport: Collective
# of Players: 6
Court Dimensions: 18m x 9m
Sport Characteristics
Name of Sport: Table Tennis
Type of Sport: SinglePlayer
# of Players: 1
Court 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;
                        }
                                 return Introduction;
                        }
       public Rectangle()
       {
               _length = 0.00;
               _height = 0.00;
       }
                     }
            }
   }
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;
                    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:
      ============================
  6. Return to Notepad
 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.
using System;
      class Sport
      {
          private double _ballWeight;
          private int    _players;
          private double _courtLength;
                                                                                              277
   class Exercise
   {
       static int Main()
       {
           Sport tennis = new Sport();
           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
4. Compile the program with csc exercise.cs
5. Execute it by typing exercise
   Here is an example:
                                                                                                 278
C:\CSharp Lessons\Inherited>exercise
      Game Characteristics
      Ball Weight:           57.5 grams
      Players on each side: 1
      Court Dimensions(LxW): 23.7m X 8.23m
      C:\CSharp Lessons\Inherited>
   6. Return to Notepad
 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 book
        int NumberOfPages1 = 422;
        // Second book
        int NumberOfPages2 = 858;
        // Third book
        int NumberOfPages3 = 422;
The first and the second books have different number of pages
The 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:
 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.
using System;
      class Sport
      {
          private   double   _ballWeight;
          private   int      _players;
          private   double   _courtLength;
          private   double   _courtWidth;
        return false;
    }
}
class Exercise
{
    static int Main()
    {
        Sport Euro2002 = new Sport();
        Sport CAN2004 = new Sport();
        Sport tennis   = new Sport();
        return 0;
                                                                                                           281
                }
          }
     2. Save the file and switch to the Command Prompt
     3. Compile and test the application. This would produce:
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;
    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.
using System;
          class Sport
          {
              private    double   _ballWeight;
              private    int      _players;
              private    double   _courtLength;
              private    double   _courtWidth;
   {
       get { return _ballWeight; }
       set { _ballWeight = value; }
   }
       return false;
   }
       if( NumberOfPlayers.Equals(1) )
            person = " person";
       else
            person = " persons";
       return result;
                                                                                                 283
          }
      }
      class Exercise
      {
          static int Main()
          {
              Sport CAN2004 = new Sport();
              Sport tennis  = new Sport();
Console.WriteLine();
                return 0;
          }
      }
  2. Save the file and switch to the Command Prompt
  3. Compile and test the application. This would produce:
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.
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.
    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:
class Exercise
{
    static void Main()
    {
        double Number;
        double Result;
           if( !Microsoft.VisualBasic.Information.IsNumeric(strNbr) )
                Number = 0.00;
           else
                Number = Microsoft.VisualBasic.Conversion.Val(strNbr);
Result = Number * 2;
    When compiling the program, you must reference the Microsoft.VisualBasic.dll library. Here is an
    example:
                                                                                                           285
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
 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:
using System;
      namespace Arithmetic
      {
          public class Operations
          {
              public static double Addition(double x, double y)
              {
                  return x + y;
              }
   using System;
   using Arithmetic;
using System;
      namespace LoanProcessing
      {
              class Exercise
              {
                      static double GetPrincipal()
                      {
                              double P;
                                                                                             288
                             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
                                                                                             289
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 Enter
   Here is an example:
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   Loan Processing
   ==================================
   Estimate on loan
   ----------------------------------
   Principal: $4,500.00
   Interest: 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:
using System;
   namespace LoanProcessing
   {
           class Exercise
           {
                   static double GetPrincipal()
                   {
                           double P;
                   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 up
                           Console.WriteLine("Bad Selection\n");
                   }
            }
                   Console.WriteLine("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
                   Console.WriteLine("Loan Processing\n");
                   Principal      = GetPrincipal();
                   IntRate        = GetInterestRate();
                               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;
                      }
            }
   }
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;
                         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:
");
                                 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 up
                                 Console.WriteLine("Bad Selection\n");
                                 return 0.00;
                         }
                  }
            }
   }
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("\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);
                               if( TypeOfPeriod == 0 )
                               {
                                       // Since the user made a bad selection, stop the program here
                                       return 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";
                               }
                                                                                        294
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:
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   Loan Processing
   ==================================
   Estimate on loan
   ----------------------------------
   Principal: $850.00
   Interest: 14.55 %
   Period:    240 days
   --------------------------------
   Interest paid on Loan: $82.45
   ==================================
19. Return to the Exercise.cs file
                                                                                                  295
Delegate Declaration
 To declare a delegate, you use the delegate keyword. The basic formula used to create a
 delegate is:
 The modifier can be one or an appropriate combination of the following keywords: new, public,
 private, protected, or internal.
 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.
 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;
class Exercise
{
        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;
class Exercise
{
        private static void Welcome()
        {
                Console.WriteLine("Welcome to the Wonderful World of C# Programming!");
        }
           }
}
    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;
class Exercise
{
        private static void Welcome()
        {
        Console.WriteLine("Welcome to the Wonderful World of C# Programming!");
        }
                   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;
class Exercise
{
        private static void Welcome()
        {
        Console.WriteLine("Welcome to the Wonderful World of C# Programming!");
        }
                   Announce();
                   return 0;
          }
}
    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.
                                                                                        297
Here is an example:
using System;
class Exercise
{
        private static void Welcome()
        {
        Console.WriteLine("Welcome to the Wonderful World of C# Programming!");
        }
                   return a + b;
          }
                   Announce();
                   Console.WriteLine("\n248.66 + 50.26 = {0}", Add());
                   return 0;
          }
}
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;
                                                               298
       int    TypeOfPeriod = 0;
       double Periods = 0;
       string PeriodName = null;
       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 here
               return 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("Total Amount Paid:     {0:C}", Amount);
       Console.WriteLine("Interest paid on Loan: {0:C}", AmountPaidAsInterest);
       Console.WriteLine("==================================\n");
       return 0;
                                                                                                  299
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      Loan Processing
      ==================================
      Estimate on loan
      ----------------------------------
      Principal: $850.00
      Interest: 14.55 %
      Period:    240 days
      --------------------------------
      Total Amount Paid:     $932.45
      Interest paid on Loan: $82.45
      ==================================
   3. Return to the Exercise.cs file
 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
{
                                                                               300
           public Cube()
           {
                   _side = 0;
           }
           public Cube(double s)
           {
                   _side = s;
           }
                   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;
           }
    }
}
                                                                                                   301
Cube Characteristics
Side:   25.58
Area:   3926.0184
Volume: 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.
    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:
using System;
class Exercise
{
        private static void Welcome()
        {
        Console.WriteLine("Welcome to the Wonderful World of C# Programming!");
        }
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:
                                                                                                    302
    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;
class Exercise
{
        private static void Welcome()
        {
        Console.WriteLine("Welcome to the Wonderful World of C# Programming!");
        }
                   Announce();
                   Console.WriteLine("\n{0} + {1} = {2}", Value1, Value2, Result);
                   return 0;
          }
}
using System;
namespace GeometricFormulas
{
        public delegate double Squared(double x);
using System;
namespace GeometricFormulas
{
        public delegate double Squared(double x);
    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);
           }
}
    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);
    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 Circle()
       {
               _radius = 0;
       }
       public Circle(double r)
       {
               _radius = r;
       }
                 Circf = FDiam(_radius);
                 return Circf * Math.PI;
       }
               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
{
                                                                                                   306
                            Round.CircleCharacteristics();
                            return 0;
                   }
          }
}
Circle Characteristics
Radius:        25.58
Diameter:      51.16
Circumference: 160.723880157654
Area:          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:
class Exercise
{
        const double PI = 3.14159;
    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;
        }
                                                                                                    307
                   return 0;
          }
}
    You can initialize each member using its index and calling the corresponding method. This can
    be done as follows:
using System;
class Exercise
{
        const double PI = 3.14159;
                   Console.WriteLine("Circle Characteristics");
                                                                                          308
                   return 0;
          }
}
Circle Characteristics
Diameter:      25.1
Circumference: 78.8539
Area:          494.808
     Practical Learning: Using an Array of Delegates
     1. Start a new file in Notepad and type the following:
using System;
        namespace MultipleChoiceQuestion
        {
                class Exercise
                {
                        enum TMCQuestion { One, Two, Three, Four, Five };
                        delegate char Question();
                                   return Answer;
                          }
       return Response;
}
       return Answer;
}
       return Wrong;
}
       return Sentence;
}
                                                                     310
       return 0;
}
                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");
                        else
                Console.WriteLine("Wrong Answer - The right answer was 'b'");
                        break;
                case 4:
                            if(Ans == 'd' || Ans == 'D')
                                    Console.WriteLine("Right answer");
                            else
                                                                                                     311
                                           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
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
                                                                                                      312
         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;
class Exercise
{
        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:
 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;
class Exercise
{
        public static event dlgSimple Simply;
After declaring the event, you must define a method that calls the event. Here is an example:
using System;
class Exercise
{
        public static event dlgSimple Simply;
    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;
class Exercise
{
        public static event dlgSimple Simply;
SayHello();
                    return 0;
                                                                                             314
          }
}
    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;
class Exercise
{
        public event dlgSimple Simply
        {
                add
                {
                       Simply += new dlgSimple(Welcome);
                }
                remove
                {
                       Simply -= new dlgSimple(Welcome);
                }
        }
                                                                                  <a
     Previous             Copyright © 2004-2007 FunctionX, Inc.
                                                                              href="Lesso
                                Exception Handling
Introduction to Exceptions
An Overview
                                                                                                        315
    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;
    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.
using System;
         namespace GeorgetownCleaningServices
         {
                 struct OrderInformation
                 {
                         // Basic information about an order
                         public string   CustomerName;
                         public string   HomePhone;
                         public DateTime OrderDate;
                         public DateTime OrderTime;
                         // Unsigned numbers to represent cleaning items
                         public uint NumberOfShirts;
                         public uint NumberOfPants;
                         public uint NumberOfDresses;
                 }
         }
using System;
   namespace GeorgetownCleaningServices
   {
           class OrderProcessing
           {
                   // Price of items
                   const decimal PriceOneShirt            =   0.95M;
                   const decimal PriceAPairOfPants        =   2.95M;
                   const decimal PriceOneDress            =   4.55M;
                   const decimal TaxRate                  =   0.0575M;   // 5.75%
OrderInformation Order;
                             ShowReceipt();
                    }
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();
                     }
            }
   }
   ====================================
   -/- Georgetown Cleaning Services -/-
   ====================================
   Customer:     Perl
   Home Phone: 301-884-0912
   Order Date: Monday, August 04, 2003
   Order Time: 10:26 AM
   ------------------------------------
   Item Type Qty Unit/Price Sub-Total
   ------------------------------------
   Shirts       12    0.95     11.40
   Pants         2    2.95      5.90
   Dresses       8    4.55     36.40
   ------------------------------------
   Total Order:    $53.70
   Tax Rate:       5.75 %
                                                                                                 319
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.
  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;
                       try
                       {
                                Console.Write("Type a number: ");
                                Number = double.Parse(Console.ReadLine());
  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
                                                                                        320
section. Combined with the try block, the syntax of an exception would be:
   try
   {
            // Try the program
   flow
   }
   catch
   {
            // Catch the exception
   }
using System;
                     try
                     {
                              Console.Write("Type a number: ");
                              Number = double.Parse(Console.ReadLine());
                     }
            }
   }
using System;
   namespace GeorgetownCleaningServices
   {
           class OrderProcessing
           {
                   . . . No Change
               {
                       . . . No Change
                       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
                       ShowReceipt();
               }
         }
     2. Save the file
     3. At the Command Prompt, to compile the application, type
         csc /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
using System;
                   try
                   {
                            Console.Write("Type a number: ");
                            Number = double.Parse(Console.ReadLine());
    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.
using System;
         namespace GeorgetownCleaningServices
         {
                 class OrderProcessing
                 {
                         . . . No Change
                                                               323
       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");
       }
                            ShowReceipt();
                     }
   ====================================
   -/- Georgetown Cleaning Services -/-
   ====================================
   Customer:    Alexandria
   Home Phone: (102) 797-8382
   Order Date: Monday, April 02, 2001
   Order Time: 9:22 AM
   ------------------------------------
   Item Type Qty Unit/Price Sub-Total
                                                                                                      325
        ------------------------------------
        Shirts       6   0.95        5.70
        Pants        0   2.95           0
        Dresses      5   4.55       22.75
        ------------------------------------
        Total Order:   $28.45
        Tax Rate:      5.75 %
        Tax Amount:     $1.64
        Net Price:     $30.09
        ------------------------------------
        Amount Tended: $0.00
        Difference:    ($30.09)
        ====================================
      5. Return to Notepad
 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.
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
                                                                                                    326
    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;
                   try
                   {
                            Console.Write("Type a number: ");
                            Number = double.Parse(Console.ReadLine());
using System;
                   try
                   {
                            Console.Write("Type a number: ");
                            Number = double.Parse(Console.ReadLine());
                    }
                    catch(Exception ex)
                    {
                    Console.WriteLine("The 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;
                    try
                    {
                             Console.Write("Type a number: ");
                             Number = double.Parse(Console.ReadLine());
    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
                                                                                                       328
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
    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.
using System;
                    try
                    {
                             Console.Write("Type a number: ");
                             Number = double.Parse(Console.ReadLine());
using System;
         namespace GeorgetownCleaningServices
         {
                 class OrderProcessing
                 {
                         . . . No Change
                                                               329
       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
                                 ShowReceipt();
                         }
using System;
// An Exercise class
class Exercise
{
        static void Main()
        {
                byte 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:
     at System.Byte.Parse(String s)
     at Exercise.Main() in c:\programs\msvcs .net 2003\project17\exercise.cs:line
11
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 class
class Exercise
{
        static void Main()
        {
                DateTime 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:
    One way you can avoid this is to guide the user but still take appropriate actions, just in case
    this error is thrown.
                                                                                         332
using System;
     namespace GeorgetownCleaningServices
     {
             class OrderProcessing
             {
                     . . . No Change
                                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 wil
     ignore it");
                                }
       {
               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
       ShowReceipt();
}
                            . . . No Change
                    }
            }
   }
2. Save the file
3. At the Command Prompt, to compile the application, type
   csc /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 -/-
   ====================================
   Customer:    Gertrude Ngana
   Home Phone: 208-923-8263
   Order Date: Monday, January 01, 0001
   Order Time: 12:00 AM
   ------------------------------------
   Item Type Qty Unit/Price Sub-Total
   ------------------------------------
   Shirts       0   0.95           0
   Pants        8   2.95       23.60
   Dresses      2   4.55        9.10
   ------------------------------------
   Total Order:   $32.70
   Tax Rate:      5.75 %
   Tax Amount:     $1.88
   Net Price:     $34.58
   ------------------------------------
   Amount Tended: $0.00
   Difference:    ($34.58)
                                                                                                       335
      ====================================
   5. Return to Notepad
 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;
                  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());
                                                                                                    336
                          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);
               }
        }
}
try {
        // Code to Try
                                                                                                       337
}
catch(Arg1)
{
        // One Exception
}
catch(Arg2)
{
        // Another Exception
}
   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 executed
      If 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;
                  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());
                           switch(Operator)
                           {
                                   case '+':
                                                                                                       338
                                    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;
                  try
                  {
                                                                                                   339
                          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
 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+
                                                                                             340
 +' catch(...) block. Therefore, if you write various catch blocks, the one that takes the
 Exception as argument must be the last.
using System;
class Exercise
{
        static void Main()
        {
                double Operand1, Operand2;
                double Result = 0.00;
                char Operator;
                 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;
                                                                                                    341
                          }
                          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.
using System;
      namespace GeorgetownCleaningServices
      {
              class OrderProcessing
              {
                      . . . No Change
                                 // To make it easy for the user, we will request different values for
      the date
                                 try
                                 {
                                          Console.WriteLine("Enter the date this order was placed");
                                          Console.Write("Enter Today's Day:   ");
                                          int day = int.Parse(Console.ReadLine());
                                                               342
        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 value
        catch(FormatException)
        {
Console.WriteLine("The time you entered is not valid and it will be refused");
        }
       try
                                                                                  343
                          {
                                  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 number
                          catch(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 value
                          catch(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 number
                          catch(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 value
                          catch(OverflowException)
                          {
                                  Console.WriteLine("The number you typed is lower than the " +
                                                 "acceptable number. It will be ignored");
                          }
. . . No Change
                          ShowReceipt();
                   }
     ====================================
     -/- Georgetown Cleaning Services -/-
     ====================================
     Customer:    Andrew Palermo
     Home Phone: (240) 493-8935
     Order Date: Monday, August 12, 2002
     Order Time: 12:00 AM
     ------------------------------------
     Item Type Qty Unit/Price Sub-Total
     ------------------------------------
     Shirts       9   0.95        8.55
     Pants        0   2.95           0
     Dresses      0   4.55           0
     ------------------------------------
     Total Order:    $8.55
     Tax Rate:      5.75 %
     Tax Amount:     $0.49
     Net Price:      $9.04
     ------------------------------------
     Amount Tended: $0.00
     Difference:    ($9.04)
     ====================================
  5. Return to Notepad
Exceptions Nesting
                                                                                                    345
 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;
                 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;
                                           Console.WriteLine("\n{0} - {1} = {2}", Operand1,
Operand2, Result);
                                            break;
                                                                                              346
                                   case '*':
                                           Result = Operand1 * Operand2;
                                           Console.WriteLine("\n{0} * {1} = {2}", Operand1,
Operand2, Result);
                                            break;
                                   case '/':
                                           // The following exception is nested in the
previous try
                                            try
                                            {
                                              if(Operand2 == 0)
                                                      throw new
DivideByZeroException("Division by zero is not allowed");
using System;
      namespace GeorgetownCleaningServices
      {
              class OrderProcessing
              {
                      // Price of items
                      const decimal PriceOneShirt     = 0.95M;
                      const decimal PriceAPairOfPants = 2.95M;
                      const decimal PriceOneDress     = 4.55M;
                                                                 347
OrderInformation Order;
          // To make it easy for the user, we will request different values for the d
          try
          {
                  int month = DateTime.Today.Month;
                  int day   = DateTime.Today.Day;
                  int year = DateTime.Today.Year;
                 try
                 {
                         Console.Write("Enter the year this order was placed: ");
                         year = int.Parse(Console.ReadLine());
                         if( year < 1998 )
                                 throw new ArgumentOutOfRangeException("This business
                                                "Any year prior to that value is inv
                         if( year > DateTime.Today.Year )
                                 throw new ArgumentOutOfRangeException("Common... " +
                                                "We can't process an order that will
                 }
                 // Exception to deal with if the user typed a non-digit in the valu
                 catch(FormatException ex)
                 {
                         Console.WriteLine("Error: " + ex.Message +
                                        "\nYou entered an invalid value for the year
                 }
                 // Exception to deal with if the user typed a negative number
                 catch(ArgumentOutOfRangeException ex)
                 {
                         Console.WriteLine(ex.Message);
                 }
                                                                  348
. . . No Change
                                   ShowReceipt();
                          }
using System;
class Exercise
{
        static void Main()
        {
                double Number1, Number2;
                double Result = 0.00;
                char Operator;
                    try
                    {
                              Console.WriteLine("To proceed, enter");
                   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:
using System;
class Exercise
{
        static void Main()
        {
                                                                                          351
                 try
                 {
                        Console.WriteLine("To proceed, enter");
                 switch(Symbol)
                 {
                         case '+':
                                 Result = Value1 + Value2;
                                 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 '*':
                                Result = Value1 * Value2;
                                                                                                    352
                       case '/':
                               // The following exception is nested in the previous try
                               try
                               {
                                       if(Value2 == 0)
                                               throw new DivideByZeroException("Division
by zero is not allowed");
                   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 = '/';
                   try
                   {
                            Console.WriteLine("To proceed, enter two numbers:");
                            Number1 = double.Parse(Console.ReadLine());
                            Number2 = double.Parse(Console.ReadLine());;
                            if( Number2 == 0 )
                                    throw new DivideByZeroException("Division by zero is not
allowed");
                    }
                    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;
                    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);
                    }
           }
                    // 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);
           }
}
    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
                                                                                               354
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.
                              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:
using System;
namespace CSharpLessons
{
    class Exercise
    {
        static void Main()
        {
            double number1 = 12.44,
                                                                                                         355
                       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.
using System;
          class IceCream
          {
                  public const decimal BasePrice             = 1.55M;
                           do
                           {
                                     try
                                     {
                                              Console.WriteLine("What type of flavor do you want?");
                                              Console.WriteLine("1 - ");
                                              Console.WriteLine("2 - ");
                                              Console.WriteLine("3 - ");
                                              Console.WriteLine("4 - ");
                                              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!");
                                     }
                                                                                     356
                     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!");
                            }
                     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)
                            {
                                    Console.WriteLine("You must enter a valid number and no other
     character!");
                            }
                     do
                     {
                                                                                                        357
                                  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!");
                                  }
using System;
      class Exercise
      {
          static void Main()
          {
          }
      }
    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:
    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.
using System;
namespace CSharpLessons
{
    class Exercise
    {
        static void Main()
        {
            double[] number = new double[5];
                          Console.WriteLine();
           }
      }
}
using System;
          class IceCream
          {
                  public const decimal BasePrice            = 1.55M;
                         int dFlv   = 0,
                             dCont = 0,
                             dIngrd = 0;
                         int Scoops = 0;
                         decimal PriceIngredient, TotalPrice;
. . . 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
 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;
                                                                                                          360
                 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 };
                             Console.WriteLine();
                    }
           }
}
If you leave the square brackets empty, the compiler will figure out the number of items.
using System;
          class IceCream
                                                                                361
{
       public const decimal BasePrice      = 1.55M;
                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]);
                               Console.Write("Your Choice? " );
                               dFlv = int.Parse(Console.ReadLine());
                       }
                       catch(FormatException)
                       {
                               Console.WriteLine("You must enter a valid number and no other
character!");
                       }
                do
                {
                                                                                 362
                       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!");
                       }
                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!");
                       }
                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!");
                        }
       switch(Cont)
       {
               case 2:
                         Console.WriteLine("Container:    Cup");
                         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:
                                                                                            364
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:
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.
using System;
     namespace CSharpLessons
     {
         class Exercise
         {
             static int Main()
             {
                 string FirstName = "James";
                 string LastName = "Weinberg";
                  double WeeklyHours = 36.50;
                 double HourlySalary = 12.58;
                 Console.WriteLine("Employee Payroll");
                 Console.WriteLine("Full Name:    {0}", FullName);
     Console.WriteLine("WeeklySalary: {0}", WeeklySalary.ToString("C"));
                   return 0;
              }
          }
     }
      C:\CSharp Lessons\CommandLine1>Exercise
      Employee Payroll
      Full Name:    Weinberg, James
      WeeklySalary: $459.17
      C:\CSharp Lessons\CommandLine1>
   7. Return to your text editor
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:
                                                                                                       368
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.
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]);
                   HourlySalary = Double.Parse(Argument[3]);
                                                                                                            369
                  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
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.
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;
                }
      }
      Receipt
      Item Number: 0
      Description: Unknown
      Unit Price: $0.00
   6. Return to Notepad
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:
 Based on this, type two integers separated by a comma in the right square brackets. Here is an
 example:
using System;
namespace CSharpLessons
{
    class Exercise
    {
                                                                                                            371
    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.
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",
                                                                                                         372
                           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
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";
                members[1,0] = "Jeremy";
                members[1,1] = "Mathew";
                 members[1,2] = "Anselme";
                 members[1,3] = "Frederique";
                 Console.WriteLine();
            }
}
                                                                                    373
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 Processing
                     try
                     {
                             Console.Write("Enter Item Number: ");
                             ItemID = long.Parse(Console.ReadLine());
                     }
                     catch(FormatException)
                     {
                             Console.WriteLine("Invalid Number - The program will terminate\n");
                     }
                     Console.WriteLine("Receipt");
                                                                                                  374
                       return 0;
                }
      }
   2. Save, compile, and execute the application. Here is an example:
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.
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},
                                                                                                           375
                        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 30
Number 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},
                                                            {    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]     =   12.44
Number[0][0][1]     =   525.38
Number[0][0][2]     =   -6.28
Number[0][0][3]     =   2448.32
Number[0][0][4]     =   632.04
Number[0][1][0]     =   -378.05
Number[0][1][1]     =   48.14
Number[0][1][2]     =   634.18
Number[0][1][3]     =   762.48
Number[0][1][4]     =   83.02
Number[0][2][0]     =   64.92
Number[0][2][1]     =   -7.44
Number[0][2][2]     =   86.74
Number[0][2][3]     =   -534.6
Number[0][2][4]     =   386.73
Number[1][0][0]     =   48.02
Number[1][0][1]     =   120.44
Number[1][0][2]     =   38.62
Number[1][0][3]     =   526.82
Number[1][0][4]     =   1704.62
                                                                                                 377
Number[1][1][0]   =   56.85
Number[1][1][1]   =   105.48
Number[1][1][2]   =   363.31
Number[1][1][3]   =   172.62
Number[1][1][4]   =   128.48
Number[1][2][0]   =   906.68
Number[1][2][1]   =   47.12
Number[1][2][2]   =   -166.07
Number[1][2][3]   =   4444.26
Number[1][2][4]   =   408.62
Number of items 30
Number 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:
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);
                                                                                                         378
                           return 0;
                  }
        }
        Receipt
        Item Number: 0
        Description: Unknown
        Unit Price: $0.00
     6. Return to Notepad
using System;
namespace CSharpLessons
{
        class Exercise
        {
                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()
                                                                                                         379
                    {
                             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][];
                             Console.WriteLine();
                    }
           }
}
    Practical Learning: Initializing a Jagged Array
     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
                                                                               380
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",
                                                             "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);
                                                                                                 381
                                return 0;
                      }
          }
     2. Save the file
using System;
namespace CSharpLessons
{
    class Exercise
    {
        static void Main()
        {
            string[][] Members = new string[2][];
Member        1:   Celeste
Member        2:   Mathurin
Member        3:   Alex
Member        4:   Germain
Member        5:   Jeremy
Member        6:   Mathew
Member        7:   Anselme
Member        8:   Frederique
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",
                                                               "Stitched Center-Bar Belt" }} };
                decimal[][][] UnitPrice =
                        new decimal[2][][]{
                                new decimal[][]{
                                        new decimal[]{ 275.25M, 180.00M, 50.00M, 265.00M,
                                                                                  383
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 Processing
               try
               {
                       Console.Write("Enter Item Number: ");
                       ItemID = long.Parse(Console.ReadLine());
               }
               catch(FormatException)
               {
                       Console.WriteLine("Invalid Number - The program will terminate\n");
               }
               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;
                                                                                                          384
                   }
          }
     2. Save, compile, and execute the file
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];
    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];
Item Retrieval
    To locate an item of an array, we were using its index. Here is an example:
                                                                                                      385
class Exercise
{
    static void Main()
    {
        string[] EmployeeName = new string[4];
           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];
           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));
      }
}
using System;
namespace CSharpLessons
{
    class Exercise
    {
                                                                                                       386
                          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 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.
 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:
 The highest member of an array can be located using the Array.GetUpperBound() method. Its
 syntax is:
 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;
using System;
namespace Arithmetic
{
        class NumberCrunching
        {
                double[] number;
        }
}
                                                                                          388
using System;
     namespace VendingMachine
     {
         // This class is used to create an ice cream
         // And to process an order
         sealed class IceCream
         {
         }
     }
using System;
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
    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];
        }
}
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;
                    }
           }
                                                                                        390
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
               // 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;
    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()
                   {
                           Number[0] = 12.44;
                           Number[1] = 525.38;
                           Number[2] = 6.28;
                           Number[3] = 2448.32;
                           Number[4] = 632.04;
                   }
          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 it
                public 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
                                                                        392
// 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";
// This method requests a flavor from the user and returns the choice
public 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 number
        try
        {
            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!");
        }
                                                                          393
    // Make sure the user selects a valid number that represents a container
    do
    {
        // If the user types a symbol that is not a number
        try
        {
            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!");
        }
    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());
         }
                                                                       394
       catch(FormatException)
       {
           Console.WriteLine("You must enter a valid number and no other character!");
       }
    return Choice;
}
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
                                                                                          396
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:\CSharp Lessons\IceCream3>
     8. Return to Notepad
class Exercise
{
    static int Main()
    {
        string[] EmployeeName = new string[4];
           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:
ms+=