Hello C#

This article has been excerpted from book "Visual C# Programmer's Guide"

C# Compiler

Although we assume you are using Visual Studio .NET, there is another way for you to get the C# compiler and compile your programs from the command line. The command line C# compiler comes along with the .NET Framework Software Development Kit (SDK). The URL for retrieving these files can be found in the Downloads section of the Web site C# Corner (
http://www.csharpcorner. com).

Hello, C# As we discussed in the previous article, you can use any text editor to type C# programs. After typing the code, save it with the .cs extension and compile it from the command line using the C# compiler called csc.

Listing 3.1 shows the Hello, C# program. Type this program in a text editor such as Notepad or in the Visual Studio .NET editor. The output of the program in Listing 1 to the system console produce "Hello, C#!" after it is compiled and run.

Listing 1: "Hello, C#" Example

using System;


class
Hello
{

static
void Main()
{

Console
.WriteLine("Hello, C#!");
}


Save the file as hello.cs and compile it from the command line using the C# compiler, using the following syntax:

csc hello.cs or C:\temp> csc hello.cs

Make sure the file path is correct.

If you don't find csc.exe in your current directory, provide the full path of csc.exe. For example, if your csc.exe is in C:\Program Files\.NET\Exes\, then compile your code from the command prompt using this command:

C:\Program Files\.NET\Exes\csc.exe
C:\hello.cs

After compiling your code, the C# compiler creates an executable file called hello.exe in the bin directory. Figure 3.1 shows the executable file as it is running.

helloImage1.gif

Figure 1: "Hello, C#" Output in Command Line

Description of "Hello, C#"

The first line of your program is using System.

using System;

The .NET Framework class library is defined in namespaces. The class keyword is similar to the C++ class keyword and is used to define a new class that is followed by a class name. For example:

class
Hello
{
...
}

The next line of code may consist of the static void Main() function, but this depends on what form of the Main method you are using. The different forms of the Main method are discussed later in this article.

In C# every application must include a static Main() or an int Main() method. This method is similar to the main() function of C or C++. This is the entry point of an application. Although an application can have multiple Main methods, you can assign only one of them as the application's entry point. For example:

static
void Main()
{
Console.WriteLine("Hello, C#!");
}


The next line of code uses the System.Console class to write output to the console. WriteLine(), a method of the Console class, writes a string followed by a line terminator to the console. The Console class defined in the System namespace contains methods to read and write from the console.

Compiler Options

When you compile a C# program, the compiler doesn't generate a native (executable) file that is executed directly by the system; rather, it generates a Common Intermediate Language file, which is read by the Common Language Runtime at runtime and then executed. Table 1 lists several compiler options you can use in your command line along with the C# compiler and describes what they do.

helloImage2.gif

Table 1: Compiler Command Line Options

The Multiple Faces of the Main Method

The Main method has a few different declarations from which you can choose. Some of its declarations are discussed here.

The first version of the Main method is the void type. This method contains no return and no arguments. The second version of the Main method has no returns but takes arguments, and the third type returns an integer value but no arguments. The different versions of the Main method are shown in Listing 2.

Listing 2: Different Versions of the Main Method

// static Main no return, no arguments

static
void Main()
{
Console.WriteLine("Hello, C#!");
}

// static Main no return, arguments

public
static void Main( string[] args)
{
Console.WriteLine("Hello, C#!");
}

// static Main no return, arguments

public
static int Main()
{
Console.WriteLine("Hello, C#!");

return
0;
}


Command Line Parameters

One form of the Main method plays a vital role when you need to process command line parameters. This Main method takes an argument of a string array. For example:

// static Main no return, arguments

public
static void Main( string[] args)
{
Console.WriteLine("Hello, C#!");
}

Let's look at a sample of passing a string array and writing out the arguments. Listing 3 shows such an example.

Listing 3: Hello.cs Command Line Arguments

using System;


class
Hello
{
   
// static Main no return, arguments
    public static void Main(string[] args)
    {
        for (int i = 0; i < args.Length; i++)
        {
            Console.Write("Args {0}, {1}", i, args[i]);
            Console.Write("\n");
        }
    }
}


When you run the hello.exe generated by the program shown in Listing 2, the output looks like that shown in Figure 2 (after you press ENTER in the command line).

helloImage3.gif

Figure 2: Command Line Output of Hello.cs

Another interesting example presented in Listing 4 takes three arguments. The first argument is either Add or Subtract, and the second and third arguments are two numbers. If the first argument is Add, the output is the sum of two numbers. If the first argument is Subtract, the output is the difference of two numbers.

Listing 4: Add and Remove Arguments from Command Line


using
System;

class
Hello
{
   
// static Main no return, arguments
    public static void Main(string[] args)
    {
        int num1 = Convert.ToInt16(args[1]);
        int num2 = Convert.ToInt16(args[2]);
        int result;
        if (args[0] == "Add")
        {
            result = num1 + num2;
            Console.WriteLine(result.ToString());
        }

        if (args[0] == "Subtract")
        {
            result = num1 - num2;
            Console.WriteLine(result.ToString());
        }
    }
}


Figure 3 shows the results of executing hello.exe from compiled Listing 4 and passing in the arguments to Add and Subtract 23 and 3 from the command line.

helloImage4.gif

Figure 3: Output from Using Add and Remove Arguments in the Command Line

Referencing an Assembly from the Command Line

The core functionality of the System namespace resides in mscorlib.dll, which is available to a program without referencing it. Thus, if you are using only the System namespace, you do not have to pass any assembly information as a command line argument. However, if you are using any other assemblies such as System.Drawing or System.Windows, you need to pass the assembly information as an argument.

For example, if you are using the Windows.Forms namespace in your program, you need to reference the system.winforms.dll assembly as a command line argument.

csc /r:system.winforms.dll myforms.cs

The C# compiler comes along with the .NET Framework SDK. You can install the C# compiler in one of two ways: either install the .NET SDK or install VS.NET. Writing a "Hello, World" sample in C# is quite similar to writing one in C++ or Java. The Main method is the entry point of any application. You can pass your command line arguments in the Main method and use them in your application.

Conclusion

Hope this article would have helped you in writing a simple Hello C# program using C# coding. See my other articles on the website on .NET and C#.


Similar Articles