Programming with C# - - 101, Page 5
Entering and Compiling hello.cs
To enter and compile the hello.cs program, follow these steps:
- Start your editor.
- Use the keyboard to type the hello.cs source code shown in Listing 1.1. Don't enter the line numbers or colons. These are provided only for reference within this book. Press Enter at the end of each line. Make sure that you enter the code using the same case. C# is case sensitive, so if you change the capitalization, you will get errors.
If you are a C or C++ programmer, you will most likely make a common mistake. In C and C++, main() is lowercase. In C#, Main() has a capital M. In C#, if you type a lowercase m, you will get an error.
- Save the source code. You should name the file hello.cs.
- Verify that hello.cs has been saved by listing the files in the directory or folder.
- Compile hello.cs. If you are using the command-line compiler, enter the following:
If you are using an Integrated Development Environment, select the appropriate icon or menu option. You should get a message stating that there were no errors or warnings.
- Check the compiler messages. If you receive no errors or warnings, everything should be okay.
If you made an error typing the program, the compiler will catch it and display an error message. For example, if you misspelled the word Console as Consol, you would see a message similar to the following:
hello.cs(5,7): error CS0117: 'System' does not contain a definition for 'Consol'
- Go back to step 2 if this or any other error message is displayed. Open the hello.cs file in your editor. Compare your file's contents carefully with Listing 1.1, make any necessary corrections, and continue with step 3.
- Your first C# program should now be compiled and ready to run. If you display a directory listing of all files named hello (with any extension), you should see the following:
hello.cs, the source code file you created with your editorhello.exe, the executable program created when you compiled hello.cs
- To execute, or run, hello.exe, enter hello at the command line. The message Hello, World! is displayed onscreen.
|If you run the hello program by double-clicking in Microsoft's Windows Explorer, you might not see the results. This program runs in a command-line window. When you double-click in Windows Explorer, the program opens a command-line window, runs the program, and[md]because the program is done - closes the window. This can happen so fast that it doesn't seem that anything happens. It is better to open a command-line window, change to the directory containing the program, and then run the program from the command line.|
Congratulations! You have just entered, compiled, and run your first C# program. Admittedly, hello.cs is a simple program that doesn't do anything useful, but it's a start. In fact, most of today's expert programmers started learning in this same way - by compiling a "hello world" program.
| This article is brought to you by Sams Publishing and Bradley L. Jones. |
This article is an excerpt from Sams Teach Yourself C# in 21 Days
© Copyright Bradley L. Jones, All Rights Reserved
# # #