February 28, 2021
Hot Topics:

Programming with C# - - 101

  • By Bradley L. Jones
  • Send Email »
  • More Articles »

Entering and Compiling hello.cs

To enter and compile the hello.cs program, follow these steps:

  1. Start your editor.
  2. 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.

  3. Save the source code. You should name the file hello.cs.
  4. Verify that hello.cs has been saved by listing the files in the directory or folder.
  5. Compile hello.cs. If you are using the command-line compiler, enter the following:

    csc hello.cs

    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.

  6. 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'
  7. 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.
  8. 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
  9. 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

# # #

Page 5 of 5

This article was originally published on November 19, 2001

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date