Aisha Ikram is a
senior software engineer and has been working mainly in VC++ 6, MFC,
ATL, COM. She now a days working on .NET framework and C#. She is also a member
of various developer's website like code project, code guru, mind cracker and
C# corner. You can write the author at
ais_ikr@yahoo.com
for your feedback concerns and problems.
Introduction
The article demonstrate how to create your first "hello world" application in C#.
Hello World is a console application. A console application is one which don't
contain the UI elements like buttons, listboxes etc.
There are two ways to create your console program:
-
Using Visual Studio for .NET IDE, which provides you with the integrated
compiler and debugger which you can invoke through menu commands
-
Using Notepad or any other text editor and saving your code with a .cs
extension. Then later one, you use command line compiler csc to compile your
file.
I will here demonstrate both of the ways.
Using Visual Studio IDE
Follow the steps below to create your project:
-
Open the Visual Studio for .NET IDE
-
Select from menu File>New>Project, it will display a new project
wizard to create your project file.
-
Select from the project types "Visual C# Projects" and then from templates
select "Console Application".
-
Specify the project name and location and then OK.

It will create a project template and open up the file Class1.cs. Do the
followings with your Class1.cs file:
-
Rename your class name and file name from Class1 to HelloWorld. You can rename
your file name using solution explorer (Ctrl+Alt+L or
View>Solution Explorer).
(Note: This is just for readability purpose)
-
Your class contains a Main method with arguments, simply delete the arguments
"arg[]" from the Main method and paste the statement
Console.WriteLine("Hello
World!!"); so that your code looks somewhat like this:
using System;
namespace ConsoleApplication
{
class HelloWorld
{
// The main entry point for the application.
static void Main()
{
Console.WriteLine("Hello World!!");
}
}
}
-
Now build your project from Build>Build ConsoleApplication or Ctrl+Shift+B
-
Run your program Debug>Start Without Debugging or Ctrl+F5.
-
A console window with Hello World message will appear.
Using NotePad or Text editor
-
Paste the code exactly as in step 2 above in your text file.
-
Save the file with name HelloWorld.cs. (Note: Make sure you save
your file with cs extention)
-
Open your command line window(from visual studio command line tools or from
Start>Run and typing cmd)
-
Type at the command prompt to compile your program:
csc HelloWorld.cs
-
To run your program type at command line:
HelloWorld
Namespace
namespace is same concept as that of package in java. In fact C# is case
sensitive and every class in C# should have unique name. Usually corelated
classes are placed within the same namespace and the scope of a class is within
that namespace. Namespaces can also be defined in C++ but in C# the
concept is significant and very useful. C#'s libraries also use namespaces to
store related classes at one place. Namespaces can be defined to any level
and a namspace may contain sub namespaces or classes.
So here System is the namespace containing a class named Console. Console is a
class for handling console relating functionality. WriteLine is a method which
writes the text at the console. Similarly ReadLine is a method to read
from console.
using
The keyword is used to refer a namespace. If you don't use "using System", you
would have to write yoru commands with full scope like
"System.Console.WriteLine()". By default the wizard automatically insert this
namespace reference to System, as System is the mostly used namespace.
Main
Main is the main entry point method in C#. In C++, it's "main". A static keyword
indicates that you can call this method without creating an object of class
HelloWorld. The arguments you deleted (arg []) are the command line arguments
if you want to specify any.
So that's it. I hope you must have enjoyed making your first Hello World
application with C#.
Keep visiting the site for updated and new articles.
Sign my guest book OR send me your feedback at ais_ikr@yahoo.com