An (incomplete) comparison of Delphi and C# features

Delphi and C# are both rich and powerful object oriented programming languages. As a reminder a short recapitulation of what that stands for : Objects are a grouping of data and methods (functions) working on that data. Objects are based on classes. A class is a declaration of the data and the code of the methods. A class will create objects using constructor methods.

An real object oriented language supports encapsulation, inheritance and polymorphism. Encapsulation hides everything inside an object except what you explicitly want to show to the outer world. Inheritance lets you re-use a class to create a  derived class with added functionality. Polymorphism lets you re-use a class and alter its exposed behavior.

Delphi has a great support for object orientation but you can write a plain Pascal program without using any object at all. In C# everything is an object, including the application itself. Running a C# program means creating an object and executing the object's main method. C# does not know how to handle any "loose" procedures or constants either, everything has to belong to a class.

The Delphi-VCL couple shares a lot with the C#-.NET couple which makes a comparison on items possible. This list is by far incomplete, it is just a road-sketch from a Delphi perspective:

Both have

In a hierarchy of objects every derived class class can introduce a new constructor. Which constructors actually get executed differs between the two languages, in Delphi each constructor has to be called explicitly. Besides the constructors Delphi's tObject base class has a method AfterConstruction, which will be executed after the last constructor has finished. So there are many places where a Delphi object can be initialized.

In C# the constructor of the ultimate base class System.object always will be executed first, the execution of a constructor of any other base class has to be explicitly asked for in the declaration of the constructor as ": base." There is no AfterConstruction in C#, everything has to be (and can be) done in the constructors themselves.

Type
    tMyClass = class(tObject)
    constructor createEx;
end;

tMyClass2 = class(tMyClass)
    constructor createEx; override;
    procedure AfterConstruction override;
end;

constructor tMyClass.CreateEx;
    begin
    ...
    end;

constructor tMyClass2.CreateEx;
    begin
    inherited CreateEx;
    ...
    end;

procedure tMyClass2.AfterConstruction
    begin
    inherited;
    // Initialization
    end;

public class MyClass
{
    public MyClass(int anInt)
        {...}
}

public class MyClass2 : MyClass
{
    public MyClass2(int anInt) : base(anInt)
        {
         ...
        // Initialization
        }
}

      

        

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. In this method the value can be checked or calculated. By only implementing only the getter or only the setter the property will be read or write only. In this example both the getter and the setter guard the property against a value less then 0.

type MyClass = class(tObject)
private
    fMyProp : integer;
    function GetMyProp: integer;
    procedure SetMyProp(const Value: integer);
public
    property MyProp : integer read GetMyProp write SetMyProp;
end;

function MyClass.GetMyProp: integer;
    begin
    result:= max(fMyProp,0);
    end;

procedure MyClass.SetMyProp(const Value: integer);
    begin
    if Value >= 0 then
       fMyProp:= Value;
    end;

In C# the value passed in the property setter is named value.

public class MyClass
{
    private int myProp = 0;
   
public  int MyProp
       
{
       
get { return Math.Max(myProp, 0); }
       
set { if (value > 0)
                 
myProp = value; }
        }
}

 

procedure MyEventHandler(sender : object)
begin
    ShowMessage('You clicked the button');
end;

MyButton.OnClick:= MyEventHandler;

private void MyClick1(object sender, System.EventArgs e)
{
   
MessageBox.Show("You clicked the button");
}

private void MyClick2(object sender, System.EventArgs e)
{
   
MessageBox.Show("And I'll tell you twice");
}

MyButton.Click += new System.EventHandler(MyClick1);
MyButton.Click += new System.EventHandler(MyClick2);

 

 

C# - .NET has

As an extra option C# offers custom type conversions. This offers the possibility to explicitly program what will happen to (the members of) an object when it is typecast from one type to another. In Inside C# Tom Archer gives a very clear example. He takes two temperature types, Celsius and Fahrenheit. Typecasting a Celsius variable to a Fahrenheit variable will handle the conversion of the value on the fly.

Celcius c = new Celsius(0F);
Fahrenheit f = (Fahrenheit)c;

The variable f will now hold a value of 32 (degrees Fahrenheit)

 

 

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. This can be very useful in your own classes. Let's take  an invoice class in which the + operator is overloaded

public class Invoice
{
    public double Total;
    public static Invoice operator+ (Invoice invoice1, Invoice invoice2)
    {
        Invoice NewInvoice = new Invoice();
        NewInvoice.Total = invoice1.Total + invoice2.Total;
        return NewInvoice;
    }
}

The method operator+ takes two Invoice objects as a parameter, constructs a new invoice and sets it's properties. After this declaration you can code:

Invoice anInvoice = new Invoice();
Invoice anotherInvoice = new Invoice();
anInvoice.Total = 1200;
anotherInvoice.Total = 2500;
textBox1.Text = "Total amount is " + (anInvoice + anotherInvoice).Total.ToString();

The + operator applied on two Invoice object will result in another Invoice object with a total amount of 3700. The + operator applied on objects of any other type will result in a default addition, the + operator is overloaded for the Invoice class.

 

Where are we ?

C# and .NET can very well be compared to Delphi and the VCL. The fact that the first is a clean start based on yesterdays lessons and today's needs make it a very tempting development environment. Working with C# in Visual Studio.net has been a very pleasing experience to me, it shows a stable and powerful implementation of all ideas and will keep the pleasure in programming for quite some time.

This article was translated to Serbo-Croatian language by Jovana Milutinovich from Webhostinggeeks.com.

This article was translated to Slovak by Juraj Rehorovsky from coupofy team

This article was translated to Vietnamese language by Julia Duong from Everycloudtech.

What's next ?