Introduction to C# (C Sharp)
Add Comment<span class="wboxheado">Motivation</span> <br> 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.<p> <span class="wboxheado">.NET Platform</span><br> 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. <br> <br> 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.<br> For people who are developing such applications its a <b>Hell</b> 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.<br> 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!<br> <br> To solve the woe's of the current programmers Microsoft has come with a very promising solution. <b>The .NET Platform</b>.<br> .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 <i>write once and execute anywhere</i> subject to the fact that you have the .NET runtime system installed for your platform and CPU.<br> 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 <i> Just-In-Time (JIT) compiler</i> 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.<br> Also the .NET platform supports a variety of languages like C#, VB.NET , Managed C++ which are being developed by Microsoft, <b>22</b> other languages like Perl, Python, COBOL etc are also coming up with their compilers for the .NET platform.<br> <br> Microsoft has addressed the current problems in programming on a fighting foot. It has come up with the <b> CTS</b> (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.<br> 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 <b>Windows Forms</b> (GUI Application) application fast and rapidly without a single extra like to convert the code from C++ to VB !!<br> <br> 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.<br> There are many other features which I will discuss some time later and are out of the scope of this article.</p> <p><span class="wboxheado">C#</span><br> According to Microsoft C# is, "<i>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++.</i>"<br> <br> Basically, C# is the premier language being promoted by Microsoft for the .NET Platform. C# has been created by <b> Anders Heljsberg</b>, famous for creating the Turbo Pascal Compiler and the leader of the team that designed Delphi, and <b> Scott Wiltamuth</b> from Microsoft.<br> 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".<br> <br> <i> Some of the features of C# are:</i><br> 1) Simple<br> 2) Modern<br> 3) Object - Oriented<br> 4) Type Safe<br> 5) Versionable<br> 6) Maintainable<br> 7) Interoperable</p> <p><span class="wboxhead">1) Simple</span><br> 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.<br> 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.<br> <br> 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.<br> Microsoft has taken a long analysis of the common mistakes made by programmers and tried to provide a solution for it in C#.<br> Example:<br> Infinite loops like below will generate a compiler error, since in C# you cannot convert implicitly between Integer and Boolean.<br> <br><span class="codetext"> int i=1 ;<br> while(i) //Compiler Error<br> {<br> ......<br> .........<br> }</span></p> <p align="justify">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.</p> <p align="justify"><span class="codetext">int i=6;<br> int j=8 ;<br> if(i=j) //Compiler Error<br> {<br> .......<br> ........<br> }</span></p> <p align="justify">So now you have to use the "= =" operator for comparison and the "=" operator for assignment.</p> <p align="justify"><span class="wboxhead">2) Modern</span><br> 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 <b> Decimal</b> Type has been introduced in C# to take care of all those precise financial calculations.</p> <p align="justify"><span class="wboxhead">3) Object - Oriented</span><br> 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.<br> The gains of using a Object Oriented model are well known. In C# the <b>Type</b> 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.<br> Unlike Java in which primitive types (int, float, double) are <b>Not</b> objects, C# has introduced Structures (<b>Structs</b>) which enable the primitive types to become full fledged objects discarding the use of wrapper classes of Java! Hence something like below is possible:</p> <p align="justify"><span class="codetext">class TestObject{<br> public static void Main()<br> {<br> int i =3 ; //Declare a integer<br> string s = i.ToString(); //Convert the primitive type to String type also called a Boxing<br> Console.WriteLine(s) ; //Print the value of s<br> Console.WriteLine(45.ToString()); //Directly Box the constant (45) to string<br> }<br> }</span></p> <p align="justify"><span class="wboxhead">4) Type Safe</span><br> 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.<br> 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.<br> Also the Switch / Case statements don't allow fall through any more!</p> <p align="justify"><span class="wboxhead">5) Versionable</span><br> 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 <i> side by side</i> execution.</p> <p align="justify"><span class="wboxhead">6) Maintainable</span><br> .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 .</p> <p align="justify"><span class="wboxhead">7) Interoperability</span><br> 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 <b>unsafe</b> 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#.</p> <p align="justify"><span class="wboxheado">Conclusion</span><br> 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 :).<br> Interested in starting up with C#? Read <a href="http://www.mastercsharp.com/article.aspx?ArticleID=88&&TopicID=4">this</a> article to install the .NET SDK v1.1 and <a href="http://www.mastercsharp.com/article.aspx?ArticleID=89&&TopicID=4">this</a> article to get started writing your own C# programs.