Similarities and Differences between Delphi and C#
This document is a heavily modified version by
Gary McGhee of the original by Peter van Ooijen available at
http://www.gekko-software.nl/DotNet/Art01.htm
The Delphi-VCL couple shares a lot with the C#-.NET couple which makes a comparison on items possible.
This is not surprising as Anders Hejlsberg was the lead architect of both languages. The common heritage can be seen, for example, in the similar or identical naming of keywords and methods. Anders has been quoted saying "Good ideas don't just go away".
This document does not take into account Delphi’s .Net version.
Both Delphi and C# have :
· Encapsulation
· Inheritance
Both Delphi and C# are limited to single inheritance, a class can have only one base class.
· Polymorphism
· Class (static) methods
· Overloading
In both languages a method can be declared several times as long as every declaration has a different signature.
· Strongly Typed
Both Delphi and C# have strong typing as a design value. Both provide the is and as operators for type checking.
· No Header Files
Delphi has “unit” files, which each have an interface and implementation section, equivalent to .c and .h files (in C terms). In C# there is no such division. Delphi units “use” each other with the uses clause, like the using clause in C#, and namespaces are specified, not files.
· Abstract methods
An abstract method is a declaration of a method without an implementation. The implementation has to be provided by the descendants.
· Interfaces
An interface is like a class with only abstract members and is in both languages an integral part of the language. In both languages a class can implement any number of interfaces, the interface list is part of the class declaration.
· Properties
To the user of the object a property behaves like any other field. In the actual implementation reading or writing the property will be done by an internal method.
· Events
In Delphi only one particular notification handling method can be assigned to the event-handler. In C# event handlers are multicast, they manage a dynamic collection of delegate objects and fire notifications to all the collection's members.
· Single Inheritance
All classes in both languages stem from one single class; TObject in Delphi and System.object in C#.
· Large standard single-rooted class library
Both environments are heavily based on a large standard tree of classes; known as VCL in Delphi and FCL in C#. These provide basic functionality such as lists, queues, streams and serialization, through to GUI design, XML processing, networking and n-Tier database access.
· Custom Components
Both environments allow custom components to be developed in the language itself, inheriting from the default class library and registered with the IDE. These appear indistinguishable from the components supplied with the product.
· 3rd Party Custom Component Market
Over the years an enormous number of commercially available 3rd party components built on the VCL have been marketed. Many of these have alternate .Net versions available. C# also has a younger but growing 3rd party component market.
· Dynamically Linked Libraries of Components
Known as Assemblies in .Net and Packages in Delphi, they are compiled and may be updated or distributed independently of each other or the application.
· Collections
The VCL has several classes which can be used as a base class for a collection. System.collections in .Net provides classes implementing IEnumerator, and items in such a collection can be enumerated with the C# foreach statement.
· Runtime Type Information
A Delphi published property has RTTI, and can be queried on type information at runtime. In C# the type information of any object is available at runtime through the GetType() method of System.object, the base class of all .NET classes. This is named Reflection in .NET.
· Exceptions
Both languages use exceptions to handle errors. Both languages support the finally clause for cleanup code. In both languages exceptions may be thrown at any time by the system (eg. Divide By Zero), not just by application code. In C# finally can be combined with catching an exception, to achieve this in Delphi two try's have to be nested.
· Interfaces
In Delphi 5 TInterfaced object was added to the VCL, a base class which implements the IUnknown interface and is refcounted. Since then TInterfacedObject has served as a base class for many components.
A .NET class can implement any interface, without doing any refcounting. All objects are being watched by the garbage collector (gc), as soon as an object is no longer referenced by anyone the gc will notice and clean up the object. This is the point where a gc really pays off.
· Minimal need for pointers
For security and to reduce runtime errors, pointers are generally not needed, but may be used in both languages.
· Accessibility Modifiers
In both languages the accessibility of members to other code can be modified using private, protected and public. Delphi adds published to indicate that this property should appear in the object inspector at design time, and should be serialized. C# adds internal to limit access to within the same assembly.
Delphi does not have :
· Attributes
Classes and methods can have metadata, properties which are of importance to the runtime environment.
· Operator overloading
As all variables in .NET are objects, performing operations on them boils down to making methods calls. Methods can be overloaded, in .NET this is also possible for many of the basic operators like +, -, <, etc.
· Struct methods
Delphi supports records, which are similar to structs in C, and do not support methods.
· Versioning of Assemblies/Packages
· Code generation for Strongly Typed DataSets and XML Schemas
· Remoting
Automatic generation of proxy classes, serialization, transport and other plumbing code to support distributed systems development.
C# does not have :
· Automatic persistence of designed forms, controls and other objects
Delphi has always provided a visual design environment that automatically stores (or “persists”) forms, controls and other objects to a text data file, unlike C# which generates the equivalent code. C# will have this ability with XAML in the forthcoming “Avalon”.
· Sets
Delphi allows a “set” or collection of binary flags to be declared that indicates whether each value in a range is present or not. Eg for a font, a single variable of type TFontStyle stores independently the presence or absence of Bold, Underline, and Italics styles.
· Default parameters
Similar to C++
· Virtual Static methods, properties or constructors
Gary McGhee of the original by Peter van Ooijen available at
http://www.gekko-software.nl/DotNet/Art01.htm
The Delphi-VCL couple shares a lot with the C#-.NET couple which makes a comparison on items possible.
This is not surprising as Anders Hejlsberg was the lead architect of both languages. The common heritage can be seen, for example, in the similar or identical naming of keywords and methods. Anders has been quoted saying "Good ideas don't just go away".
This document does not take into account Delphi’s .Net version.
Both Delphi and C# have :
· Encapsulation
· Inheritance
Both Delphi and C# are limited to single inheritance, a class can have only one base class.
· Polymorphism
· Class (static) methods
· Overloading
In both languages a method can be declared several times as long as every declaration has a different signature.
· Strongly Typed
Both Delphi and C# have strong typing as a design value. Both provide the is and as operators for type checking.
· No Header Files
Delphi has “unit” files, which each have an interface and implementation section, equivalent to .c and .h files (in C terms). In C# there is no such division. Delphi units “use” each other with the uses clause, like the using clause in C#, and namespaces are specified, not files.
· Abstract methods
An abstract method is a declaration of a method without an implementation. The implementation has to be provided by the descendants.
· Interfaces
An interface is like a class with only abstract members and is in both languages an integral part of the language. In both languages a class can implement any number of interfaces, the interface list is part of the class declaration.
· Properties
To the user of the object a property behaves like any other field. In the actual implementation reading or writing the property will be done by an internal method.
· Events
In Delphi only one particular notification handling method can be assigned to the event-handler. In C# event handlers are multicast, they manage a dynamic collection of delegate objects and fire notifications to all the collection's members.
· Single Inheritance
All classes in both languages stem from one single class; TObject in Delphi and System.object in C#.
· Large standard single-rooted class library
Both environments are heavily based on a large standard tree of classes; known as VCL in Delphi and FCL in C#. These provide basic functionality such as lists, queues, streams and serialization, through to GUI design, XML processing, networking and n-Tier database access.
· Custom Components
Both environments allow custom components to be developed in the language itself, inheriting from the default class library and registered with the IDE. These appear indistinguishable from the components supplied with the product.
· 3rd Party Custom Component Market
Over the years an enormous number of commercially available 3rd party components built on the VCL have been marketed. Many of these have alternate .Net versions available. C# also has a younger but growing 3rd party component market.
· Dynamically Linked Libraries of Components
Known as Assemblies in .Net and Packages in Delphi, they are compiled and may be updated or distributed independently of each other or the application.
· Collections
The VCL has several classes which can be used as a base class for a collection. System.collections in .Net provides classes implementing IEnumerator, and items in such a collection can be enumerated with the C# foreach statement.
· Runtime Type Information
A Delphi published property has RTTI, and can be queried on type information at runtime. In C# the type information of any object is available at runtime through the GetType() method of System.object, the base class of all .NET classes. This is named Reflection in .NET.
· Exceptions
Both languages use exceptions to handle errors. Both languages support the finally clause for cleanup code. In both languages exceptions may be thrown at any time by the system (eg. Divide By Zero), not just by application code. In C# finally can be combined with catching an exception, to achieve this in Delphi two try's have to be nested.
· Interfaces
In Delphi 5 TInterfaced object was added to the VCL, a base class which implements the IUnknown interface and is refcounted. Since then TInterfacedObject has served as a base class for many components.
A .NET class can implement any interface, without doing any refcounting. All objects are being watched by the garbage collector (gc), as soon as an object is no longer referenced by anyone the gc will notice and clean up the object. This is the point where a gc really pays off.
· Minimal need for pointers
For security and to reduce runtime errors, pointers are generally not needed, but may be used in both languages.
· Accessibility Modifiers
In both languages the accessibility of members to other code can be modified using private, protected and public. Delphi adds published to indicate that this property should appear in the object inspector at design time, and should be serialized. C# adds internal to limit access to within the same assembly.
Delphi does not have :
· Attributes
Classes and methods can have metadata, properties which are of importance to the runtime environment.
· Operator overloading
As all variables in .NET are objects, performing operations on them boils down to making methods calls. Methods can be overloaded, in .NET this is also possible for many of the basic operators like +, -, <, etc.
· Struct methods
Delphi supports records, which are similar to structs in C, and do not support methods.
· Versioning of Assemblies/Packages
· Code generation for Strongly Typed DataSets and XML Schemas
· Remoting
Automatic generation of proxy classes, serialization, transport and other plumbing code to support distributed systems development.
C# does not have :
· Automatic persistence of designed forms, controls and other objects
Delphi has always provided a visual design environment that automatically stores (or “persists”) forms, controls and other objects to a text data file, unlike C# which generates the equivalent code. C# will have this ability with XAML in the forthcoming “Avalon”.
· Sets
Delphi allows a “set” or collection of binary flags to be declared that indicates whether each value in a range is present or not. Eg for a font, a single variable of type TFontStyle stores independently the presence or absence of Bold, Underline, and Italics styles.
· Default parameters
Similar to C++
· Virtual Static methods, properties or constructors
