List
of Articles
C# - A New opportunity
Knocking
In the last article we discussed about .NET architecture. Today we will discuss about
the new language C# (pronounced as c-sharp) launched with .NET.
Why C#
C, C++ has lots of cryptic syntax. Memory Allocation, releasing the memory that is allocated, type casting all features are surely available but very much dependent on programmer. It makes programmer responsible for releasing the memory, taking care of overflow in calculations, exception handling etc.
Assigning a character variable to integer or vice a versa is never an error because it is a valid statement. So at times when it is done wrongly, it is taken as logical mistake and not syntactical. Because of all these features, which are fantastic in one aspect but debugging and managing huge code is difficult.
Visual basic is very easy to code and maintain but doesn’t give you the power and flexibility of C, C++.
So C# is language which will give us the power and flexibility of C, C++ and simplicity of
Visual Basic.
The main advantage of C# is that it is designed in such a way that it will meet many of the new needs of programmers, along with maintaining the advantages of currently available languages.
Features of C#
- C# is a simple but powerful
programming language intended for writing enterprise applications.
- It is an evolution of C++. It
uses many features and syntax of C++.
- Lots of new features are
introduced like Type safety, Versioning, Events etc. Gives access to common
API’s provided by .NET framework.
- Full COM Support
- XML Support
Before we write a simple HelloWorld program in C#, let us understand a new keyword.
Namespaces – When lots of classes are created the chances of many programmers giving the same name is possible so a concept of namespaces is used for grouping of classes.
It is like directories and files. If Amit and Sumit are working on same machine and both of them want to create a file called as biodata.doc. So what will we suggest them? Make two directories amit and sumit and save files in corresponding directories.
So Amit should refer to his files as c:\amit\biodata.doc and Sumit should say c:\sumit\biodata.doc.
The same way we create two namespaces and classes. So if both of them have written programs as HelloWorld, in corresponding namespaces, they should refer to
their classes like amit.HelloWorld and sumit.HelloWorld. (For Java programmers, needless to say, it is same like ‘package’)
There are many such namespaces already created. And we are extensively going to use these in our code. In the simple HelloWorld program which we are going to write now, We will use System namespace. This namespace contains a predefined class called as console that provides access to the
standard input, standard output, and standard error streams. (Something similar to stdio or iostream in C & C++)
Now let us have a look at
HelloWorld program. Extension of C# programs should be .cs. using System;
using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello, World!");
}
}
The first line ‘using System’ indicates we want to use the classes of system name space in our program.
Second line is class declaration which defines access
modifier to the class which is public and name of the class. When class gets
called ‘Main’ is the first function to get called. In this program, we are
defining only main function for simplicity but there can be plenty of other
functions. We want to print ‘Hello World’ on the screen. So we call WriteLine
method of Console class to do the same. Since it is a static method, we can
directly call with class name. More about C# next week.
The author, Ms. Vaishali is
Director Fands Infotrainers and can be reached at vaishali@fandsindia.com