An Introduction to C#

This tutorial explains the basics of C#, a modern object oriented programming language that was designed by Anders Hejlsberg at Microsoft.

A QUICK INTRODUCTION

The pursuit to teach any new language usually begins with the classic Hello World program. This tutorial does not break that tradition�

/*
* HelloWorld.cs
* Version 1.1
* A program that prints Hello, world! on screen
*/

class HelloWorld
{
public static void Main()
{
System.Console.WriteLine("Hello, world!");
}
}

Compiling and running:
There are currently two approaches to compiling and running this program. The more rugged solution would be to download the Microsoft.NET platform SDK, from microsoft.com and compile it using the C# command line compiler. This produces an Intermediate Language .exe that can be executed from the console.The second and more costly option would be to purchase Microsoft Visual Studio. NET. It has a neat IDE, in which the above program could be compiled under a new Solution.

Using the first option�
csc HelloWorld.cs
Microsoft (R) Visual C# Compiler Version �[CLR version�]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

In this admittedly simple case, you do not have to specify anything other than the file to compile. In particular, C# does not use the additional step of linking that is required by C/C++ etc.
The default output of the C# compiler is an executable file of the same name, and running this program generates the following output:

C:\�\HelloWorld\cs>HelloWorld
Hello, world!

Programmers familiar with JAVA will be amazed at how close C# syntax is to JAVA

As in c, c++, java // /*, */ are valid comment lines in C# also. Any line started by // or contained within /* and */ is ignored by the compiler. However in C#, XML statements may be embedded within comments and may later be used to generate documentation for the program.

Just as the entry point to a c/c++/java program is main, the entry point to a C# program is Main() *with a capital M*. But unlike in the case of the former, no arguments are passed to Main(), and it returns a void (a keyword that indicates the function does not return anything).

public and static are qualifiers for the entry point function Main(). More on this later.
From the Official Documentation:
"The compiler requires the entry point to be called Main. The entry point must also be marked with both public and static. In addition, the entry point takes no arguments and does not return anything (although different signatures for more sophisticated programs are certainly possible)."

Since C# is strictly an Object Oriented language, there needs to be at least one class in every program. In HelloWorld.cs the class that contains Main() is given the name HelloWorld (Note that the class name and the file name need not match. This program can be put into a file called myFirstProgram.cs. It will compile and run properly even then).
From the Official Documentation:
"In C#, all code must be contained in methods of a class. So, to house the entry-point code, you must first create a class. (The name of the class does not matter here)."

For those familiar to C/C++/JAVA programming System.Console.WriteLine() appears to be a function call. It is! It takes one argument and displays it on the console.

NAMESPACES

Namespaces were used earlier in C++ and now in C#. Namespaces, as the name suggests are spaces that are used to contain a set of programming entities such as classes, methods etc.
From the Official Documentation:
"C# programs are organized using namespaces. Namespaces are used both as an "internal" organization system for a program, and as an "external" organization system – a way of presenting program elements that are exposed to other programs"

This has several advantages…
In a large project, two different persons working on it may name a class similarly, which may lead to ambiguity. Namespaces offer a neat solution. Any number of independent programmers may give the same name to classes they create, provided they put everything into their own unique namespace.

For example there could be a two namespaces called Console and GUI. Both could contain a function named PutText() that displays text in the command line in case of the former and in a window in the case of the latter. Such functions could be accessed by saying Console.PutText() for the console version and GUI.PutText() for the other.

The above example was a generic programming example. In C# since every method needs to reside within a class, different namespaces could contain different classes with same names.

Getting back to HelloWorld.cs, I said at the end of the previous section that System.Console.WriteLine() is a function call. Well, it is only partly true. Actually System is a namespace that contains a class called Console, and Console contains a static method called Write Line().

A Quick note on static methods:
When a class contains a method, it cannot be accessed, like a normal c style function. It needs to be invoked using an object – which is defined as an instance of that class. But it is often required that some methods of a class be accessed without an object. To define such functions we include a qualifier called static. That explains why static is added to the Main() function. The CLR needs to call Main(), the entry point without creating any object for it.

The .NET Framework defines more than 90 namespaces that begin with the word System. System contains a class called Console that mostly contains methods that are related to console operations.

Since HelloWorld.cs only contained one statement that called WriteLine() method in Console class, we fully qualified it using
Namespace name . Class name . Method name
Image we were to make a thousand calls to it from our program… It would be tedious to reference the method using the Namespace name every time. For this purpose C# includes a keyword called using. Lets now re-write the same example…

/*
* HelloWorld.cs
* Version 1.2
* A program that prints Hello, world! on screen
*/

using System;

class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}

The above program explains clearly, the purpose of 'using'. You specify a namespace only once in the using statement and then avoid typing the namespace name to refer to classes that come under it. In the above program the amount of typing avoided may seem to be trivial. Just imagine you were using the namespace System.Windows.Forms, or your own namespace called ProgramminginCSharp.MyPrograms.Introduction?

From the Official Documentation:
"'Using' directives facilitate the use of namespaces and types defined in other namespaces. Using directives impact the name resolution process of namespace-or-type-names (Section 3.8) and simple-names (Section 7.5.2), but unlike declarations, using directives do not contribut
e new members to the underlying declaration spaces of the compilation units or namespaces within which they are used."

All along I've been talking about, putting classes into namespaces. But in the two versions of out program HelloWorld.cs, our class has not been put inside any namespace. What happens then? They go into the global namespace. Lets now try to write version 3 of this program that uses namespaces profitably.

/*
* HelloWorld.cs
* Version 1.3
* A program that prints Hello, world! on screen
*/

using System;

namespace CSharpTutorial
{
class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello, world!");
}
}
}

Just enclose the class within a namespace called CSharpTutorial. Just as you access, pre defined classes like System.Console, you can access this class from other programs by referring to it as CSharpTutorial.HelloWorld (this class is featureless however, to be accessed by other classes).

Namespaces also play an important role in the organization of the .NET framework documentation. All the classes have been organized by the Namespaces under which they have been grouped.

A sample that accompanies the .NET Framework SDK itself is a command-line type locater that supports sub string searches. You can use it to explore classes contained in the various namespaces. (JAVA developers will find it similar to javap). To build and use this sample, follow the instructions contained in the Readme.htm file that is located in the InstallDirectory\Samples\Applications\TypeFinder subdirectory.
Here is an example of how to use the type finder:

C:\�\TypeFinder\CS>findtype String

class System.IO.StringWriter

That concludes the tutorial. Hope you got a feel of C#!

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!