This short article based on the classic "Hello World" sample program familiarizes you with the language syntax and introduced the command line compiler. It Illustrate how to write a C# program and compile it from the command line by exploring some of the basics of C# programming and compiler options. After completing this article ,you will know how to write a simple program in C# using a text editor and compile if from the command line.
C# Compiler
Although we assume you are using Visual Studio.NET, there is another way for you to get the C# complier and compile your program 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 website (http://www.c-sharpcorner.com).
Hello C#
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 the text editor such as notepad or in the Visual Studio.NET editor. The output of the program in Listing 3.1 to the system console produce "Hello C#!" after it is compile and run.
Listing 3.1 "Hello C#" Example
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, C#!");
}
}
save the file as hello.cs and compile it from the command line using the C# complier,using the following systax :
csc helo.cs or c:\temp > hello.cs
make sure the file path is correct.
if you dont find csc.exe in your directory,provide the full path of csc.exe. For Example, if your csc.exe in C:\Program Files\.NET\Exes, then compile your code from the command prompt using the command :
C:\Program Files\.NET\Exes\csc.exe C:\hello.cs
After compling the 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.
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.
In C# every application must include a static Main() Method. This method is similar to the main() function of C and 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 Ststem.Console class to write output the the console.Writeline(), a method of the Console class, writes a string followed by a line terminator the the console. The Console class in the System namespace contains methods to read and write from the console.We discuss this class in more depth in the next article.