Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The following console program is the C# version of the traditional "Hello World!" program, which displays the string Hello World!
.
using System;
// A "Hello World!" program in C#
namespace HelloWorld
{
class Hello
{
static void Main()
{
System.Console.WriteLine("Hello World!");
}
}
}
Let us now look at the important parts of this program in turn.
Comments
The first line contains a comment:
// A "Hello World!" program in C#
The characters //
convert the rest of the line to a comment. You can also comment a block of text by placing it between the characters /*
and */
, for example:
/* A "Hello World!" program in C#.
This program displays the string "Hello World!" on the screen. */
The Main Method
The C# program must contain a Main
method, in which control starts and ends. The Main
method is where you create objects and execute other methods.
The Main
method is a static method that resides inside a class or a struct. In the previous "Hello World!" example, it resides in a class called Hello
. Declare the Main
method in one of the following ways:
It can return void:
static void Main() { //... }
It can also return an int:
static int Main() { //... return 0; }
With both of the return types, it can take arguments:
static void Main(string[] args) { //... }
-or-
static int Main(string[] args) { //... return 0; }
The parameter of the Main
method is a string
array that represents the command-line arguments used to invoke the program. Notice that, unlike C++, this array does not include the name of the executable (exe) file.
For more information on using command-line arguments, see the example in Main() and Command Line Arguments (C# Programming Guide) and How to: Create and Use C# DLLs (C# Programming Guide).
Input and Output
C# programs generally use the input/output services provided by the run-time library of the .NET Framework. The statement, System.Console.WriteLine("Hello World!");
uses the WriteLine method, one of the output methods of the Console
class in the run-time library. It displays its string parameter on the standard output stream followed by a new line. Other Console methods are used for different input and output operations. If you include the using System;
directive at the beginning of the program, you can directly use the System classes and methods without fully qualifying them. For example, you can call Console.WriteLine
instead, without specifying System.Console.Writeline
:
using System;
Console.WriteLine("Hello World!");
For more information on input/output methods, see System.IO.
Compilation and Execution
You can compile the "Hello World!" program either by creating a project in the Visual Studio IDE, or by using the command line. Use the Visual Studio Command Prompt or invoke vsvars32.bat to put the Visual C# tool set on the path in your command prompt.
To compile the program from the command line:
Create the source file using any text editor and save it using a name such as
Hello.cs
. C# source code files use the extension.cs
.To invoke the compiler, enter the command:
csc Hello.cs
If your program does not contain any compilation errors, a
Hello.exe
file will be created.To run the program, enter the command:
Hello
For more information on the C# compiler and its options, see C# Compiler Options.