Compiling and Executing C Programs without VS .NET


Compiling and Executing C# Programs without VS .NET

There is no easier way to practice running the command-line compiler than to create a stand-alone console application outside VS .NET. A console application is an application that runs inside the command prompt window. The best way to describe it (if you're an old-timer like me) is that a console application looks like a DOS app rather than a Windows app.

To run the C# command line compiler:

  1. Create a directory in which to save your work.

  2. From the Start menu choose Run.

  3. Type notepad.exe .

  4. Type in the code shown in Figure 1.28 and save the code as cstest.cs. Make sure that before you save you select All Files in the Save As Type field of the Save As dialog ( Figure 1.29 ). Otherwise, Notepad will append .txt to the end of the filename.

    Figure 1.28 All code in C# must be part of a class. For a console application, one of the classes must have a Main procedure. The runtime begins the program by executing the Main function.
     using System.Text; class HelloApp {    static void Main()    {       System.Console.WriteLine("Thank you for purchasing C# VQS Guide:");       StringBuilder str = new       StringBuilder("ASP .NET is so ");       for (int i = 0; I < 10; i++)          str.Append("cool ");       str.Append("!");       System.Console.WriteLine(       str.ToString());    } } 
    Figure 1.29. If you don't select All Files in the Save As Type field in notepad, notepad adds a .txt extension. Worse, if you have the option "Hide extensions for known file types" in the Windows Explorer folder options, you won't see the txt extension and will wonder why the ASP page isn't running properly.

    graphics/01fig29.gif

  5. If you installed Visual Studio .NET, choose Start > All Programs > Microsoft Visual Studio .NET > Visual Studio .NET Tools > Visual Studio .NET Command Prompt.

    or

    If you installed the .NET Framework SDK (and not Visual Studio .NET), choose Start > All Programs > Accessories > Command Prompt.

  6. From the Command Prompt window change to the directory you created in step 1.

  7. Type csc.exe /t:exe /out:cstest.exe cstest.cs .

  8. Press Enter.

  9. Type cstest.exe and then press Enter to execute the application ( Figure 1.30 ).

    Figure 1.30. The compiler will display errors if you didn't type the code accurately. Otherwise the compiler will just display the copyright notice. When you run the program it displays its text in the console window.

    graphics/01fig30.gif

graphics/tick.gif Tips

  • C# source files normally have the .cs extension, but you can give the source files any extension you like.

  • The parameter /t:exe tells the compiler to build a stand-alone application that runs inside the command prompt. Since /t:exe is the default option for csc.exe, omitting it also produces a console application.

  • To run a .NET EXE, the machine has to have the .NET runtime installed. This means that at the very least you must install the .NET Runtime redistributable file Microsoft provides. The concept is analogous to Java, where there must be a virtual machine installed on the machine for the Java code to run.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net