Introduction to C#


(pronounced C-Sharp, just like in musical notation) is a new language for Windows applications, intended as an alternative to the main previous languages, C++ and VB. Its purpose is twofold:

  1. It gives access to many of the facilities previously available only in C++, while retaining some of the simplicity to learn of VB.
  2. It has been designed specifically with the .NET framework in mind, and hence is very well structured for writing code that will be compiled for .NET.

Where Does C# Fit In?

There are a couple of always in which you can look at C#. In this article I'll examine what C# gives you from three different perspectives.

  1. As a .NET programming language, that lets you conveniently and easily use the new .NET framework.
  2. As an object-oriented programming language that let's you program in a natural object-oriented way.
  3. As an intermediate level programming language that combines the best features of the existing pre-.NET languages of VB and C++.

C# As A .NET Programming Language

It is important to interview C# not in isolation but as an integral part of the entire .NET framework. .NET represents not only a revolution in facilities available for general-purpose Windows programming, but it will also be the immediate environment seen by your .NET code (more properly known as managed code) when it runs.

.NET brings about many benefits, including the easy-to-use object model, the garbage collection mechanism for automatically cleaning up resources, and far improved libraries covering areas ranging from Windows GUI support to data access and generating ASP.NET pages. However, for any environment there is clearly going to be a certain mechanism to access the features of that environment. In classic windows this came from the Windows API, which was written in C and therefore most easily accessible from C and C++ code (One reason why VB has never had easy access to the full features of the Windows API). The real benefit of C# is that it has been designed from scratch specifically with the .NET framework in mind. This means that in many cases the syntax and features of the language have been designed in such a way as to make it is particularly easy for you to take advantage of the features offered by .NET.

We'll illustrate this with two examples:

  1. Strings and Text Processing
    The .NET base class library includes a class, System.String, which is there to handle strings. This class is extremely powerful (far more powerful than VB's String data type), and implements a large number of methods to do all sorts of string processing, such as case conversions, trimming whitespace or extracting substrings. C# has a inbuilt awareness of this class and has its own keyword, string, which is automatically recognised by the C# compiler as meaning the System.String class. It's this ability that allows you to write C# code such as
2.           string MyString1 = "Hello";
3.           string MyString2 = "World";
4.           string MyString3 = MyString1 + ", " + MyString2;
5.           MyString3.ToUpper();
6.           // as a result MyString3 will contain "HELLO, WORLD"
  1. Garbage collection
    The .NET framework is extremely good at the task of freeing up resources. However in order to achieve this, it needs the variables stored in managed code to be stored in a certain way on the heap. C# is aware of this requirement, and automatically ensures that variables of the appropriate managed types our stored in the correct way, without you having to do anything or even be aware of the fact.

Of course we should emphasise that to these abilities aren't just confined to C#. VB has been heavily upgraded into what is effectively a new language, VB.NET, While C++ has also been extended, by the addition of certain keywords allowing you to write Managed C++ , which is also .NET aware. Both VB.NET and Managed C++ are able to provide much of the same functionality that C# does. However both of these languages are to some extent hampered by having evolved over the last decade or even longer. This means much of their syntax has not been designed with .NET in mind - which can to some extent hamper ease of development. Indeed in the case of VB.net the syntax arguably isn't really designed even to facilitate good object-oriented programming at all.

C# on the other hand comes with a clean slate. Microsoft have been able to design it from scratch looking simultaneously at the requirements of the .NET framework, the requirements of good object-oriented design, and the need to try to keep the syntax as simple as practicable. A for this reason you'll find C# is a very well designed language for working with the .NET framework.


C# As An Object Oriented Programming Language

A crucial aspect of C# is that it is a genuine object-oriented programming language. This may he not sound very revolutionary - after all C++ is also object-oriented, and VB has also been described as object-oriented.

However the problems with both the C++ and VB models.

In the case of C++, although it is a successful and well-designed object-oriented language, this is never really enabled the code-reuse that was thought likely when the object-oriented programming style first appeared. This has got a lot to do with the facts that (a) classic C++ code is not language interoperable, and (b) the way C++ works, you need access to the header files from the original source code in order to be able to inherit classes. (Inheritance is a crucial feature of object oriented code reuse and is discussed in the box below).

Some language interoperability was provided later with the introduction of COM. C++ classes could be coded up as COM objects, which meant that they could be called very easily from other applications written in any COM-aware language including VB, J++, C and C++. However COM had the disadvantage of being very hard to learn, and it also never really supported inheritance in classical object-oriented way.

VB on the other hand was great for generating COM objects, which could therefore be used by other applications, while hiding the developer from the complexities of COM. However, most OOP experts would say that VB was not truly object-oriented because VB's model was did not support inheritance.

C# (or more correctly .NET also provided these features) gives you the best of both worlds. It fully supports inheritance, but also of the .NET framework insures that enough information is included in the compiled library files (the assemblies ) that your classes can be inherited from and used by other .NET-aware code without requiring access to your source files. To this extent .NET arguably should provide real cross-language code reuse using true object-oriented principles for the first time in the history of programming.

What is inheritance?

Inheritance is a feature of the object-oriented methodology of which allows you to base classes on other classes. This allows you to specialise existing code for new situations. For example suppose you want to write a groovy new listbox that features animated text with flashing lights. Instead of writing this from scratch you could indicate in your code that your list box is inherited from Microsoft's ListBox class (which is in one of the .NET base classes). Then instead of writing the entire code for your list box, you just have to specify the bits of its implementation that are different from Microsoft's version.

The idea of inheritance is one of the main ways that object oriented programming is supposed to facilitate code reuse, saving you a lot of development time.




C# As An Intermediate Level Programming Language

In programming language terms, if you start from the lowest level of language available to Windows developer and move up, you'd see something like this:

» Assembly Language
» C, C++
» C#, J++
» VB
» VBA, scripting languages

As you move down the list you get to languages that are considered progressively easier to learn and quicker to develop code in, but which leads to less efficient code. Thus, assembly language will give you absolute top-performance code, but it's so hard to write and debug that you'll only really find it used in things like selected subroutines in some high-performance games, and some software that controls hardware devices where performance is critical. For most applications requiring good performance, C++ has traditionally been the language of choice, while if you're developing GUI applications and want them written quickly, you'll probably opt for VB. Performance isn't really an issue for GUI applications because the limiting factor tends to be how fast the user can click the mouse!

C# is designed to fill a perceived gap between VB and C++. It's aimed at developers who would like to be able to write code more easily than is possible with C++, but without the overhead of VB. (A gap that J++ seems to have largely failed to satisfy - although it has to be said that there are a lot of similarities between C# and J++). C# has a syntax that is very similar to C++, and supports many C++ language features, for example inheritance (though not multiple inheritance) of classes. For this reason C++ programmers are likely to find C# easy to learn. VB developers will find themselves having to learn a completely new syntax as well as about various object-oriented concepts like inheritance, but are still likely to find C# easier to learn than C++.

A good example of the way C# steers a middle ground between C++ and VB is on the question of memory management. In VB, the only memory management you ever need to do is remember to Set your objects to Nothing when you've finished with them - and the only penalty if you forget to do this is system resources and memory may be used for slightly longer than necessary. On the other hand, C++ not only gives developers the chance to fine tune exactly what variables occupy memory for how long (thus potentially leading to highly efficient memory usage) - it largely requires them to do so. Which means C++ programs are prone to memory management bugs, often leading to memory leaks, in a way that can't really happen in VB.

C# mostly takes the VB path on memory management - the language itself takes responsibility for handling it. But unlike VB, in C# if you want to sort out the memory yourself, you can opt to do so by declaring a section of your code as 'unsafe', and then allocating and deallocating memory in the same way as is done in C++.

{ 0 comments ... read them below or add one }

Post a Comment