|
This tutorial shows several versions of a Hello World program in C#.
TutorialThe following examples show different ways of writing the C# Hello World program. Example 1// Hello1.cs
public class Hello1
{
public static void Main()
{
System.Console.WriteLine("Hello, World!");
}
}
OutputHello, World! Code Discussion
Example 2To avoid fully qualifying classes throughout a program, you can use the // Hello2.cs
using System;
public class Hello2
{
public static void Main()
{
Console.WriteLine("Hello, World!");
}
}
OutputHello, World! Example 3If you need access to the command line parameters passed in to your application, simply change the signature of the // Hello3.cs
// arguments: A B C D
using System;
public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
OutputHello, World! You entered the following 4 command line arguments: A B C D Example 4To return a return code, change the signature of the // Hello4.cs
using System;
public class Hello4
{
public static int Main(string[] args)
{
Console.WriteLine("Hello, World!");
return 0;
}
}
OutputHello, World! See Also |
