Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

@!$h@'s Code World
Welcome to @!$h@'s Free CodeWorld
     .NET Framework
 
   MFC/Win32/COM
 
   C# Section 
 
    Creating your "Hello World" program in C#

  • Platform: Windows NT, 2000, XP
    Programming Tools: Visual Studio .NET, C#, Notepad, .NET SDK
    Dependancies: None

About the Author

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:

  1. Using Visual Studio for .NET IDE, which provides you with the integrated compiler and debugger which you can invoke through menu commands
  2. 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:

  1. Open the Visual Studio for .NET IDE
  2. Select from menu File>New>Project, it will display a new project wizard to create your project file.
  3. Select from the project types "Visual C# Projects" and then from templates select "Console Application".
  4. Specify the project name and location and then OK.

    Project Wizard

It will create a project template and open up the file Class1.cs. Do the followings with your Class1.cs file:

  1. 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)
  2. 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!!");
    		}
    	}
    }
  3. Now build your project from Build>Build ConsoleApplication or Ctrl+Shift+B
  4. Run your program Debug>Start Without Debugging or Ctrl+F5.
  5. A console window with Hello World message will appear.

Using NotePad or Text editor

  1. Paste the code exactly as in step 2 above in your text file.
  2. Save the file with name HelloWorld.cs. (Note: Make sure you save your file with cs extention)
  3. Open your command line window(from visual studio command line tools or from Start>Run and typing cmd)
  4. Type at the command prompt to compile your program:
    csc HelloWorld.cs
  5. 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

Copyright 2003, Aisha Ikram