The Language C#
C# (pronounced: see sharp) is Microsoft's new programming language for the
.NET platform. Although .NET can also be programmed in many other languages
(for example, Visual Basic .NET, or C++) C# is Microsoft's preferred language; it
supports .NET best and is best supported by .NET.
C# is not a revolutionary new language. It is more a combination of Java, C++
andVisual Basic. The aim has been to adopt the best features of each of these languages while avoiding their more complex features. C# has been carefully developed by a small team lead by Anders Hejlsberg . Hejlsberg is an experienced language expert. At Borland he was the chief designer of Delphi. He is known to
design his languages with the needs of practitioners in mind.
In this chapter weassume that the reader already has some programming experience, preferably in Java or C++. While we are explaining the concepts of C#
we will also compare them with Java and C++.
Similarities to Java
At first sight C# programs look much like Java programs. Any Java programmer
should be able to read them. As well as having almost identical syntax the following concepts havebeen carried across from Java:
K Object-orientation. Like Java, C# is an object-oriented language with single
inheritance. Classes can inherit from just one base class but can implement
K Type safety. C# is a type-safe language. Programming errors that arise from
incompatible types in statements and expressions are detected by the compiler. There is no arbitrary pointerarithmetic and no unchecked type casts
as in C++. At run time there are checks to ensure that array indices lie in the
appropriate range, that objects are not referenced via uninitialized pointers
and that a type cast leads to a well-defined result.
The Language C#
K Garbage collection. Dynamically allocated objects are not released by the
programmer, butare automatically disposed of by a garbage collector as
soon as they are no longer referenced. This eliminates many awkward errors that can occur, for example, in C++ programs.
Namespaces. What Java calls packages C# calls namespaces. A namespace
is a collection of type declarations. It allows the same names for classes,
structures or interfaces to be used in different contexts.
Threads. C#supports lightweight parallel processes in the form of threads.
As in Java, there are mechanisms for synchronization and communication
Reflection. As in Java, type information about a program can be accessed at
run time, classes can be loaded dynamically, and it is even possible to compose executable programs at run time.
Libraries. Many types in the C# library resemble thosein the Java library.
There are familiar classes such as Object , String , Hashtable or Stream, often
even with the same methods as in Java.
Various features are also taken from C++, for example operator overloading,
pointer arithmetic in system-level classes (which must be marked as unsafe) as well
as some syntactical details, for example in connection with inheritance. From Visual Basiccomes the foreach loop, for example.
Differences from Java
Beside these similarities however, C# has several characteristics that go beyond
Java. Most of them also apply to the other .NET languages,:
K Reference parameters. Parameters can be passed not only b y value but also
by reference. Because of this, one can use not only input parameters but
also output and transientparameters.
Objects on the stack. Whereas in Java all objects are kept on the heap, in
C# an object can also be stored in the method-call stack. Such objects are
lightweight, that is, they make no demands of the garbage collector.
Block matrices. For numerical applications the Java storage model for
multi-dimensional arrays is too inefficient. C# allows the programmer to
choose whether to...