
05-24-2006, 10:06 PM
|
|
Senior Member
|
|
Join Date: May 2006
Posts: 99
|
|
|
C# compared to C & C++
Compared to C and C++, the language is restricted or enhanced in a number of ways, here's some of them (feel free to add to this list):
- True support for pointers. However pointers can only be used within unsafe scopes, and only programs with appropriate permissions can execute code marked as unsafe. Most object access is done through safe references, which cannot be made invalid, and most arithmetic is checked for overflow. An unsafe pointer can be made to not only value-types, but to subclasses of System.Object as well. Also safe code can be written that uses a pointer (System.IntPtr).
- Managed memory cannot be explicitly freed, but instead is garbage collected when no more references to the memory exist. (Objects that reference unmanaged resources, such as an HBRUSH, can be instructed to release those resources through the standard IDisposable interface, which provides a pattern for deterministic deallocation of resources.)
- Multiple inheritance is prohibited (although a class can implement any number of interfaces). This was a design decision by the language's lead architect (Anders Hejlsberg) to avoid complication, avoid 'dependency hell,' and simplify architectural requirements throughout CLI (Common Language Infrastructure).
- C# is more typesafe than C++. The only implicit conversions by default are safe conversions, such as widening of integers and conversion from a derived type to a base type (and this is enforced at compile-time and, indirectly, during JIT). There are no implicit conversions between booleans and integers and between enumeration members and integers, and any user-defined implicit conversion must be explicitly marked as such, unlike C++'s copy constructors.
- Syntax for array declaration is different ("int[] a = new int[5];" instead of "int a[5];").
- Enumeration members are placed in their own namespace.
- C# 1.0 lacks templates, however, C# 2.0 provides generics.
- Properties are available which results in syntax that resembles C++ member field access, similar to VB.
- Full type reflection and discovery is available.
|