C#, The Basics

Peter Kitson

ISBN : 9812140344

Order a printed copy of this book from Amazon.

Cover Design - C#, The Basics

For your free electronic copy of this book please verify the numbers below. 

(We need to do this to make sure you're a person and not a malicious script)



Sample Chapter From C#, The Basics
     Copyright © Vijay Mukhi, Sandeep Shanbhag, Sonal Mukhi

Getting Started

C# is pronounced as \"C sharp\". It is a new programming language that enables programmers in quickly building solutions for the Microsoft .NET platform.

Today, one cannot, just cannot, afford to ignore C#. It is our considered opinion that it holds immense promise and we are going to try our best, through this book, to help you realize its potential. Be assured, we are not going to teach you just another programming language. It is our intention to help you apply C# in practical situations, to actually implement your newly acquired knowledge on the Net.

With this brief introduction, let us embark on a path that will take you to new adventures in the world of Internet. In this chapter, we will get you started with C# by introducing a few very simple programs. For remember, even a journey of a thousand miles must begin with a single step.

We assume that you have no prior knowledge of any programming language. But before we get ensnared in the fascinating world of C#, let\'s make a directory where we will save all our work. In order to do so, click on Start, Programs, then go to Accessories and select Command Prompt (Windows 2000) or the MS-DOS Prompt as it is called in Windows 98.  Once you are at the command prompt create a directory called csharp (md csharp) and change to this directory (cd csharp). Now type the command \'edit a.cs\', which will open the MS-DOS editor - the world\'s simplest editor

C:\csharp>edit a.cs

Yes, we very well understand how you must be yearning to write your first C# program and get it working. But before we do that, there are certain intricacies that you must understand. What a.cs refers to is called the filename or program name. Here we have named our file or program a.cs. Why a.cs? Well, before we began writing this book, we consulted a renowned astrologer who predicted that if we named our first file a.cs then great luck would be showered on us. Not wanting to quarrel with the stars, we named our file a.cs. But you are free to go ahead and call your file any name you want. But then do so at your own risk! Remember, forewarned is forearmed!

Jokes aside, \'cs\' is the extension used for C# files. They say of all the things you wear, your expression is the most important. Notwithstanding this, one does look more dapper in a suit rather than a vapid shirt and trousers. Similarly, though it is not mandatory to provide the extension \'cs\', you can make a filename seem more impressive by giving it an extension. To reiterate, you could have given the extension say \'ws\' too; it does not matter. But absent minded as we are, it is more prudent to give appropriate extensions while naming files

As the first step, we will understand the basic structure of a C# program


class zzz



Here we start with the word class zzz followed by open and close curly braces. A class is nothing but a collection --- a collection of everything that the programming language contains. It is like a packet or a container, which can hold anything. Hence everything in a C# program must be enclosed within a class. We have named our class zzz, again you could have named it anything else but if you would rather follow our naming convention (for reasons well amplified above!), name it zzz.


Now for the part that you\'ve been eagerly waiting for

In order to execute the program, go to the File menu, and click on Exit. You will get a dialog box asking you whether you want to save the file or not, say yes. Now that we have typed and saved our file we need to execute it. The compiler creates executable code. The command used to call the C# compiler is csc followed by the program name. Since our program name is a.cs, the command csc a.cs will call the compiler. A compiler is a program which understands the C# programming language. Thus the word class is part and parcel of the C# language. Microsoft lets you freely download the C# compiler from their web site : http://msdn.microsoft.com/library/default.asp.  Select .Net Framework SDK under .NET Development. Choose the Download option to download the sdk which is around 127 MB large. Install the product on your machine cause if you don’t, none of the following programs will work. Also, Internet Explorer 5.5 and Microsoft Data Access Components(2.7) must be installed prior to installing the sd

Once done, type the command as follows:

C:\csharp>csc a.c

You will see the following output on your screen in the dos box

Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914] Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

error CS5001: Program ‘a.exe’ does not have an entry point define

Just as the excitement was beginning to grow, our program returns with an error message. Don\'t worry, occasional failure is the price of improvement.

The error message starts with an error number CS5001 followed by a cryptic message, which we do not understand.

We are aware of the fact that everything has a beginning and an end. Similarly, a C# program also has a start and an end. Ah! Now you realize why the error occurred. We forgot to tell C# where to start executing our program from. This starting point is also called an entry point

You can specify the entry point by adding static void Main() to your program, just as we have done below


class zzz


static void Main()




Compile the above code giving the command as csc a.cs.  Voila! Now no errors.

The compiler will now generate an exe file called a.exe. Giving the command dir at the command prompt will prove the same. On keen observation you will notice that among the 2 files listed, there is a file by the name a.exe. Simply type \'a\' at the command prompt, your program will now be executed!


The program will run but shows no output on our screen. But, at least we get no errors.

The words, static and void, will be explained to you a little later, in the right perspective. Thus if you had felt the beginnings of a massive headache, you can breathe easy! Anything followed by  \'(\' and \')\' brackets is called a function. So, it is obvious that Main is nothing but a function. Here we are creating a function called Main. It is followed by the \'{\' and \'}\' curly braces. Note that the letter M in Main is capital. Thus C# requires a function called Main, which is the first function that will be executed. Failure to do so will result in an error. Ergo, whenever you see a word beginning with an open \'(\' and close bracket \')\', C# and most other programming languages call it a function. The { signifies the start of a function and the } signifies the end of the function. The guys who designed the language decided to use {} braces instead of start and end. When you tell people that you are learning a programming language, you are actually learning to use {} braces to specify the beginning and end of a function. These rules have to be remembered by rote. You have no choice.


Now we are ready to add some code in our program. In order to do so, add the line WriteLine(\"Hell\"), just as we have done below.