Motivation
During the past few months while I have been on the Internet after I started
up this site, many people have popped up the question to me, What is
C#? To spare me the trouble of replying to each and everyone (which I
usually do ..) I decided to put my head into a article which answers
the question.
.NET Platform
Today software technology has come a long way from what
anyone might have imagined 5 years back. Internet has been able to
connect to people all around the world helping them to communicate and
work together despite the geographical boundaries. Today everyone's
talking about convergence, e-commerce and other such terms which have
evolved with the evolution on the Internet. Benefits of Internet is
well known to you all as consumers, but the hardships faced by the
programmers who have to program for such diverse environment is known
to few.
The enterprise setup's today need to have n-tire architecture
with diverse platforms and object models communicating with each
other. The size of applications has also multiplied and developers
need to develop applications which can be accessed from any platform
(like Windows, Linux, Mac , Unix etc) and which consist of components
written in many programming languages and object models.
For people who are developing such applications its a Hell
out there! Since most of the programming languages used today have
been written 5 to 10 years back, when the language vendors did not
know the problems that would be faced by the current situation.
Many language vendors have tried to upgrade their languages, but there
is a limit to which they have been successful since they have to
maintain backward capability and face many other problems too.
Due to this new Object - Oriented languages like Java have been a obvious
choice of many developers since it has edge over the previous
languages. Microsoft has recognized this, and is aware that
there is a need for a new Microsoft technology which will help developers spend
more time actually developing huge applications rather than sit
debugging it!
To solve the woe's of the current programmers Microsoft has come with a very
promising solution. The .NET Platform.
.NET, is a platform; by this I mean its sort of a runtime environment
but much more bigger (in size too :) ) on which applications coded in
any of the managed languages run. The .NET runtime environment acts as software
layer between the applications written on the .NET and the operating system (or
hardware). This is what actually gives programs written on .NET, Java like
functionality of write once and
execute anywhere subject to the fact that you have the .NET runtime
system installed for your platform and CPU.
As I said .NET acts software abstraction between your .NET Programs (
Managed Code ) and the hardware, to run Managed Code the .NET runtime
has a Just-In-Time (JIT) compiler which JIT's the Managed code into Native
code at runtime. Remember that .NET Platform always compiles the code unlike Java
which interprets the code during runtime.
Also the .NET platform supports a variety of languages like C#, VB.NET
, Managed C++ which are being developed by Microsoft, 22 other languages like Perl,
Python, COBOL etc are also coming up with their compilers
for the .NET platform.
Microsoft has addressed the current problems in programming on a
fighting foot. It has come up with the CTS (Common Type
Specification), which is nothing but a specification of the minimum
norms all the languages which are being developed on the .NET platform
should support. This guarantees the fact that the code written in any
of the CTS compliant languages can be used (extended/inherited) from any other
language on
the .NET seamlessly. This feature has brought the interoperability issue between
languages to a extinction.
Just imagine you convert all your old C++ algorithms to Managed C++ with
minimum modification and then you use these
algorithms in Visual Basic.NET to create Windows Forms (GUI
Application) application fast and rapidly without
a single extra like to convert the code from C++ to VB !!
Second very interesting feature is the death of Memory
Management by the developer. All .NET languages are automatically garbage
collected, so Bye- Bye to all those Memory Leaks, the .NET garbage
collector does all the memory management job for you. Hence
applications on the .NET Platform are called Managed Applications.
There are many other features which I will discuss some time
later and are out of the scope of this article.
C#
According to Microsoft C# is, "C# is a simple,
modern, object oriented, and type-safe programming language derived
from C and C++. C# (pronounced "C sharp") is firmly planted in the
C and C++ family tree of languages, and will immediately be familiar
to C and C++ programmers. C# aims to combine the high productivity of
Visual Basic and the raw power of C++."
Basically, C# is the premier language being promoted by Microsoft for
the .NET Platform. C# has been created by Anders Heljsberg, famous for creating the Turbo Pascal Compiler and the leader of the team that designed Delphi, and
Scott Wiltamuth from Microsoft.
C# has been designed to provide optimum blend of simplicity, performance and creativity to leverage the power of the .NET Platform.
A huge portion of the .NET Framework library has
been coded in C#. C# was developed specifically for the .NET Platform and hence
it leverages the .NET Platform features to the Max. C# is not a upgrade to
C++, its totally a new object - oriented language. It is being truly
promoted by Microsoft that, "C# has the power of C++ and ease of
VB".
Some of the features of C# are:
1) Simple
2) Modern
3) Object - Oriented
4) Type Safe
5) Versionable
6) Maintainable
7) Interoperable
1) Simple
My nightmares of pointers in C / C++ are still alive! I can
never remember when to use "::" or when to use
"->" operator. C# has simply done away with with
pointers, this makes life much simple for the programmer. Also since
its on .NET Platform, it inherits the feature of automatic memory management
and garbage collection.
Also varying rages of the primitive types like Integer, Floats etc
according to the processor is no longer a problem. Your integer data-type Int16 will
always be of 16 bits and Int32 will always be 32 bits no matter which
processor or operating system you are on.
Integer values of 0 and 1 are no longer accepted as Boolean values. Boolean
values are pure True or False values in C#. Hence in your loops
and condition checking expressions should
evaluate to Boolean values only. Also the
operators have been made safe, so no more errors of assignment (=
operator) when
you want to actually what to check you conditions (== operator) can occur.
Microsoft has taken a long analysis of the common mistakes made by
programmers and tried to provide a solution for it in C#.
Example:
Infinite loops like below will generate a compiler error, since in C#
you cannot convert implicitly between Integer and Boolean.
int i=1 ;
while(i) //Compiler Error
{
......
.........
}
Also the below condition checking expression will produce a error at
compile time , since you are trying to assign a value instead of
making a comparison.
int i=6;
int j=8 ;
if(i=j) //Compiler Error
{
.......
........
}
So now you have to use the "= =" operator
for comparison and the "=" operator for assignment.
2) Modern
As I have explained in my introduction to the .NET platform, the
existing languages have been developed say 5 to 10 years back and lack
the concepts to meet the modern day requirements. On the other hand C#
has been based according to the current trend and is very powerful and
simple for building interoperable, scalable applications. Features
like events, error handling make programming GUI's easy. Also
a new Decimal Type has been introduced in C# to take care of all those
precise financial calculations.
3) Object - Oriented
Over the years we have seen a change in many programming models
from structural, to procedural, to modular and finally the Object -
Oriented Model. The Object - Oriented model has been based on how
objects in the real world interact. C# is a fully Object - Oriented
Language as against C++ which though called as Object Oriented allows other
object models also.
The gains of using a Object Oriented model are well known. In
C# the Type contains everything and is the building
block of any program. Data Encapsulation , inheritance ,
polymorphism are supported by C#. Although C# only supports single
inheritance, multiple inheritance of interfaces is possible.
Unlike Java in which primitive types (int, float, double) are Not
objects, C# has introduced Structures (Structs) which enable
the primitive types to become full fledged objects discarding the use
of wrapper classes of Java! Hence something like below is possible:
class TestObject{
public static void Main()
{
int i =3 ; //Declare a integer
string s = i.ToString(); //Convert the primitive type to String
type also called a Boxing
Console.WriteLine(s) ; //Print the value of s
Console.WriteLine(45.ToString()); //Directly Box the constant
(45) to string
}
}
4) Type Safe
All Variables in C# are type safe. By Type Safety I mean that you
cannot perform unsafe casts like convert a Double to a Boolean. All
variables have to be properly initialized before use or the compiler
will complain. Value Types (primitive types/structures/enums) are initialized to Zero and Reference types
(objects and classes) are initialized to Null by the
compiler automatically.
Arrays are zero base indexed and are bound
checked. Overflow of types can be checked in C# too. Hence, say
if a Int16 variable is assigned a value greater than its range, you
can set the compiler to generate a error instead of silently letting
the variable being set to a wrong value and causing problems in your code
later.
Also the Switch / Case statements don't allow fall through any more!
5) Versionable
Programmers of on the Windows platform are well, aware of the
"Dll Hell". It is caused when, some program updates a shared library, which causes the current
application to crash. In C# you can Version your Dll's so that your
application is compatible with new versions of the Dll and the runtime always
loads the correct version of the library required by the application. Different
versions of the same Dll can execute at the same time , this is called
side by side execution.
6) Maintainable
.NET has introduced assemblies which are self-describing and do not need to be
registered anywhere. Hence in the case you want to upgrade your application its as simple as deleting the old files and
updating them with new ones. You no longer have to go through the
tedious process of registering all your dll's .
7) Interoperability
Another significant gain C# gets from the .NET Platform is, interoperability of code. Even though C# does not support pointers,
you can used them as unsafe code blocks to interop with your old code. COM components can also
be easily invoked from C#. Components from VB.NET and other Managed code
languages and directly
be used in C#.
Conclusion
Due to these and many other rich features C# is one of the
promising languages built for the future. All those people doubting
the power and feature of this language should shed their fear about
C#. Its better to lead the band wagon rather than run behind it later
:).
Interested in starting up with C#? Read this article to install the .NET SDK v1.1 and this article to get started writing your own C# programs. |