A Comparison Of C And C


A Comparison Of C# And C++

This section briefly summarizes the overall differences and similarities between the two languages.

Differences

The main areas in which C# differs from C++ are as follows:

  • Compile target — C++ code usually compiles to assembly language. C# by contrast compiles to intermediate language (IL), which has some similarities to Java byte code. The IL is subsequently converted to native executable code by a process of Just-In-Time (JIT) compilation. The emitted IL code is stored in a file or set of files known as an assembly. An assembly essentially forms the unit in which IL code, along with metadata, is packaged, corresponding to a DLL or executable file that would be created by a C++ compiler.

  • Memory management — C# is designed to free the developer from memory management bookkeeping tasks. This means that in C# you do not have to explicitly delete memory that was allocated dynamically on the heap, as you would in C++. Rather, the garbage collector periodically cleans up memory that is no longer needed. To facilitate this, C# does impose certain restrictions on how you can use variables stored on the heap and is stricter about type safety than C++.

  • Pointers — Pointers can be used in C# just as in C++, but only in blocks of code that you have specifically marked for pointer use. For the most part, C# relies on Visual Basic/Java-style references for instances of classes, and the language has been designed in such a way that pointers are not required nearly as often as they are in C++.

  • Operator overloads — C# does not allow you to explicitly overload as many operators as C++. This is largely because the C# compiler automates this task to some extent by using any available custom overloads of elementary operators (like =) to work out overloads of combined operators (+=) automatically.

  • Library — Both C++ and C# rely on the presence of an extensive library. For ANSI C++ this is the standard library. C# relies on a set of classes known as the .NET base classes. The .NET base classes are based on single inheritance, whereas the standard library is based on a mixture of inheritance and templates. Also, whereas ANSI C++ keeps the library largely separate from the language itself, the interdependence in C# is much closer, and the implementation of many C# keywords is directly dependent on particular base classes.

  • Target environments — C# is specifically designed to target programming needs in GUI-based environments (not necessarily just Windows, although the language currently only supports Windows), as well as background services such as Web services. This doesn't really affect the language itself, but is reflected in the design of the base class library. C++, by contrast, was designed for more general use in the days when command-line user interfaces were dominant. Neither C++ nor the standard library includes any support for GUI elements. On Windows, C++ developers have had to rely directly or indirectly on the Windows API for this support.

  • Preprocessor directives — C# has some preprocessor directives, which follow the same overall syntax as in C++. But in general there are far fewer preprocessor directives in C#, because other C# language features make these less important.

  • Enumerators — These are present in C#, but are much more versatile than their C++ equivalents, because they are syntactically fully fledged structs in their own right, supporting various properties and methods. Note that this support exists in source code only — when compiled to native executables, enumerators are still implemented as primitive numeric types, so there is no performance loss.

  • Destructors — C# cannot guarantee when class destructors are called. In general, you should not use the programming paradigm of placing code in C# class destructors, as you can in C++, unless there are specific unmanaged resources to be cleaned up, such as file or database connections. Because the garbage collector cleans up all dynamically allocated memory, destructors are not as important in C# as they are in C++. For cases in which it is important to clean up external resources as soon as possible, C# implements an alternative mechanism involving the IDisposable interface.

  • Classes versus structs — C# formalizes the difference between classes (typically used for large objects with many methods) and structs (typically used for small objects that comprise little more than collections of variables). Among other differences, classes and structs are stored differently, and structs do not support inheritance.

Similarities

Areas in which C# and C++ are very similar include the following:

  • Syntax — The overall syntax of C# is very similar to that of C++, although numerous minor differences exist.

  • Execution flow — C++ and C# both have roughly the same statements to control flow of execution, and these generally work in the same way in the two languages.

  • Exceptions — Support for these in C# is essentially the same as in C++, except that C# allows finally blocks and imposes some restrictions on the type of object that can be thrown.

  • Inheritance model — Classes are inherited in the same way in C# as in C++. Related concepts such as abstract classes and virtual functions are implemented in the same way, although there are some differences in syntax. Also, C# supports only single inheritance of classes, but multiple interface inheritance. The similarity in class hierarchy incidentally means that C# programs will normally have a very similar overall architecture to corresponding C++ programs.

  • Constructors — Constructors work in the same way in C# as in C++, though again some differences in syntax exist.

New Features

C# introduces a number of new concepts that are not part of the ANSI C++ specification (although most of these have been introduced by Microsoft as non-standard extensions supported by the Microsoft C++ compiler). These are as follows:

  • Delegates — C# does not support function pointers. However, a similar effect is achieved by wrapping references to methods in a special form of class known as a delegate. Delegates can be passed around between methods and used to call the methods to which they contain references, in the same way that function pointers can be in C++. What is significant about delegates is that they incorporate an object reference as well as a method reference. This means that, unlike a function pointer, a delegate contains sufficient information to call an instance method in a class.

  • Events — Events are similar to delegates, but are specifically designed to support the callback model, in which a client notifies a server that it wants to be informed when some action takes place. C# uses events as a wrapper around Windows messages in the same way that Visual Basic does.

  • Properties — This idea, used extensively in Visual Basic and in COM, has been imported into C#. A property is a method or get/set pair of methods in a class that have been dressed up syntactically, so to the outside world it looks like a field. Properties allow you to write code like MyForm.Height = 400 instead of MyForm.SetHeight(400).

  • Interfaces — An interface can be thought of as an abstract class, whose purpose is to define a set of methods or properties that classes can agree to implement. The idea originated in COM. C# interfaces are not the same as COM interfaces; they are simply lists of methods and properties and such, whereas COM interfaces have other associated features such as GUIDs, but the principle is very similar. This means that C# formally recognizes the principle of interface inheritance, whereby a class inherits the definitions of functions but not any implementations.

  • Attributes — C# allows you to decorate classes, methods, parameters, and other items in code with meta-information known as attributes. Attributes can be accessed at runtime and used to determine the actions taken by your code.

New Base Class Features

The following features are new to C# and have no counterparts in the C++ language. However, support for these features comes almost entirely from the base classes, with little or no support from the C# language syntax itself. Therefore, they are not covered in this appendix. (For more details, see Chapter 11, "Reflection," and Chapter 13, "Threading.")

  • Threading — The C# language includes some support for thread synchronization, via the lock statement. (C++ has no built-in support for threads and you have to call functionality in code libraries.)

  • Reflection — C# allows code to obtain information dynamically about the definitions of classes in compiled assemblies (libraries and executables). You can actually write a program in C# that displays information about the classes and methods that it is made up from!

    Important

    There's one important feature of native C++ that is not available with C# or any other .NET language: C++ supports multiple inheritance. With C# multiple inheritance is only supported for interfaces.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net