static void Main(string[] args) {
Console.WriteLine('Hello, world! This is my first C# program!');
Console.ReadLine();
return;
}
}
Next, you define the class name as Program
:
static void Main(string[] args) {
Console.WriteLine('Hello, world! This is my first C# program!');
Console.ReadLine();
return;
}
All C# code must be contained within a class. Because this class is within the HelloWorld
namespace, its fully qualified name is HelloWorld.Program
.
Classes and objects are discussed in detail in Chapter 4.
Within the Program
class, you have the Main()
method:
class Program {
Console.WriteLine('Hello, world! This is my first C# program!');
Console.ReadLine();
return;
}
Every C# program must have an entry point, which in this case is Main()
. An entry point is the method that is first executed when an application starts up. The static
keyword indicates that this method can be called without creating an instance of the class.
Chapters 4 and 5 provide more information about object-oriented programming.
Unlike languages such as VB.NET in which a method can be either a function or a subroutine (a function returns a value; a subroutine does not), C# only supports functions. If a function does not return a result, you simply prefix the function name with the void
keyword; otherwise, you indicate the return type by specifying its type.
You will find more about functions in Chapter 4.
Finally, you write the statements within the Main() method:
static void Main(string[] args) {
}
The WriteLine()
method from the Console
class writes a string to the command prompt. Notice that in C# you end each statement with a semicolon (;
), which indicates to the compiler the end of each statement. Hence, you can rewrite the WriteLine()
statement like this:
Console.WriteLine(
'Hello, world! This is my first C# program!');
This is useful when you have a long statement and need to format it to fit into multiple lines for ease of reading.
The use of the ReadLine()
statement is to accept inputs from the user. The statement is used here mainly to keep the command window visible. If you run this program in Visual Studio 2008 without using the ReadLine()
method, the program will print the hello world statement and then close the window immediately.
Passing Arguments to Main()
If you run a program in the command prompt as described earlier in the chapter, you can pass in arguments to the application. For example, you might want the program to display your name. To do so, pass in the name like this:
C:C#>HelloWorld Wei-Meng Lee
The argument passed into the program can be accessed by the args parameter (a string array) defined in the Main() method. Hence, you need to modify the program by displaying the values contained in the args string array, like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld {
class Program {
static void Main(string[] args) {
Console.ReadLine();
return;
}
}
}
Chapter 8 covers string arrays in depth.
Language Syntax