C Primer Plus (5th Edition)

   

The Seven Steps to Writing a Simple C# Program

After you have successfully installed the .NET SDK and chosen a suitable editor you are ready to write and run your C# programs. This can be accomplished by performing the following seven steps (shown here by using pseudocode):

  1. Open the command console.

  2. Use the chosen text editor to type and edit the C# source code and save this text as a .cs file.

  3. Use the C# language compiler to turn this source code into a PE (.exe) or DLL (.dll) file.

  4. If the compiler detects any syntax errors in step 3, go back to step 2 and correct these errors. Continue through steps 3, 4, and so on.

  5. Run the program.

  6. Verify that the output matches your expectations. If this is not the case, go back to step 2 and correct the mistakes in the C# code. Then continue through steps 3, 4, and so on.

  7. Time to celebrate your first C# program.

Let's take a closer look at each of these steps.

Opening and Using the Command Console (Step 1)

The command console is like a control center. It allows the user to operate the computer completely by typing special commands with the keyboard.

Throughout this book, you will be using the command line console to launch Notepad, create directories, copy files, initiate the C# compiler, execute your programs, and view their output. The source code itself is written inside Notepad.

You can open the command console by first selecting Run option on the Start menu and then typing cmd (see Figure 2.8) in the text field. Click OK. You should then see the command console appear, as shown in Figure 2.9. In general, commands are typed after the C:\> prompt and executed by pressing the Enter key on the keyboard. In this text, we refer to the C:\> prompt, but this could also be D:\> or E:\>, depending on the disk drive you are using.

Figure 2.8. Type cmd in the Run window.

Figure 2.9. The command console window.

Appendix F, "Using DOS Ccommands in the Console Window" explains the basic command console options you will need.

Note

The conventions used in this book for explaining how to give commands in the command console are illustrated by this example:

C:\>cd mydirectory <enter>

The C:\> part, and any other text that is printed by the computer, is written with normal text. The part you type, in this case cd mydirectory, is bolded. Any named keys are indicated with <> angle braces. <enter> here for example means that you have to press the Enter key.

Before writing the C# source code, you need to create a directory where it can be stored. Create a directory called MyC#Programs for this purpose with the following command:

C:\>md MyC#Programs<enter>

Enter this new directory by typing the following:

C:\>cd MyC#Programs<enter>

Typing and Saving the C# Source Code (Step 2)

Use the chosen text editor to type and edit the C# source code and save this text as a .cs file.

First you need to start Notepad by typing the following command in the console window::

C:\>MyC#Programs>notepad<enter>

You will see Notepad appear, as shown in Figure 2.10.

Figure 2.10. The Notepad window.

You are now ready to write the C# program in the text editor.

Our first C# program will print out the following lines on the command console:

Though this be madness yet there is method in it William Shakespeare

Even though the quote was written many years ago, before the advent of computers, it might still be a good description of what most people feel like when they learn a new programming language.

Listing 2.1 contains a C# program that will print the quote by Shakespeare on the command console.

The line numbers and colons appearing on the left side of Listing 2.1 are not part of the program. They are included to help me refer to specific lines by referring to their numbers and are not to be included when you type in the program with the editor. However, all other details, such as semicolons, parenthesis, and braces, must be entered exactly as shown. The C# compiler is case sensitive, meaning Main, main, and maiN are all different. Consequently, you must capitalize all letters exactly as written in Listing 2.1.

Note

Typing mistakes will likely cause syntax errors and will be reported by the compiler.

You are not meant to understand the C# program shown in Listing 2.1. It is only intended to show you the mechanics of how to write, compile, and run a C# program. However, it can still be enlightening to read through the code and make a guess about what is happening in each line. Later, you can compare your guesses with the brief explanations of each line that I provide.

Listing 2.1 Source code of Shakespeare.cs

01: using System; 02: public class Shakespeare 03: { 04: public static void Main() 05: { 06: Console.WriteLine("Though this be madness"); 07: Console.WriteLine("yet there is method in it"); 08: Console.WriteLine("William Shakespeare"); 09: } 10: }

After you have finished typing the program, your Notepad window should look like Figure 2.11.

Figure 2.11. Notepad window displaying the Shakespeare.cs source code.

Save the program by selecting Save As on the File menu of Notepad and typing Shakespeare.cs in the File Name text box. Your screen should now look similar to Figure 2.12. Then click the Save button.

You are now ready to activate the C# compiler and turn the brand new Shakespeare.cs file into a PE file.

Note

When your source code is ready to be compiled, you can turn it into a PE file (.exe file extension) or a DLL file (.dll file extension), depending on the command you give the compiler.

Turn the Source Code into a PE (.exe) File (Step 3)

Initiate the C# compiler by giving the following command:

C:\MyC#Programs>csc Shakespeare.cs<enter>

Now hold your breath and wait with excitement…if the compiler detected no syntax errors, it will print two lines stating the compiler version and a copyright message, similar to what is shown in Figure 2.13. Notice that the compiler version and other details might vary, the important part is that no error messages or other complaints were printed.

Figure 2.13. No errors detected by the compiler.

Note

A successful compilation does not mean your source code is free of errors (bugs). It merely means that the source code represents a valid C# program free of syntax errors.

In the event of a successful compilation, the compiler creates a file called Shakespeare.exe and saves it in the same folder that contains Shakespeare.cs. Shakespeare.exe is a PE file and is also an assembly containing MSIL and a manifest (metadata).

But what if a typing error had sneaked its way into the source code?

If the Compiler Detects Syntax Errors (Step 4)

Let's have a bit of fun with the compiler and watch how it responds to a syntax error.

To enhance the understanding for humans viewing the source code, various styles are being used when writing it. Listing 2.1 follows a specific style of presenting the source code. For example, putting the semicolon in position (1,13) looks much better and clearer than putting it in position (1,34). Style is discussed further in Chapter 3.

Let's return to our Shakespeare.cs file. Use Notepad to put the missing semicolon back so that the source file looks exactly like Listing 2.1 again. Compile the source code as already shown and verify that the compiler reports no errors. It is now time to execute your Shakespeare.exe file.

Run the Program (Step 5)

To run the PE file Shakespeare.exe, you type the following command:

C:\MyC#Programs>Shakespeare<enter>

The JIT compiler is activated and machine language is emitted that is then executed, resulting in the now familiar lines

Though this be madness

yet there is method in it

William Shakespeare

Verify the Output (Step 6)

Did you get the correct output? If the answer is no, then go back to step 2 of the seven steps. Compare your source code with Listing 2.1 and find the mistake. Correct the code with the editor. Even if you only change a semicolon in the source code, you still need to go through step 3, 4 and so on until the program is working.

If the answer is yes, then congratulations! You have successfully executed your first C# program. You can move to the last step.

Time to Celebrate (Step 7)

Now it's time to celebrate !


   

Категории