C# Tutorial Lesson 2: Comparing C# to C++ and Java

This lesson gives a brief overview of the differences between C# and the two languages that are its closest relatives. References are given in each cases to more comprehensive works currently to be found on the web.

C# versus Java

C# and Java are both new-generation languages descended from a line including C and C++. Each includes advanced features, like garbage collection, which remove some of the low level maintenance tasks from the programmer. In a lot of areas they are syntactically similar.

Both C# and Java compile initially to an intermediate language: C# to Microsoft Intermediate Language (MSIL), and Java to Java bytecode. In each case the intermediate language can be run - by interpretation or just-in-time compilation - on an appropriate 'virtual machine'. In C#, however, more support is given for the further compilation of the intermediate language code into native code.

C# contains more primitive data types than Java (lesson 4), and also allows more extension to the value types. For example, C# supports 'enumerations', type-safe value types which are limited to a defined set of constant variables (lesson 7), and 'structs', which are user-defined value types (lesson 11). (Note: Java doesn't have enumerations, but there is a standard way of emulating them - see http://java.sun.com/developer/JDCTechTips/2001/tt0807.html#tip2)

Unlike Java, C# has the useful feature that we can overload various operators.

Like Java, C# gives up on multiple class inheritance in favour of a single inheritance model extended by the multiple inheritance of interfaces (lesson 11). However, polymorphism (lesson 14) is handled in a more complicated fashion, with derived class methods either 'overriding' or 'hiding' super class methods

C# also uses 'delegates' - type-safe method pointers (see lesson 16). These are used to implement event-handling.

In Java, multi-dimensional arrays are implemented solely with single-dimensional arrays (where arrays can be members of other arrays. In addition to jagged arrays, however, C# also implements genuine rectangular arrays (lesson 6).

For more comparison of C# and Java see:

A Comparative Overview of C#

Microsoft .NET vs J2EE: How do they stack up?

C# versus C++

Although it has some elements derived from Visual Basic and Java, C++ is C#'s closest relative.

In an important change from C++, C# code does not require header files. All code is written inline.

As touched on above, the .NET runtime in which C# runs performs memory management, taking care of tasks like garbage collection. Because of this, the use of pointers in C# is much less important than in C++. Pointers can be used in C#, where the code is marked as 'unsafe' (lesson 5), but they are only really useful in situations where performance gains are at an absolute premium.

Speaking generally, the 'plumbing' of C# types is different from that of C++ types, with all C# types being ultimately derived from the 'object' type (lesson 4). There are also specific differences in the way that certain common types can be used. For instance, C# arrays are bounds checked unlike in C++, and it is therefore not possible to write past the end of a C# array.

C# statements are quite similar to C++ statements. To note just one example of a difference: the 'switch' statements has been changed so that 'fall-through' behaviour is disallowed (lesson 10).

As mentioned above, C# gives up on the idea of multiple class inheritance. Other differences relating to the use of classes are: there is support for class 'properties' of the kind found in Visual Basic, and class methods are called using the . operator rather than the :: operator.

For more comparison of C# and C++ see:

C++ -> C#: What you need to know to move from C++ to C#.

Deep Inside C#: An Interview with Microsoft Chief Architect Anders Hejlsberg.

Link Building Information