Hello World sample program in C#

By: Ram Baskar Emailed: 1608 times Printed: 2078 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The following is a very simple C# program, a version of the classic "Hello, world" example:

using System;
class ExampleClass
{
    static void Main()
    {
        Console.WriteLine("Hello, world!");
    }
}
The effect is to write the following text to the output console:

Hello, world!

Each line has a purpose:

using System;

The above line of code tells the compiler to use 'System' as a candidate prefix for types used in the source code. In this case, when the compiler sees use of the 'Console' type later in the source code, it tries to find a type named 'Console', first in the current assembly, followed by all referenced assemblies. In this case the compiler fails to find such a type, since the name of the type is actually 'System.Console'. The compiler then attempts to find a type named 'System.Console' by using the 'System' prefix from the using statement, and this time it succeeds. The using statement allows the programmer to state all candidate prefixes to use during compilation instead of always using full type names.

class ExampleClass 

Above is a class definition. Everything between the following pair of braces describes ExampleClass.

static void Main() 

This declares the class member method where the program begins execution. The .NET runtime calls the Main method. (Note: Main may also be called from elsewhere, like any other method, e.g. from another method of ExampleClass.) The static keyword makes the method accessible without an instance of ExampleClass. Each console application's Main entry point must be declared static. Otherwise, the program would require an instance, but any instance would require a program. To avoid that irresolvable circular dependency, C# compilers processing console applications (like that above) report an error if there is no static Main method. The void keyword declares that Main has no return value. 

Console.WriteLine("Hello, world!"); 

This line writes the output. Console is a static class in the System namespace. It provides an interface to the standard input, output, and error streams for console applications. The program calls the Console method WriteLine, which displays on the console a line with the argument, the string "Hello, world!". 

A GUI example:

using System.Windows.Forms;
class ExampleClass
{
  static void Main()
  {
    MessageBox.Show("Hello, world!");
  }
}

Csharp Home | All Csharp Tutorials | Latest Csharp Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ram Baskar
Hello World sample program in C#
Comments in C#
Preprocessor directives in C#
Boxing and unboxing in C#
Categories of datatypes in C#
Major features of C#
Which version of C# to use?
History of C#?
Why it was named C#?
Design Goals of C#
What is C#?
A complete sample program in AJAX
Handling the Server Response in AJAX
How to Make an HTTP Request in AJAX

More Tutorials in Csharp
Major features of C#
Categories of datatypes in C#
Boxing and unboxing in C#
Preprocessor directives in C#
Comments in C#
Hello World sample program in C#
SOAP serialization in C#
What is C#?
Design Goals of C#
Why it was named C#?
History of C#?
Which version of C# to use?
Convert Image to Base64 String in C#
Convert Base64 String to Image in C#

More Latest News
Most Viewed Articles (in Csharp )
Major features of C#
Convert Base64 String to Image in C#
Design Goals of C#
Why it was named C#?
History of C#?
Categories of datatypes in C#
Comments in C#
Hello World sample program in C#
Convert Image to Base64 String in C#
SOAP serialization in C#
What is C#?
Which version of C# to use?
Boxing and unboxing in C#
Preprocessor directives in C#
Most Emailed Articles (in Csharp)
History of C#?
Convert Image to Base64 String in C#
What is C#?
Why it was named C#?
Which version of C# to use?
Convert Base64 String to Image in C#
SOAP serialization in C#
Design Goals of C#
Major features of C#
Comments in C#
Boxing and unboxing in C#
Preprocessor directives in C#
Hello World sample program in C#
Categories of datatypes in C#