Hello World sample program in C#
By: Ram Baskar
The following is a very simple C# program, a version of the classic "Hello, world" example:
The effect is to write the following text to the output console:
using System;
class ExampleClass
{
static void Main()
{
Console.WriteLine("Hello, world!");
}
}
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!");
}
}
Archived Comments
1. excellent job,very impressive.you have done a quite good job.I wondered about that others have not t
View Tutorial By: Massaging Back of Legs at 2012-03-29 06:34:55
Most Viewed Articles (in Csharp ) |
Latest Articles (in Csharp) |
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview