Tutorials » DotNet tutorials »

"Hello World" Application


It is time to get started with code. In this chapter, we will show you how to write your first C# program. This chapter assumes that you have already installed and configured Visual Studio .NET in your computer.


Here we will guide you through step by step process to create your first sample .NET application. All our sample code will be using C# syntax. Readers are suggested to stick to this new elegant language, than going back to your favorite VB style.

Hello World Application using C#

Let us get started the traditional way, with a 'Hello World' application. We hope you all have Visual Studio.NET installed on your computer. If you have VS.NET, goto the menu option File > New > Project. Select 'Visual C# projects' and choose the project template 'Console Application'. This will create a default

A simple C# program would look like the following.


using System;

public class Hello
{
public static void Main (string[] args)
{
Console.WriteLine("Hello C# World");
}
}


When you create a new Console Application, it will create a default class and you can just insert one line of code into it:


Console.WriteLine("Hello C# World");


Now compile and run your first program by pressing Ctrl + F5. You will see the following result :

> Hello C# World

You are done! You got your first C# program successfully running.




If you do not have VS.NET, you may use any editor (including notepad) to create your first C# program. Create a new file with the name sample.cs and type the C# code shown. Now go to the command prompt and navigate to the folder where you have the .NET framework installed. Compile your csharp file by using the command 'csc':

csc c:\samples\sample.cs

csc is the C# compiler. The above command will produce the output file sample.exe. You can run the sample.exe and you will see the output from the program.


Analyzing your first program

See the first line of code :

using System;

"System" is a namespace and the "using" directive says that all classes in the "System" namespace can beused in this class without using the fully qualified name. In our class, "Console" is a class in the namespace "System". To use this class you have to actually write :System.Console.WriteLine ("...");

But the using System; directive on top of the classallows us to use the class without including the namespace. So, you can now simply write :

Console.WriteLine("...");

If you are familiar with Object Oriented Programming, you might not need more explanation for the next line - declaring a class.

public static void Main (string[] args) - is the Main method, which is thestarting point of the application.

string[] args is the list of arguments that are passed to this application. (In our case, we are not passing any command line parameters).

Console.WriteLine("...") is another line of important code. Console is a class, part of .NET class library included in System namespace. WriteLine is a method part if this class and used to printoutput to the default Console.




Next Chapter: C# Language Syntax
Previous Chapter: Download VS.NET
More Chapters: .NET Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India