IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesC#Language Details  Print This     

Cross Ref > Language Details

By Mike Prestwood

C# versus Delphi: A side by side comparison between C# and Delphi.

 
Language Details
 

Language Details is kind of a catch all for stuff that didn't make it into language basics nor any other category.

Custom Routines

[Other Languages] 

Languages Focus

For non-OOP languages, a custom routine is a function, procedure, or subroutine and for pure OOP languages, a custom routine is a class method. Hybrid languages (both non-OOP and OOP) combine both.

C#: 

C# requires () in both the function declaration, and when it's invoked. Leave off the parens to signify a property.

ReturnType RoutineName()
Syntax Example:
void SayHello(String pName)
{
MessageBox.Show("Hello " + pName);
}
int Add(int a, int b) 
{
return a + b;
}
Delphi:   procedure, function

Delphi is a hybrid language so you can create either class methods (functions and procedures) or you can create global functions and procedures. When a function or procedure is part of a class, it is a class method.

[function/procedure] RoutineName : ReturnType;

As with C++, your custom routine must come before it's first usage or you have to prototype it in the Interface section.

Note: Contrast Delphi with Delphi Prism which is an OOP language (everything is within a class). Both Delphi and Delphi Prism are based on Object Pascal but implement OOP features differently and have some syntax differences too.

Syntax Example:
procedure SayHello(pName: String);
begin
  ShowMessage('Hello ' + pName);
end;
 
function Add(p1, p2 : Double): Double;
begin
  Result := p1 + p2;
end;




Event Handler

[Other Languages] 

In computer programming, an event handler is part of event driven programming where the events are created by the framework based on interpreting inputs. Each event allows you to add code to an application-level event generated by the underlying framework, typically GUI triggers such as a key press, mouse movement, action selection, and an expired timer. In addition, events can represent data changes, new data, etc. Specifically, an event handler is an asynchronous callback subroutine that handles inputs received in a program.

A custom event is a programmer created event. For example, you can contrast an event handler with a member event, an OOP concept where you add an event to a class.

Languages Focus

Many development environments and compilers provide for event driven programming, a standard set of application events such as startup, end, on click of a button, etc. This section documents the applicaton event handler or an overview for each language.

For OOP languages, do not confuse this section with class member events discussed in the OOP Details section of our Cross Reference Coding Encyclopedia.

[Not specified yet. Coming...]
Delphi: 

Many objects in Delphi have events you can use to trigger code. For example, when you add a form to your project you have access to the form events such as onCreate, onShow, onHide, onDockDrop, etc. In addition, Delphi offers module level events initialization and finalization sections.

More Info / Comment




Inline Code

[Other Languages] 

Languages Focus

Also known as embedded code where you embed another syntax language within the native code of the development environment you are using. The inline code can be compiled by the current development's compiler or by an external compiler.

Do not confuse with inlining which is a coding technique where custom routines are moved inline where the code is executed either by you, by a compiler directive, or automatically by the compiler.

C#:   Not Supported

Since all the .Net languages compile into intermediate language (IL), and not to a specific CPU, they do not provide support for inline assembler code.

Delphi:   asm

In Delphi, you can inline assembler code using the asm keyword.

More Info / Comment




Inlining

[Other Languages] 

General Info: Inline Routines

Instead of calling a routine, you move the code from the routine itself and expand it in place of the call. In addition to manual inlining, some languages support automatic inlining where the compiler or some other pre-compiler decides when to inline a code routine. Also, some languages allow for developer defined inlining where the developer can suggest and/or force the inlining of a code routine. Inlining can optimize your code for speed by saving a call and return, and parameter management.

Languages Focus

Does it support inlining? If so, does it support developer defined inlining? Does it support automatic inlining? Both?

C#:   Automatic

In C#, inlining is automatically done for you by the JIT compiler for all languages and in general leads to faster code for all programmers whether they are aware of inlining or not.

More Info / Comment
Delphi:   Inline

Delphi introduced developer defined function and procedure inlining with Delphi 2005. Use the inline keyword to tell the compiler to try to inline a routine (a compiler hint). Since Delphi will only try to inline the routine, make sure you test for speed because inlining a routine can lead to slower code under some circumstances.

Syntax Example:  
function Add(a, b: Integer): Integer; inline;
begin
end;




Overloading

[Other Languages] 

Types of overloading include method overloading and operator overloading.

Method Overloading is where different functions with the same name are invoked based on the data types of the parameters passed or the number of parameters. Method overloading is a type of polymorphism and is also known as Parametric Polymorphism.

Operater Overloading allows an operator to behave differently based on the types of values used. For example, in some languages the + operator is used both to add numbers and to concatenate strings. Custom operator overloading is sometimes referred to as ad-hoc polymorphism.

C#:   implicit

C# supports both method and operator overloading.

For methods, C# supports implicit overloading (no need for an overload keyword).

Delphi:   overload

Delphi supports both method and operator overloading.

For method overloading, you use the overload keyword (all versions of the routine must include the overload keyword). The compiler chooses the correct method first based on the number of parameters, then on the type so you can have two overloaded methods with the same number of parameters so long as at least one parameter is different.

Another form of method overloading is with the use of default parameters, a shortcut syntax, where you specify a default parameter value.

Delphi also supports operator overloading with some operators.

Syntax Example:
function Add(a, b: integer): Integer;  overload;
begin
Result := a+b;
end;
  
function Add(const msg: String; a, b: integer): String; overload;
begin
  Result := msg + IntToStr(a+b);
end;




Parameters

[Other Languages] 
C#: 

In C# the data type of each parameter must be specified, even if adjacent parameters are of the same type. To pass a parameter by reference, use the ref or out keyword.

Syntax Example:

integer Add(int a, int b)

Delphi:   var, const

Object Pascal allows parameters of the same type to be listed together, separated by commas, and followed with a single data type (more params of different data types can follow, after a semi-colon).

Delphi also supports default parameters (a form of overloading).

By Reference or Value (and by constant): The default for parameters is by value. For by reference, add var in front of the parameter. Object Pascal also offers constant parameters where you add const in front of the parameter. A constant parameter is like a local constant or read-only parameter the compiler can optimize. You cannot assign a value to a constant parameter, nor can you pass one as a var parameter to another routine. (But when you pass an object reference as a constant parameter, you can still modify the object's properties.)

Syntax Example:
function Add(a, b: integer) : integer; 
begin
  Result := a + b;
end;
 
procedure ReplaceTime(var pDT: TDateTime; const pNewDT: TDateTime);
begin
end;




Self Keyword

[Other Languages] 
C#:   this

To refer to the current instance of a class, use the this keyword. The this keyword provides a way to refer to the specific instance in which the code is currently executing. It is particularly useful for passing information about the currently executing instance.

The this keyword is also used as a modifier of the first parameter of an extension method.

You cannot use this with static method functions because static methods do not belong to an object instance. If you try, you'll get an error.

More Info / Comment
Delphi:   Self

Within the implementation of a method, the identifier Self references the object in which the method is called. The Self variable is an implicit parameter for each object method. A method can use this variable to refer to its owning class.

Syntax Example:
ShowMessage('Self=' + Self.ClassName);




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]