Wednesday, 2 March 2011

Chapter 3 Your First C# Program

Now lets start writing our first C# program. You will see that C# can create many different kind of programs. Each program is called a project. It is very important to understand the concept of projects. A project is a collection of programs and resources under one name and treated as a single unit. Thus if we are creating a software, it is not necessary to have different files strewn everywhere, instead each file and code is categorized and kept under one roof called project and when needed they are compiled.
C# has many different kind of projects, each project has its own distinct features, like some are for Form based programming, some are for console only, some for the web etc. In this chapter for illustration purpose we will take the simplest of all the Console Application. So without further delay fire up your Visual Studio IDE

STEP1

Open Visual Studio and click on File menu

STEP 2

Choose the New Project option from the File menu, it will show New Project Dialog Box as shown in the figure given below. Depending on your configuration you may have more or less amount of items in this window. Please click on the Visual C# entry to the left of the Dialog Box, it will bring up all the project types under C#. From that list choose Console Application. At the bottom of this dialog you will find Application Name entry, please change it to something appropriate in our case I will name it HelloWorld


STEP 3

You will be resented with this window, this is the IDE giving you a skeletal C# program pre-written for you. It does not do anything for now but saves lot of your time typing mundane and repetitive lines. We will modify the code and add a single line.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World, Welcome to C#");
    }
  }
}
Check the extra line added Console.WriteLine, this is the statement which will print a message given inside the quotes on the screen when we will execute this code.

STEP 4

To execute this program we must compile it first and check for any errors. As this program is just a basic C# code lets do it in one go and compile and execute it together.
If we press F5 it will start the BUILD(Compile) and execute the code inside the debugger(it will be explained later) Unfortunately our program is so short that you will only see the console window flashing and the closing.
Thus we will use Ctrl + F5 which starts without the debugger and after completion keeps the window open until you press some key. The output of your program should be something like the figure given below




Congratulation you have written your first C# program. Please feel free to read online documentation and modify the program as you wish, I will expand on this program and its various parts in the next post.


No comments:

Post a Comment