C# - Your First C# Program

Type the following into a text editor (such as Notepad), and save it with a .cs extension (for example, Keen.cs). The Main() method is shown here

  using System;
  namespace Keen
        public class Program
            static void Main()
                  Console.WriteLine("Hello from Keen.");

Compiling and Running the Program

You can compile this program by simply running the C# command-line compiler (csc.exe) against the source file, like this:

csc Keen.cs

If you want to compile code from the command line using the csc command, you should be aware that the .NET command-line tools, including csc, are available only if certain environment variables have been set up. Depending on how you installed .NET (and Visual Studio), this may or may not be the case on your machine.

Compiling the code produces an executable file named Keen.exe, which you can run from the command line or from Windows Explorer like any other executable. Give it a try:

csc Keen.cs 

If you type csc Keen.cs in the command window, and you see

    'csc' is not recognized as an internal or external command,
    operable program or batch file

then it means that you must help the command window locate the C# compiler, csc. Here's how:

1. First, locate the C# compiler on your computer. (You can use your computer's search facility: press Start in the lower left corner, press Search, then within the Search window, select ``Search all files'' and then type a search for csc.exe.)

You will probably locate the program in a folder named something like

c:\Windows\Microsoft.NET\Framework\vX.X.XXX Should contain the latest 32 bit version of csc.exe

c:\Windows\Microsoft.NET\Framework64\vX.X.XXX Should contain the lastest 64 bit version of csc.exe

This folder is the 'path' to the compiler.

2. Next, you must tell the command window the path to the compiler. The simplest way to do this is to open a new command window and type this command:
set path=c:\Windows\Microsoft.NET\Framework64\vX.X.XXX

Now, you can cd to the folder that contains your C# program and type csc Keen.cs