Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Editing Your First C# Program

We mentioned earlier that .NET is basically a collection of floating abstractions. As such, our first program pays tribute to .NET by simply printing the message Flotation is Groovy! Remember that the point of this first program is simply to test whether you've installed the C# compiler correctly. We'll look at how to program a more useful application—vampire bots—in the next article. For now, let's get started on creating our message:

  1. Open your favorite text editor. We'll use Notepad, as shown in the figures in this section. We're still working at the prompts, so to start notepad you type notepad and press Enter (see Figure 35).

    A blank notepad window opens (see Figure 36).

    Figure 35 Starting Notepad.

    Figure 36 Notepad window.

    One of the first statements in any C# program that prints out a lot of messages is this: using System; (the semicolon ends the command). System in this command is what's known as a namespace. We'll define namespaces more formally in the next article. For now, think of a namespace as a folder for "free" code that you don't have to write; when you use a namespace you gain access to that code, such as the code for printing things out to a screen.

  2. Type using System; into the Notepad window (see Figure 37).


    C# is case sensitive, so be sure to type the statement exactly as displayed.

    All the code for a C# program is contained inside a class. This is the syntax for a simple class:

    class class_name { }

    We'll give our class the name test.

  3. Type the following lines in Notepad (see Figure 38). The opening and closing braces are actually part of the command, but we'll place them on separate lines for readability.

    class test

    Figure 37 Adding the System namespace.

    Figure 38 Adding a class block.

    We're almost done. Every standalone C# program also has a method named Main(), which is where code execution starts. If you've never done object-oriented programming and don't know what a method is, think of it as a "function inside a class." You can declare the simplest form of Main() as follows:

    public static void Main() { }

    In Part 2 of this tutorial, we'll look at how to modify Main() so that it passes command-line arguments to a program. For now, let's finish up the first part of this little program.

  4. Type this command inside the class declaration (see Figure 39):

    public static void Main()

    As in step 3, we've put the opening and closing braces on separate lines, and we've added some blank lines to improve readability even more.


    Again, pay attention to case (Main is not the same as main). A lot of frustration for beginning programmers is due to mistyping case—that and forgetting semicolons at the end of statements!

    Figure 39 Adding the Main() method.

    The final step is to add the code that displays a message. To display a message, you use the Console.WriteLine() method, passing as a string parameter the message you want displayed. Here's the syntax for a simple WriteLine:


    You don't have to write this method; it's part of the System namespace. However, if you didn't declare using System; as the first line of your program, you wouldn't get to use this "free" method.

  5. Time to add the message. Type the following inside the Main() method (see Figure 40):

    Console.WriteLine("Flotation is Groovy!");

    Don't forget the semicolon at the end of the statement, and pay attention to case sensitivity (WriteLine versus Writeline).

    Figure 40 Code to display a message.

    That's it for the code, but you need to save your file before compiling.

  6. Choose File, Save As from the menu (see Figure 41). The Save As dialog box appears (see Figure 42).

    Figure 41 Preparing to save your code.

    Figure 42 The Save As dialog box.

  7. Type this filename in the File Name text box:


    By convention, C# source files end in the extension .cs.


    You need to specify the entire path (c:\csharp\) the first time you save the file (test.cs). Also, the double quotes around the filename are there to make sure that Notepad doesn't add .txt to the end of your filename (for example, test.cs.txt). (See Figure 43.)

  8. Click Save. Notice that the name of the file now appears in the Notepad window's title bar (see Figure 43).

    Figure 43 Final code.

  9. You're done writing the code; just exit the Notepad window.

Time to compile and execute the code.

  • + Share This
  • 🔖 Save To Your Account